flutter - Flutter,卡在登录
问题描述
登录后我正在尝试重定向到新屏幕
我正在编写一个应用程序,需要帮助更改正在看到的屏幕,当按下谷歌登录按钮时,它会验证个人资料,但我不知道如何在登录后让它进入主页,所以它只是重定向回登录页面并重复登录过程。我有什么办法解决这个问题吗?
import 'package:flutter/material.dart';
import 'auth.dart';
import 'flutter_auth_buttons.dart';
class LoginScreen2 extends StatelessWidget {
final Color backgroundColor1;
final Color backgroundColor2;
final Color highlightColor;
final Color foregroundColor;
final AssetImage logo;
LoginScreen2({Key k, this.backgroundColor1, this.backgroundColor2, this.highlightColor, this.foregroundColor, this.logo});
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
begin: Alignment.centerLeft,
end: new Alignment(1.0, 0.0),
// 10% of the width, so there are ten blinds.
colors: [this.backgroundColor1, this.backgroundColor2],
// whitish to gray
tileMode: TileMode
.repeated, // repeats the gradient over the canvas
),
),
height: MediaQuery
.of(context)
.size
.height,
child: Column(
children: <Widget>[
Container(
padding: const EdgeInsets.only(top: 150.0, bottom: 50.0),
child: Center(
child: new Column(
children: <Widget>[
Container(
height: 128.0,
width: 128.0,
child: new CircleAvatar(
backgroundColor: Colors.transparent,
foregroundColor: this.foregroundColor,
radius: 100.0,
child: new Text(
"",
style: TextStyle(
fontSize: 40.0,
fontWeight: FontWeight.w100,
),
),
),
),
new Padding(
padding: const EdgeInsets.all(16.0),
)
],
),
),
),
new Container(
width: MediaQuery
.of(context)
.size
.width,
margin: const EdgeInsets.only(left: 40.0, right: 40.0),
alignment: Alignment.center,
decoration: BoxDecoration(
),
padding: const EdgeInsets.only(left: 0.0, right: 10.0),
child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
),
new Container(
width: MediaQuery
.of(context)
.size
.width,
margin: const EdgeInsets.only(
left: 40.0, right: 40.0, top: 10.0),
alignment: Alignment.center,
decoration: BoxDecoration(
),
padding: const EdgeInsets.only(left: 0.0, right: 10.0),
child: new Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
),
new Container(
width: MediaQuery
.of(context)
.size
.width,
margin: const EdgeInsets.only(
left: 40.0, right: 40.0, top: 30.0),
alignment: Alignment.center,
child: new Row(
),
),
new Container(
width: MediaQuery
.of(context)
.size
.width,
margin: const EdgeInsets.only(
left: 100.0, right: 40.0, top: 10.0),
alignment: Alignment.center,
child: new Row(
children: <Widget>[
GoogleSignInButton(onPressed: () =>authService.googleSignIn (
)) ,
new Expanded(
child: new FlatButton(
padding: const EdgeInsets.symmetric(
vertical: 20.0, horizontal: 20.0),
color: Colors.transparent,
onPressed: () => {},
child: Text(
"",
style: TextStyle(
color: this.foregroundColor.withOpacity(0.5)),
),
),
),
],
),
),
new Expanded(child: Divider(),),
new Container(
width: MediaQuery
.of(context)
.size
.width,
margin: const EdgeInsets.only(
left: 40.0, right: 40.0, top: 10.0, bottom: 20.0),
alignment: Alignment.center,
child: new Row(
children: <Widget>[
new Expanded(
child: new FlatButton(
padding: const EdgeInsets.symmetric(
vertical: 20.0, horizontal: 20.0),
color: Colors.transparent,
onPressed: () => {},
child: Text(
"Don't have an account? Create One",
style: TextStyle(
color: this.foregroundColor.withOpacity(0.5)),
),
),
),
],
),
),
],
),
)
]);
}}
class SubPage extends StatelessWidget{
@override
Widget build(BuildContext context){
return new Scaffold(
appBar: new AppBar(
title: new Text('Tile'),
),
body: new GridView.count(
crossAxisCount: 2,
children: new List<Widget>.generate(1000, (index) {
return new GridTile(
child: new Card(
color: Colors.blue.shade200,
child: new Center(
child: new Text('tile $index'),
)
),
);
}),
),
);
}
}
解决方案
我假设谷歌登录按钮正在调用一个异步作业,该作业对用户信息进行身份验证并返回一个通知它是否成功登录的承诺。
在这种情况下,您可以在收到响应后导航到主页。
GoogleSignInButton(
onPressed: () => authService.googleSignIn ().then((response){
if(response is okay){
Navigator.push(context, MaterialPageRoute(builder: (context) => HomePage()));
}
})
)
推荐阅读
- python - 从字符串列表中获取整数的总和
- shell - 在多个服务器上运行命令的脚本
- buffer - vulkan pushConstant vs 统一缓冲区更新
- json - 错误类型错误:无法读取未定义的属性“日期/时间”
- machine-learning - 聚类算法的性能指标是什么?
- java - 如何在 jBPM 中查看自定义流程事件监听器的日志
- android-studio - 按钮为空对象引用(Android Studio)
- flutter - 如何在运行时创建两条线并计算它的颤动角度?
- c++ - MongoDB 中 javascript 函数的替代方案
- reactjs - 制作动态 crud 时是否可以使用 getStaticProps 方法?