如何发送其他页面/小部件的数据?

时间:2019-06-04 13:37:12

标签: flutter dart

我是新手。请解释一件事,如何将数据从一个类发送到另一个,然后再发送到下一个?

我尝试通过构造函数和InheretedWidget进行操作,但到目前为止没有任何反应。

我想从类“日期”发送“选择”到类“ ChooseTime”,然后在Approval()页面上发送。在这里,我尝试使用Date继承,但是当我尝试转到日期页面时,出现错误:构建函数返回null。我已经尝试过以不同的方式编写代码,但是对于如何在类之间传递具有选定日期的变量仍然不了解

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

1 个答案:

答案 0 :(得分:1)

如果数据较小,则可以在构造函数中发送数据,在这种情况下,您只想在选定的日期发送数据。

所以在导航至下一页的按钮中,

Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => ChooseTime(pickedDate:picked)), // Notice that i send the Picked date in
)

然后在第二页中可以使用

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...'),
      ),