颤抖导航到新屏幕不起作用

时间:2018-08-23 00:53:43

标签: dart flutter

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

这是按钮代码:

    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()));

                  }
               },

        )
 );

这是我的route.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()));

                  }
               },

            )
       );
     }

    }

2 个答案:

答案 0 :(得分:1)

更改此行

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

使用它,它将按您的期望工作

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

class MyApp extends StatelessWidget {
 @override
 Widget build(BuildContext context) {
  return new MaterialApp(
  title: 'Flutter Demo',
  theme: new ThemeData(
    primarySwatch: Colors.blue,
  ),
  debugShowCheckedModeBanner: false,
  home: new LBConnectApp(),
);
}
}

答案 1 :(得分:0)

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

我必须用Material App包裹我的课程,然后将我的routes属性添加到其中。这是正确的代码:

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