flutter - 你如何在 Flutter TimePicker 中存储 DateTime 值
问题描述
我正在使用 datetime_picker_formfield 包(https://pub.dev/packages/datetime_picker_formfield)并希望创建用户输入的两个日期时间选择器,然后在用户提交后计算它们之间的差异。
我已经向 datetime 添加了一个控制器,如下所示,但我意识到这只是一个 TextEditing Controller 而不是 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
解决方案
您的代码不包含日期信息,因此假设今天使用时差。
如果您有日期字段,您可以更改
代码片段
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.
);
}
}
推荐阅读
- angularjs - 我可以在 angularJS 应用程序中使用 angular material 2 吗?
- mongodb - 获取具有多个条件的 MongoDB 集合记录
- java - 调用“子字符串”是多余的
- javascript - 如何每 5 分钟重绘一次画布仪表板?
- aspnetboilerplate - 启动项目中的 Web.Tests 项目发生了什么?
- objective-c - Objective-C 崩溃:从类创建对象
- python - PyMongo如何获取集合中的最后一项?
- javascript - 获取特定单元中某个css属性的值
- sendmail - 由于“握手错误”,无法使用 STARTTLS 连接到 Outlook SMTP
- ruby-on-rails - 如何在 rake 任务中使用 ActiveStorage 将图像附加到对象?