gpt4 book ai didi

flutter - 导航到下一个屏幕时如何避免键盘弹出?

转载 作者:行者123 更新时间:2023-12-03 03:15:08 28 4
gpt4 key购买 nike

当我从 LoginScreen() 导航到 HomeScreen() 时,键盘无缘无故地弹出一瞬间,然后在导航过程中立即关闭。

调用屏幕pushReplacement的函数:

    () async {
if (_formKey.currentState.validate()) {
final FirebaseAuth _auth = FirebaseAuth.instance;
try {
await _auth.signInWithEmailAndPassword(
email: email, password: password);
} catch (e) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Email or Password are incorrect'),
));
}

_user = await _auth.currentUser();

if (_user == null) {}
if (_user.isEmailVerified == true) {
Navigator.of(context)
.pushReplacementNamed(HomeScreen.routeName);
} else
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('Validate your email pls!'),
));
}
},

和 HomeScreen():

    import 'package:final_login/screens/loginscreen.dart';
import 'package:final_login/services/auth.dart';
import 'package:flutter/material.dart';

class HomeScreen extends StatefulWidget {
static const routeName = '/home-screen';
@override
_HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('Log out'),
onPressed: (){
AuthService().signOut();
Navigator.of(context).pushReplacementNamed(LoginScreen.routeName);
},
),

],
),
),
);
}
}

通过在函数调用之前放置 FocusScope.of(context).requestFocus(FocusNode()); 解决。

最佳答案

更新:

下面的答案不再是 flutter 更新后最好的方法,现在这是更好的方法:

FocusScope.of(context).unfocus();

旧答案

试试这个,它会在推送新页面之前以编程方式删除键盘:

if (_user.isEmailVerified == true) {
FocusScope.of(context).requestFocus(FocusNode());
Navigator.of(context).pushReplacementNamed(HomeScreen.routeName);
}

关于flutter - 导航到下一个屏幕时如何避免键盘弹出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61373774/

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