尝试将数据发送到第二页

时间:2019-09-23 04:41:30

标签: flutter dart flutter-layout

我尝试将数据发送到第二页/(仪表板), 但它总是返回未为_homestate类定义的错误方法

  

_homestate是我的第一页

materialpageroute仪表板中的此错误信息

  

未为类“ _HomeState”定义方法“ DashBoard”。   尝试将名称更正为现有方法的名称,或定义一个名为“ DashBoard”的方法。


class Home extends StatefulWidget {
  Home({Key key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String tableName;
  String guestPax;
  crudMedthods crudObj = new crudMedthods();
  var table;
  Future<bool> addDialog(BuildContext context) async {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Add Table', style: TextStyle(fontSize: 15.0)),
            content: Column(
              children: <Widget>[
                TextField(
                  textInputAction: TextInputAction.next,
                  decoration: InputDecoration(hintText: 'Enter Table Name'),
                  onChanged: (value) {
                    this.tableName = value;
                  },
                ),
                SizedBox(height: 5.0),
                TextField(
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(hintText: 'Enter Guest Pax'),
                  onChanged: (value) {
                    this.guestPax = value;
                  },
                ),
                SizedBox(height: 5.0),
                Row(
                  children: <Widget>[
                    FlatButton(
                      child: Text('Add'),
                      textColor: Colors.blue,
                      onPressed: () {
                        if (!UtilsImporter()
                            .commanUtils
                            .validateTName(tableName)) {
                          UtilsImporter().commanUtils.showToast(
                              UtilsImporter().stringUtils.retrunTName, context);
                        } else if (!UtilsImporter()
                            .commanUtils
                            .validateGuestPax(guestPax)) {
                          UtilsImporter().commanUtils.showToast(
                              UtilsImporter().stringUtils.returnGuestPax,
                              context);
                        } else {
                          crudObj.addData({
                            'tableName': this.tableName,
                            'guestPax': this.guestPax
                          }).then((result) {
                            // dialogTrigger(context);
                          }).catchError((e) {
                            print(e);
                          });
                          Navigator.push(context,MaterialPageRoute(
                              builder: (context) =>
                                  DashBoard(data: tableName)));
                        }
                      },
                    ),
                    FlatButton(
                      child: Text('Return'),
                      textColor: Colors.blue,
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    )
                  ],
                ),
              ],
            ),
          );
        });
  }

  @override
  void initState() {
    crudObj.getData().then((results) {
      setState(() {
        table = results;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeLeft,
      DeviceOrientation.landscapeRight,
    ]);
    return Scaffold(
        backgroundColor: Colors.white,
        resizeToAvoidBottomPadding: false,
        appBar: AppBar(
          elevation: 0,
          actions: <Widget>[
            FlatButton(
              child: Text(
                '+Add Table',
                style: TextStyle(color: Colors.black, fontSize: 40),
              ),
              onPressed: () {
                addDialog(context);
              },
            ),
            UtilsImporter().widgetUtils.spacehorizontal(100),
            FlatButton(
                child: Text(
                  'Logout',
                  style: TextStyle(color: Colors.black, fontSize: 40),
                ),
                onPressed: () {
                  Navigator.of(context).popAndPushNamed('/Login');
                }),
            IconButton(
              icon: Icon(Icons.refresh),
              onPressed: () {
                crudObj.getData().then((results) {
                  setState(() {
                    table = results;
                  });
                });
              },
            )
          ],
          backgroundColor: Colors.blue,
          brightness: Brightness.light,
        ),
        body: tableGrid()
        // bottomNavigationBar: ,
        );
  }
}

2 个答案:

答案 0 :(得分:0)

首页

        class ProductDetailScreen extends StatefulWidget {
          @override
          _ProductDetailScreenState createState() => _ProductDetailScreenState();
        }

        class _ProductDetailScreenState extends State<ProductDetailScreen> {

     await Navigator.push(
            context, AnimationPageRoute(AddCartScreen(isFromProductDetailScreen: true), context: context));

         }

第二页

class AddCartScreen extends StatefulWidget {
  final bool isFromProductDetailScreen;

  AddCartScreen({this.isFromProductDetailScreen});

  @override
  _AddCartScreenState createState() => _AddCartScreenState();
}

答案 1 :(得分:0)

这里是导航器的示例。将数据发送到第二页,然后将数据从第二页返回到第一页。

在第一个小部件按钮上单击

_navigateOnAgreement(BuildContext context, String userId) async {

    final _agreementVal = await Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => Signature(
              userId: userId,
              agreementSignedIds: widget.__signedAgreementIds,
              driversId: widget._driverIds,)),
    );

    setState(() {
     // Refresh the current widget if some data is returned e.g.  
      //widget._agreementAgreed = _agreementVal[0];

    });
  }

这是第二个小部件如何定义

class Signature extends StatefulWidget {
  Signature({this.userId, this.agreementSignedIds,this.driversId});


  @override
  _SignatureState createState() => _SignatureState();
}

签名({this.userId,this.agreementSignedIds,this.driversId}); 是构造方法。您可以根据需要传递参数。

如果您想将一些数据返回到上一个窗口小部件

这里是一个例子:

Navigator.pop(
            context, [true, _signatureUrls, widget.agreementSignedIds]);

如果要返回单个值,则可以直接传递它。

Navigator.pop(context, false);

但是如果您必须传递多个值,则需要如上所述传递数组。