Flutter日期选择器插件moon_ruler_picker的使用

Flutter日期选择器插件moon_ruler_picker的使用

描述

此包为您提供了一个优秀的尺规选择器!
体验尺规选择器通过平滑的滑动手势平稳滑动,并自然停止!
可以通过设置加速度和阻力来自定义尺规选择器的移动行为。

gif.gif

开始使用

在您的 pubspec.yaml 文件中添加依赖:

flutter pub add moon_ruler_picker

使用方法

以下是一个简单的示例,展示了如何在Flutter应用中使用moon_ruler_picker

示例代码

import 'package:flutter/material.dart';
import 'package:moon_ruler_picker/ruler_picker_lib.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'moon ruler-picker'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  [@override](/user/override)
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  double _doubleData = 0;
  int _intData = 0;
  MoonRulerLinesType rulerLinesType = MoonRulerLinesType.lineWithLabel;
  double longVerticalLineHeightRatio = 0.8;
  double shortVerticalLineHeightRatio = 0.55;
  double selectedVerticalLineHeightRatio = 1.2;

  [@override](/user/override)
  Widget build(BuildContext context) {

    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Form(
              child: TextFormField(
                controller: TextEditingController(text: _doubleData.toStringAsFixed(2)),
                key: UniqueKey(),
                onChanged: (data) {
                  _doubleData = double.tryParse(data) ?? 0;
                  _intData = _doubleData.floor();
                },
                onEditingComplete: () {
                  setState(() {});
                },
              ),
            ),

            Form(
              child: TextFormField(
                controller: TextEditingController(text: _intData.toString()),
                key: UniqueKey(),
                onChanged: (data) {
                  _doubleData = double.tryParse(data) ?? 0;
                  _intData = _doubleData.floor();
                },
                onEditingComplete: () {
                  setState(() {});
                },
              ),
            ),

            SizedBox(
              width: 400,
              height: 200,
              child: RulerPicker(
                  height: 200,
                  minNumber: 0,
                  maxNumber: 100,
                  longVerticalLineHeightRatio: longVerticalLineHeightRatio,
                  shortVerticalLineHeightRatio: shortVerticalLineHeightRatio,
                  selectedVerticalLineHeightRatio: selectedVerticalLineHeightRatio,
                  resistance: 1,
                  acceleration: 1,
                  linesType: rulerLinesType,
                  callbackDouble: (data) {
                    setState(() {
                      _doubleData = data;
                    });
                  },
                  callbackInt: (data) {
                    setState(() {
                      _intData = data;
                    });
                  },
                  initNumber: _doubleData,
                  borderWidth: 2,
                  pickedBarColor: Colors.red,
                  barColor: const Color(0XFF0180BE).withOpacity(0.3)
              ),
            ),

            const Spacer(),

            ElevatedButton(
                onPressed: () {
                  setState(() {
                    rulerLinesType = MoonRulerLinesType.circularLine;
                    longVerticalLineHeightRatio = 1;
                    shortVerticalLineHeightRatio = 0.85;
                    selectedVerticalLineHeightRatio = 1;
                  });
                },
                child: Text('尺规圆选择器')
            ),

            ElevatedButton(
                onPressed: () {
                  setState(() {
                    rulerLinesType = MoonRulerLinesType.line;
                    longVerticalLineHeightRatio = 0.8;
                    shortVerticalLineHeightRatio = 0.55;
                    selectedVerticalLineHeightRatio = 1.2;
                  });
                },
                child: Text('尺规线选择器')
            ),

            ElevatedButton(
                onPressed: () {
                  setState(() {
                    rulerLinesType = MoonRulerLinesType.lineWithLabel;
                    longVerticalLineHeightRatio = 0.8;
                    shortVerticalLineHeightRatio = 0.55;
                    selectedVerticalLineHeightRatio = 1.2;
                  });
                },
                child: Text('带标签的尺规线选择器')
            ),

            const Spacer(),

          ],
        ),
      ),
    );
  }
}

更多关于Flutter日期选择器插件moon_ruler_picker的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter日期选择器插件moon_ruler_picker的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用moon_ruler_picker日期选择器插件的一个示例代码案例。moon_ruler_picker是一个流行的日期选择器插件,支持多种日期选择模式,如年月日选择、年月选择等。

首先,你需要在你的pubspec.yaml文件中添加对moon_ruler_picker的依赖:

dependencies:
  flutter:
    sdk: flutter
  moon_ruler_picker: ^版本号  # 替换为最新的版本号

然后运行flutter pub get来安装依赖。

接下来,在你的Flutter项目中,你可以使用以下代码来展示一个日期选择器:

import 'package:flutter/material.dart';
import 'package:moon_ruler_picker/moon_ruler_picker.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Date Picker Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: DatePickerDemo(),
    );
  }
}

class DatePickerDemo extends StatefulWidget {
  @override
  _DatePickerDemoState createState() => _DatePickerDemoState();
}

class _DatePickerDemoState extends State<DatePickerDemo> {
  DateTime selectedDate = DateTime.now();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Date Picker Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Selected Date: ${selectedDate.toLocal()}',
              style: TextStyle(fontSize: 20),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                _showDatePicker(context);
              },
              child: Text('Select Date'),
            ),
          ],
        ),
      ),
    );
  }

  void _showDatePicker(BuildContext context) async {
    final DatePickerController _pickerController = DatePickerController();

    final DateTime? pickedDate = await showDatePicker(
      context: context,
      pickerController: _pickerController,
      initialDate: selectedDate,
      firstDate: DateTime(1900),
      lastDate: DateTime(2101),
      locale: Localizations.localeOf(context),
      builder: (BuildContext context, Widget? child) {
        return Theme(
          data: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
          ),
          child: child!,
        );
      },
      dialogBackgroundColor: Colors.white,
      cancelText: 'Cancel',
      confirmText: 'Confirm',
      headerTextStyle: TextStyle(fontSize: 20, color: Colors.black),
      pickerTextStyle: TextStyle(fontSize: 18),
      showTitleActions: true,
      title: Text(
        'Select Date',
        style: TextStyle(fontSize: 24, color: Colors.black),
      ),
      onConfirmed: (DateTime date) {
        setState(() {
          selectedDate = date;
        });
      },
      onCanceled: () {
        print('Picker canceled.');
      },
    );

    if (pickedDate != null && pickedDate != selectedDate) {
      setState(() {
        selectedDate = pickedDate;
      });
    }
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,其中包含一个按钮和一个显示选定日期的文本。点击按钮会弹出一个日期选择器对话框,用户可以选择一个日期,选择完成后,选定的日期会显示在文本中。

请注意,moon_ruler_picker提供了很多自定义选项,如日期范围、语言、主题等,你可以根据需求进一步调整上述代码。

此外,如果你希望使用moon_ruler_picker提供的更多高级功能,如年月日选择器、年月选择器、星期选择器等,请参考插件的官方文档或GitHub仓库中的示例代码。

回到顶部