Flutter 如何直接执行login()方法,而不是重写build方法并在AccountKit上返回小部件
我正在使用我一直在使用的基本应用程序模板设置AccountKit。我使用的示例是由提供的 到目前为止,这一切都很顺利 我要做的是,我在登录屏幕上使用OnTap,以便将用户带到插件的示例实现(我的文件中的account-kit.dart) 然后,在account-kit.dart页面中,生成了一个小部件,它有一个按钮,必须单击该按钮才能执行Flutter 如何直接执行login()方法,而不是重写build方法并在AccountKit上返回小部件,flutter,flutter-layout,account-kit,Flutter,Flutter Layout,Account Kit,我正在使用我一直在使用的基本应用程序模板设置AccountKit。我使用的示例是由提供的 到目前为止,这一切都很顺利 我要做的是,我在登录屏幕上使用OnTap,以便将用户带到插件的示例实现(我的文件中的account-kit.dart) 然后,在account-kit.dart页面中,生成了一个小部件,它有一个按钮,必须单击该按钮才能执行Future login()async{…}方法 这就产生了一个问题,我的用户现在看到了一个不必要的屏幕,从我的登录切换到颤振。它是这样的: 我的登录屏幕 示例
Future login()async{…}
方法
这就产生了一个问题,我的用户现在看到了一个不必要的屏幕,从我的登录切换到颤振。它是这样的:
LoginScreen()
上的login
按钮。然后被带到accountkit.dart的login()方法,该方法将执行,它将直接带我到MessagesScreen()
但是,如果我没有在小部件构建(BuildContext context){}
上返回任何内容,并且只在其中执行login()
,我将面临错误
那么,消除屏幕上这些不必要的元素的最佳方法是什么
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: new Center(
child: RaisedButton(
padding: EdgeInsets.all(0.0),
color: _state == 2 ? Colors.green : Colors.blue,
elevation: 2.0,
splashColor: Colors.blueGrey,
child: buildButtonChild(),
onPressed: _isInitialized ? this.login : null,
),
),
),
);
}
Widget buildButtonChild() {
if (_state == 0) {
return Text(
'Login',
style: TextStyle(color: Colors.white, fontSize: 16.0),
);
} else if (_state == 1) {
return SizedBox(
height: 24.0,
width: 24.0,
child: CircularProgressIndicator(
value: null,
valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
));
} else {
return Icon(Icons.check, color: Colors.white);
}
}
@覆盖
小部件构建(构建上下文){
返回新材料PP(
家:新脚手架(
正文:新中心(
孩子:升起按钮(
填充:所有边缘设置(0.0),
颜色:_state==2?颜色。绿色:颜色。蓝色,
标高:2.0,
颜色:颜色。蓝灰色,
子项:buildButtonChild(),
onPressed:\u已初始化?this.login:null,
),
),
),
);
}
小部件buildButtonChild(){
如果(_state==0){
返回文本(
“登录”,
样式:TextStyle(颜色:Colors.white,fontSize:16.0),
);
}else如果(_state==1){
返回大小框(
身高:24.0,
宽度:24.0,
子对象:循环压缩机指示器(
值:null,
valueColor:AlwaysStoppedAnimation(颜色.白色),
));
}否则{
返回图标(Icons.check,颜色:Colors.white);
}
}
你可以在下面找到相关三页的完整代码
下图显示了正在加载的不必要屏幕。我只是不想让它们出现(第二和第三个屏幕上有额外的登录按钮)
这是启动accountkit的功能
Future<void> initAccountkit() async {
print('Init account kit called');
bool initialized = false;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
final theme = AccountKitTheme(
headerBackgroundColor: Colors.green,
buttonBackgroundColor: Colors.yellow,
buttonBorderColor: Colors.yellow,
buttonTextColor: Colors.black87);
await akt.configure(Config()
..facebookNotificationsEnabled = true
..receiveSMS = true
..readPhoneStateEnabled = true
..theme = theme
);
initialized = true;
} on PlatformException {
print('Failed to initialize account kit');
}
Future initAccountkit()异步{
打印('Init account kit called');
bool初始化=false;
//平台消息可能会失败,因此我们使用try/catch PlatformException。
试一试{
最终主题=会计基调(
headerBackgroundColor:Colors.green,
按钮背景颜色:颜色。黄色,
按钮顺序颜色:Colors.yellow,
ButtonText颜色:颜色。黑色87);
等待akt.configure(Config())
..facebookNotificationsEnabled=true
..receiveSMS=真
..readPhoneStateEnabled=true
…主题=主题
);
初始化=真;
}平台异常{
打印('未能初始化帐户套件');
}
现在,在您的登录按钮按下时调用此函数
Future loginNow() async {
//here you can call the function and handle the output(return value) as result
initAccountkit().then((result) {
// print(result);
//call login function of accountKit below
});
}
Future<void> login() async {
if (_state == 1) {
return;
}
setState(() {
_state = 1;
});
final result = await akt.logInWithPhone();
if (result.status == LoginStatus.cancelledByUser) {
print('Login cancelled by user');
setState(() {
_state = 0;
});
} else if (result.status == LoginStatus.error) {
print('Login error');
setState(() {
_state = 0;
});
} else {
print('Login success');
setState(() {
_state = 2;
});
}
}
Future loginNow()异步{
//在这里,您可以调用函数并作为结果处理输出(返回值)
initAccountkit()。然后((结果){
//打印(结果);
//调用下面accountKit的登录功能
});
}
Future login()异步{
如果(_state==1){
返回;
}
设置状态(){
_状态=1;
});
最终结果=等待akt.logInWithPhone();
if(result.status==LoginStatus.canceledbyuser){
打印(“用户取消登录”);
设置状态(){
_状态=0;
});
}else if(result.status==LoginStatus.error){
打印(“登录错误”);
设置状态(){
_状态=0;
});
}否则{
打印(“登录成功”);
设置状态(){
_状态=2;
});
}
}
谢谢您的回复。我在问题中添加了一个新的图像,详细显示了问题。我只想去掉出现的不必要的UI部分。期待它!谢谢。我根据您提出的想法解决了问题。我没有保留AccountKit页面,而是删除了整个内容并将AccountKit转移到因此,第二页的问题现在已经不存在了。让我们来看看。
Future loginNow() async {
//here you can call the function and handle the output(return value) as result
initAccountkit().then((result) {
// print(result);
//call login function of accountKit below
});
}
Future<void> login() async {
if (_state == 1) {
return;
}
setState(() {
_state = 1;
});
final result = await akt.logInWithPhone();
if (result.status == LoginStatus.cancelledByUser) {
print('Login cancelled by user');
setState(() {
_state = 0;
});
} else if (result.status == LoginStatus.error) {
print('Login error');
setState(() {
_state = 0;
});
} else {
print('Login success');
setState(() {
_state = 2;
});
}
}