gpt4 book ai didi

dart - flutter 导航到新屏幕不起作用

转载 作者:IT王子 更新时间:2023-10-29 07:00:24 24 4
gpt4 key购买 nike

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

这是按钮代码:

    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 包装我的类,然后将我的路由属性添加到它。这是正确的代码:

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com