I am using the datetime_picker_formfield package ( https://pub.dev/packages/datetime_picker_formfield ) and want to create two datetime pickers that the user enters and then calculate the difference between them once the user submits.
I have added a controller to datetime as shown below, but I realize this is just a TextEditing Controller and not a datetime controller. How do I track the submitted datetime of the two fields and then get the difference of them when I submit the "Get Difference" button.
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);
} ,)
],)
Updated to include error code:
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
Yous code do not contains date information, so assume time difference use today.
You can change if you have a Date field
code snippet
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}');
} ,)
working demo and output
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
full code
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.
);
}
}
You can just wrap the contents of your dialog in StatefulBuilder. This makes the dialog's content stateful, so you can call setState().
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
...
setState(()=>do something);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.