gpt4 book ai didi

flutter - 如何直接执行 login() 方法而不是覆盖构建方法并在 AccountKit 上返回一个小部件

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

我正在我一直在研究的基本应用程序模板上设置 AccountKit。我使用的示例是 AccountKit Plugin for Flutter 提供的示例.

现在,这到目前为止工作正常。

我所做的是,我在登录屏幕上使用 OnTap,以便将用户带到插件的示例实现(我文件中的 account-kit.dart)。

onTap: () {
Navigator.of(context).push(
// MaterialPageRoute(builder: (context) => MessagesScreen()),
MaterialPageRoute(builder: (context) => AccountKit()),
);
},

然后,在 account-kit.dart 页面中,生成了一个小部件 - 它有一个必须单击才能执行 Future<void> login() async{...} 的按钮。方法。

这会产生问题,我的用户现在会在从我的登录切换到 flutter 之间看到一个不必要的屏幕。它是这样的:

  1. 我的登录屏幕
  2. 示例应用程序的登录屏幕
  3. AccountKit 的登录(必需)
  4. 返回示例应用程序的登录屏幕
  5. 导航到 MessagesScreen()

这有点笨拙,所以我期待完全摆脱第 2 步和第 4 步。

所以这意味着我会点击login LoginScreen() 上的按钮.然后被带到accountkit.dart的login()方法,它会执行,它会直接带我到MessagesScreen()。 .

但是,如果我在 Widget build(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);
}
}

您可以在 THIS GIT GIST 找到相关三页的完整代码.

下图显示了正在加载的不必要的屏幕。我只是希望那些不要出现(带有额外登录按钮的第 2 和第 3 个屏幕)。

enter image description here

最佳答案

这是启动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 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;
});
}
}

关于flutter - 如何直接执行 login() 方法而不是覆盖构建方法并在 AccountKit 上返回一个小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55503802/

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