【问题标题】:How do you store the DateTime value in Flutter TimePicker你如何在 Flutter TimePicker 中存储 DateTime 值
【发布时间】:2019-11-14 16:41:41
【问题描述】:

我正在使用 datetime_picker_formfield 包 (https://pub.dev/packages/datetime_picker_formfield) 并希望创建两个用户输入的日期时间选择器,然后在用户提交后计算它们之间的差异。

我已经向 datetime 添加了一个控制器,如下所示,但我意识到这只是一个 TextEditing 控制器而不是 datetime 控制器。如何跟踪两个字段的提交日期时间,然后在提交“获取差异”按钮时获取它们的差异。

 Row(
                                children: <Widget>[
                                  Container(
                                    width: width*.5,
                                    child:  DateTimeField(
                                      controller: _startTimeController,
                                      decoration: InputDecoration(
                                        enabledBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color: Colors.white),
                                        ),
                                        focusedBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color: Colors.white),
                                        ),
                                      ),
                                      style: TextStyle(color: Colors.white, fontSize: 32),
                                      format: format,
                                      onShowPicker: (context, currentValue) async {
                                        final time = await showTimePicker(
                                          context: context,
                                          initialTime: TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
                                        );
                                        return DateTimeField.convert(time);
                                      },
                                    ),
                                  ),
                                ],
                              ),
                              Row(
                                children: <Widget>[
                                  Container(
                                    width: width*.5,
                                    child:  DateTimeField(
                                      controller: _startTimeController,
                                      decoration: InputDecoration(
//        suffixIcon: IconButton(icon: Icon(Icons.close, color: Colors.white,), onPressed: state.clear,),
                                        enabledBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color: Colors.white),
                                        ),
                                        focusedBorder: UnderlineInputBorder(
                                          borderSide: BorderSide(color: Colors.white),
                                        ),
                                      ),
//                                      initialValue: yesterday10pm,
                                      style: TextStyle(color: Colors.white, fontSize: 32),
                                      format: format,
                                      onShowPicker: (context, currentValue) async {
                                        final time = await showTimePicker(
                                          context: context,
                                          initialTime: TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
                                        );
                                        return DateTimeField.convert(time);
                                      },
                                    ),
                                  ),
                                ],
                              ),
                              Row(children: <Widget>[
                                RaisedButton(child: Text("Get Difference"), onPressed: () {
                                  print(_startTimeController);
                                  print(_endTimeController);
                                } ,)
                              ],)

更新为包含错误代码:

flutter: 2019-11-14 04:25 PM
flutter: 2019-11-14 10:34 PM
flutter: ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
flutter: The following FormatException was thrown while handling a gesture:
flutter: Invalid date format
flutter: 2019-11-14 04:25 PM

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您的代码不包含日期信息,因此假设今天使用时差。
    如果您有日期字段,您可以更改

    代码sn-p

    RaisedButton(child: Text("Get Difference"), onPressed: () {
                  var now = new DateTime.now();
                  var formatter = new DateFormat('yyyy-MM-dd');
                  String today = formatter.format(now);
                  String startDateTime = "${today} ${_startTimeController.text}";
                  String endDateTime = "${today} ${_endTimeController.text}";
                  print(startDateTime);
                  print(endDateTime);
    
                  var parsedstartDateTime = DateTime.parse(startDateTime);
                  var parsedendDateTime = DateTime.parse(endDateTime);
    
                  Duration difference = parsedendDateTime .difference(parsedstartDateTime);
                  print(' Days ${difference.inDays}');
                  print(' Hours ${difference.inHours}');
                  print(' Mins ${difference.inMinutes}');
    
                } ,)
    

    工作演示和输出

    I/flutter (22206): 2019-11-15 02:12
    I/flutter (22206): 2019-11-15 08:18
    I/flutter (22206):  Days 0
    I/flutter (22206):  Hours 6
    I/flutter (22206):  Mins 366
    

    完整代码

    import 'package:flutter/material.dart';
    import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
    import 'package:intl/intl.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            // This is the theme of your application.
            //
            // Try running your application with "flutter run". You'll see the
            // application has a blue toolbar. Then, without quitting the app, try
            // changing the primarySwatch below to Colors.green and then invoke
            // "hot reload" (press "r" in the console where you ran "flutter run",
            // or simply save your changes to "hot reload" in a Flutter IDE).
            // Notice that the counter didn't reset back to zero; the application
            // is not restarted.
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      // This widget is the home page of your application. It is stateful, meaning
      // that it has a State object (defined below) that contains fields that affect
      // how it looks.
    
      // This class is the configuration for the state. It holds the values (in this
      // case the title) provided by the parent (in this case the App widget) and
      // used by the build method of the State. Fields in a Widget subclass are
      // always marked "final".
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      final format = DateFormat("HH:mm");
      TextEditingController _startTimeController = TextEditingController();
      TextEditingController _endTimeController = TextEditingController();
    
      void _incrementCounter() {
        setState(() {
          // This call to setState tells the Flutter framework that something has
          // changed in this State, which causes it to rerun the build method below
          // so that the display can reflect the updated values. If we changed
          // _counter without calling setState(), then the build method would not be
          // called again, and so nothing would appear to happen.
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        // This method is rerun every time setState is called, for instance as done
        // by the _incrementCounter method above.
        //
        // The Flutter framework has been optimized to make rerunning build methods
        // fast, so that you can just rebuild anything that needs updating rather
        // than having to individually change instances of widgets.
        return Scaffold(
          appBar: AppBar(
            // Here we take the value from the MyHomePage object that was created by
            // the App.build method, and use it to set our appbar title.
            title: Text(widget.title),
          ),
          body: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Container(
                    color: Colors.deepOrangeAccent,
                    width: 200,
                    child:  DateTimeField(
                      controller: _startTimeController,
                      decoration: InputDecoration(
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                        ),
                        focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                        ),
                      ),
                      style: TextStyle(color: Colors.white, fontSize: 32),
                      format: format,
                      onShowPicker: (context, currentValue) async {
                        final time = await showTimePicker(
                          context: context,
                          initialTime: TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
                        );
                        return DateTimeField.convert(time);
                      },
                    ),
                  ),
                ],
              ),
              Row(
                children: <Widget>[
                  Container(
                    color: Colors.blue,
                    width: 200,
                    child:  DateTimeField(
                      controller: _endTimeController,
                      decoration: InputDecoration(
    //        suffixIcon: IconButton(icon: Icon(Icons.close, color: Colors.white,), onPressed: state.clear,),
                        enabledBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                        ),
                        focusedBorder: UnderlineInputBorder(
                          borderSide: BorderSide(color: Colors.white),
                        ),
                      ),
    //                                      initialValue: yesterday10pm,
                      style: TextStyle(color: Colors.white, fontSize: 32),
                      format: format,
                      onShowPicker: (context, currentValue) async {
                        final time = await showTimePicker(
                          context: context,
                          initialTime: TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
                        );
                        return DateTimeField.convert(time);
                      },
                    ),
                  ),
                ],
              ),
              Row(children: <Widget>[
                RaisedButton(child: Text("Get Difference"), onPressed: () {
                  var now = new DateTime.now();
                  var formatter = new DateFormat('yyyy-MM-dd');
                  String today = formatter.format(now);
                  String startDateTime = "${today} ${_startTimeController.text}";
                  String endDateTime = "${today} ${_endTimeController.text}";
                  print(startDateTime);
                  print(endDateTime);
    
                  var parsedstartDateTime = DateTime.parse(startDateTime);
                  var parsedendDateTime = DateTime.parse(endDateTime);
    
                  Duration difference = parsedendDateTime .difference(parsedstartDateTime);
                  print(' Days ${difference.inDays}');
                  print(' Hours ${difference.inHours}');
                  print(' Mins ${difference.inMinutes}');
    
                } ,)
              ],)
            ],
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ), // This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }
    

    【讨论】:

    • 谢谢@chunhunghan!不幸的是,我收到以下错误,我已经更新了我的答案。知道为什么我会得到这个吗?
    • 我使用这个最终格式 = DateFormat("HH:mm");不会有“PM”或“AM”。你可以复制粘贴运行我的完整代码,看看它是否有效。
    【解决方案2】:

    您可以将对话框的内容包装在 StatefulBuilder 中。这使得对话框的内容是有状态的,所以你可以调用 setState()。

    StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
        ...
        setState(()=>do something);
    

    【讨论】:

      猜你喜欢
      • 2014-09-27
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 2020-12-07
      • 2023-02-22
      • 1970-01-01
      相关资源
      最近更新 更多