【发布时间】:2022-04-16 16:07:53
【问题描述】:
当我从 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());放在函数调用之前解决。
【问题讨论】: