I have previously posted about this problem I am still facing Which is to return data from a Stateful widget back to a Stateless Widget
The Widget I am using is Dat
In this situation passing data with Navigator is not suitable. Because there isn't page transition between the page and your dateTime
Widget. I recommend you to implement ValueChanged
callback to pass data between widgets in same screen.
It's little bit tricky. But material.dart
's widgets often use this technique. I hope this will help you!
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
DateTime dateT;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Add a Reminder'),
),
body: new Container(
padding: new EdgeInsets.all(20.0),
child: new Form(
child: new ListView(
children: [
new TextFormField(
keyboardType: TextInputType.text,
// Use email input type for emails.
decoration: new InputDecoration(
hintText: 'Title of reminder',
),
),
dateTime(
onDateTimeChanged: (newDateTime) {
dateT = newDateTime;
},
),
RaisedButton(
child: Text('Save'),
onPressed: () {
Navigator.pop(context);
},
)
],
),
),
),
);
}
}
class dateTime extends StatefulWidget {
final ValueChanged onDateTimeChanged;
dateTime({Key key, this.onDateTimeChanged}) : super(key: key);
@override
dateTimeState createState() => dateTimeState();
}
class dateTimeState extends State {
DateTime dateT;
InputType inputType = InputType.both;
final formats = {
InputType.both: DateFormat("EEEE, MMMM d, yyyy 'at' h:mma"),
InputType.date: DateFormat('yyyy-MM-dd'),
InputType.time: DateFormat("HH:mm"),
};
Widget build(BuildContext context) => Container(
child: DateTimePickerFormField(
inputType: InputType.both,
editable: true,
format: formats[inputType],
decoration: InputDecoration(
labelText: 'Date/Time', hasFloatingPlaceholder: false),
onChanged: (dt) {
widget.onDateTimeChanged(dt);
},
),
);
}
(Btw your dateAndTimeWidget
is a method, is not a Widget. So if you assign it to Column`s children(List), Flutter framework cannot understand.)