我正在构建一个将数据传递到主屏幕的登录屏幕,当我单击登录按钮时,什么也没有发生。没有错误,但也没有发送到新屏幕。

这是按钮代码:

    Widget submitButton()
    {
 return Container(
        margin: EdgeInsets.all(10.0),
        child:RaisedButton(
        child: Text('LOGIN'),
        onPressed: (){
          if(formKey.currentState.validate()){

            formKey.currentState.save();
             print('Time to save $emailAddress and $passWord to my API');
             Navigator.push(
               context,
                      new MaterialPageRoute(
                     builder: (BuildContext context) =>
                     new Home()));

                  }
               },

        )
 );

这是我的 routes.dart 文件
 import 'package:flutter/material.dart';
 import '../screens/home.dart';
 import 'package:lbconnectv3/main.dart';

 final routes = {
  '/home':         (BuildContext context) => new Home(),
   '/' :          (BuildContext context) => new LBConnectApp(),
   };

编辑

main.dart 的完整代码,它基本上有一个表单,其中包含 2 个用于登录的文本字段。控制台中没有错误,它进入 if 语句。
import 'package:flutter/material.dart';
import 'utils/validation.dart';
import 'screens/home.dart';
import 'utils/routes.dart';

 void main() => runApp(new LBConnectApp());

  class LBConnectApp extends StatefulWidget {

  createState()
  {
     return new LBConnectAppState();
   }

   }

   class LBConnectAppState extends State<LBConnectApp> with Validation
   {

      final formKey = GlobalKey<FormState>();
      String emailAddress = '';
      String passWord = '';
      Widget build(context) {
      return MaterialApp(

       title: "LB Connect",
        home: Scaffold(
        backgroundColor: const Color(0xFF0099a9),
        body:
        Container(
        margin: EdgeInsets.all(25.0),
        child: Form(
        key: formKey,
        child: Column(
         children: [
          Container(
            margin: EdgeInsets.only(top: 100.0),
            child:Image.asset('images/lb_connect_trans.png')
           ),
           emailField(),
           passwordField(),
          submitButton(),
        ],
         ),
        ),
       )
       ),
       );
       }

       Widget emailField()
       {
         return TextFormField(
         keyboardType: TextInputType.emailAddress,
          decoration : InputDecoration(
           labelText: 'Email Address',
         ),

         validator: validateEmail,
         onSaved: (String value){

           emailAddress = value;

         },

       );
       }

       Widget passwordField()
       {
           return TextFormField(
         obscureText: true,
         decoration : InputDecoration(
           labelText: 'Password',

         ),

         validator: validatePassword,
          onSaved: (String value){

            passWord = value;
         },

         );
       }

       Widget submitButton()
      {
          return Container(
        margin: EdgeInsets.all(10.0),
        child:RaisedButton(
        child: Text('LOGIN'),
        onPressed: (){
          if(formKey.currentState.validate()){

            formKey.currentState.save();
             print('Time to save $emailAddress and $passWord to my API');
             Navigator.push(
               context,
                      new MaterialPageRoute(
                     builder: (BuildContext context) =>
                     new Home()));

                  }
               },

            )
       );
     }

    }

最佳答案

在弄乱我的应用程序后,我发现了问题:)

我必须用 Material App 包装我的类(class),然后将我的 routes 属性添加到它。这是正确的代码:

 void main() => runApp(MaterialApp(
 routes: routes,
 home: LBConnectApp()
 )
);

关于dart - flutter 导航到新屏幕不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51976964/

10-11 22:28
查看更多