【问题标题】:How can i create custom listview of dates and times in Flutter?如何在 Flutter 中创建日期和时间的自定义列表视图?
【发布时间】:2020-05-05 05:09:33
【问题描述】:

我正在使用 Flutter 开发一个待办事项应用程序,在“新任务”屏幕中,我想创建一个“提醒”按钮,该按钮显示两个自定义列表视图,看似无限的日期和时间,用户将能够从中选择。创建该按钮的最佳方法是什么?

【问题讨论】:

    标签: datetime listview button flutter flutter-layout


    【解决方案1】:

    你可以使用包https://pub.dev/packages/flutter_cupertino_date_picker
    并将日期格式设置为您需要的

    代码sn-p

    DateTimePickerWidget(
                    minDateTime: DateTime.parse(MIN_DATETIME),
                    maxDateTime: DateTime.parse(MAX_DATETIME),
                    initDateTime: DateTime.parse(INIT_DATETIME),
                    locale: DateTimePickerLocale.pt_br,
                    dateFormat: DATE_FORMAT,
                    pickerTheme: DateTimePickerTheme(
                      showTitle: false,
                      title: Container(
                        width: double.infinity,
                        height: 40.0,
                        alignment: Alignment.center,
                        child: Text('Date Time Picker Title'),
                        decoration: BoxDecoration(color: Color(0xFFc0ca33)),
                      ),
                      backgroundColor: Color(0xFFf0f4c3),
                    ),
                    onChange: (dateTime, selectedIndex) {
                      setState(() {
                        _dateTime = dateTime;
                      });
                    },
                  )
    

    工作演示,设备语言为Portuguese (PT) Brazil

    完整代码

    import 'package:flutter/material.dart';
    import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
    
    ///
    /// @author dylan wu
    /// @since 2019-05-10
    class DateTimePickerInPage extends StatefulWidget {
      DateTimePickerInPage({Key key}) : super(key: key);
    
      @override
      State<StatefulWidget> createState() => _DateTimePickerInPageState();
    }
    
    const String MIN_DATETIME = '2019-05-15 20:10:55';
    const String MAX_DATETIME = '2019-07-01 12:30:40';
    const String INIT_DATETIME = '2019-05-16 09:00:58';
    const String DATE_FORMAT = 'MMMM-EEEE-dd,HH:mm';
    
    class _DateTimePickerInPageState extends State<DateTimePickerInPage> {
      DateTime _dateTime;
    
      @override
      void initState() {
        super.initState();
        _dateTime = DateTime.parse(INIT_DATETIME);
      }
    
      @override
      Widget build(BuildContext context) {
        TextStyle hintTextStyle =
            Theme.of(context).textTheme.subhead.apply(color: Color(0xFF999999));
        return Scaffold(
          appBar: AppBar(title: Text("DateTimePicker In Page")),
          body: Container(
            decoration: BoxDecoration(color: Colors.white),
            padding: EdgeInsets.all(16.0),
            child: Column(
              children: <Widget>[
                // min datetime hint
                Padding(
                  padding: EdgeInsets.only(bottom: 8.0),
                  child: Row(
                    children: <Widget>[
                      Container(
                        width: 115.0,
                        child: Text('min DateTime:', style: hintTextStyle),
                      ),
                      Text(MIN_DATETIME,
                          style: Theme.of(context).textTheme.subhead),
                    ],
                  ),
                ),
    
                // max datetime hint
                Padding(
                  padding: EdgeInsets.only(bottom: 8.0),
                  child: Row(
                    children: <Widget>[
                      Container(
                        width: 115.0,
                        child: Text('max DateTime:', style: hintTextStyle),
                      ),
                      Text(MAX_DATETIME,
                          style: Theme.of(context).textTheme.subhead),
                    ],
                  ),
                ),
    
                // init datetime hint
                Padding(
                  padding: EdgeInsets.only(bottom: 8.0),
                  child: Row(
                    children: <Widget>[
                      Container(
                        width: 115.0,
                        child: Text('init DateTime:', style: hintTextStyle),
                      ),
                      Text(INIT_DATETIME,
                          style: Theme.of(context).textTheme.subhead),
                    ],
                  ),
                ),
    
                // date format
                Padding(
                  padding: EdgeInsets.only(bottom: 8.0),
                  child: Row(
                    children: <Widget>[
                      Container(
                        width: 115.0,
                        child: Text('Date Format:', style: hintTextStyle),
                      ),
                      Text(DATE_FORMAT, style: Theme.of(context).textTheme.subhead),
                    ],
                  ),
                ),
    
                // show custom title widget
                Row(
                  children: <Widget>[
                    Text('show custom title', style: hintTextStyle),
                    Checkbox(value: true, onChanged: (value) {}),
                  ],
                ),
    
                // custom title height
                Padding(
                  padding: EdgeInsets.only(bottom: 8.0),
                  child: Row(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(right: 8.0),
                        child: Text('custom title height:', style: hintTextStyle),
                      ),
                      Text('40.0', style: Theme.of(context).textTheme.subhead),
                    ],
                  ),
                ),
    
                // date time picker widget
                Container(
                  margin: EdgeInsets.only(top: 8.0, bottom: 40.0),
                  child: DateTimePickerWidget(
                    minDateTime: DateTime.parse(MIN_DATETIME),
                    maxDateTime: DateTime.parse(MAX_DATETIME),
                    initDateTime: DateTime.parse(INIT_DATETIME),
                    locale: DateTimePickerLocale.pt_br,
                    dateFormat: DATE_FORMAT,
                    pickerTheme: DateTimePickerTheme(
                      showTitle: false,
                      title: Container(
                        width: double.infinity,
                        height: 40.0,
                        alignment: Alignment.center,
                        child: Text('Date Time Picker Title'),
                        decoration: BoxDecoration(color: Color(0xFFc0ca33)),
                      ),
                      backgroundColor: Color(0xFFf0f4c3),
                    ),
                    onChange: (dateTime, selectedIndex) {
                      setState(() {
                        _dateTime = dateTime;
                      });
                    },
                  ),
                ),
    
                // selected date time
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('Selected DateTime:',
                        style: Theme.of(context).textTheme.subhead),
                    Container(
                      width: double.infinity,
                      padding: EdgeInsets.only(top: 4.0),
                      child: Text(
                        _dateTime != null
                            ? '${_dateTime.year}-${_dateTime.month.toString().padLeft(2, '0')}-${_dateTime.day.toString().padLeft(2, '0')} ${_dateTime.hour.toString().padLeft(2, '0')}:${_dateTime.minute.toString().padLeft(2, '0')}:${_dateTime.second.toString().padLeft(2, '0')}'
                            : '',
                        style: Theme.of(context).textTheme.title,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      }
    }
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: DateTimePickerInPage(),
        );
      }
    }
    

    【讨论】:

    • 非常感谢先生,这很有帮助!
    猜你喜欢
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多