Reputation: 43
I'm new to flutter. Please explain one thing, how do I send data from one class to another and then to the next?
I tried to do it through the constructor and InheretedWidget, but so far nothing happened.
I wanna from class 'Date' sent "picked" to class 'ChooseTime' and after on the Approval() page. Here I'm trying to use Date inheritance, but when I try to go to the date page, an error appears: the build function returned null. I have already tried to write code in different ways, but still there is no understanding of how a variable with the selected date should be passed between classes
var sixtyDaysFromNow = now.add(new Duration(days: 30));
var dateNow = now.subtract(new Duration(days: 1));
class Date extends InheritedWidget {
Date({
Widget child,
this.date,
}) : super(child: child);
bool updateShouldNotify(Date oldWidget) => date != oldWidget.date;
static Date of(BuildContext context) =>
context.inheritFromWidgetOfExactType(Date);
DateTime date = new DateTime.now();
Future<Null> _selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: date,
firstDate: dateNow,
lastDate: sixtyDaysFromNow,
);
if(picked != null && picked != date) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChooseTime()),
);
Widget build(BuildContext context) {
final date = Date.of(context).date;
return new Scaffold(
appBar: new AppBar(
title: new Text('Запись на маникюр'),
),
///////////Second page
class ChooseTime extends StatefulWidget {
@override
_State createState() => new _State();
}
class _State extends State<ChooseTime> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('online...'),
),
body: new Container(
// padding: new EdgeInsets.all(32.0),
child: new Column(
children: <Widget>[new Text(
'You choosed $date, choose time:',
style: new TextStyle(
fontSize: 20.0, fontWeight: FontWeight.bold),
new FlatButton(
color: Colors.blue,
textColor: Colors.white,
disabledColor: Colors.grey,
disabledTextColor: Colors.black,
padding: EdgeInsets.all(8.0),
splashColor: Colors.blueAccent,
key: Key("10:00"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Approval())///
As a result, I want to make the selected Date passed to another class
Upvotes: -1
Views: 635
Reputation: 3653
You can send the data in the constructor, if the data is small in which your case you only want to send in the chosen date.
So in your button which navigates to the next page do
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChooseTime(pickedDate:picked)), // Notice that i send the Picked date in
)
Then in your second page you can use
class ChooseTime extends StatefulWidget {
ChooseTime({key:key,this.pickedDate}): super(key: key); // constructor
DateTime pickedDate;
@override
_State createState() => new _State();
}
class _State extends State<ChooseTime> {
// then here you can just call widget.pickedDate to use it
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('online...'),
),
Upvotes: 1