Flutter 当文本字段对焦并出现键盘时,为什么它会将我导航回上一个屏幕?
我有一个由以下屏幕组成的简单应用程序: 着陆屏幕 登录屏幕 验证屏幕 当尝试在验证屏幕上写入内容时,当键盘出现并且文本字段获得焦点时,键盘自动关闭,它将引导我返回登录屏幕,而不会给出任何错误或bug。 我不知道为什么会这样 信息:我在物理和模拟器上运行应用程序,但两者没有任何区别,颤振版本是2.0.3,dart 2.12.2 这里是弗利特医生:Flutter 当文本字段对焦并出现键盘时,为什么它会将我导航回上一个屏幕?,flutter,mobile,flutter-layout,flutter-dependencies,flutter-test,Flutter,Mobile,Flutter Layout,Flutter Dependencies,Flutter Test,我有一个由以下屏幕组成的简单应用程序: 着陆屏幕 登录屏幕 验证屏幕 当尝试在验证屏幕上写入内容时,当键盘出现并且文本字段获得焦点时,键盘自动关闭,它将引导我返回登录屏幕,而不会给出任何错误或bug。 我不知道为什么会这样 信息:我在物理和模拟器上运行应用程序,但两者没有任何区别,颤振版本是2.0.3,dart 2.12.2 这里是弗利特医生: Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Cha
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.0.3, on Microsoft Windows [Version
10.0.18363.1440], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version
30.0.3)
[√] Chrome - develop for the web
[√] Android Studio (version 4.1.0)
[√] IntelliJ IDEA Ultimate Edition (version 2020.2)
[√] VS Code (version 1.53.2)
[√] Connected device (3 available)
以下是main的代码库:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(
MyApp(),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title:TextConstants.appName,
debugShowCheckedModeBanner: false,
home: LandingScreen(),
initialRoute: '/landing-screen',
defaultTransition: Transition.downToUp,
getPages: [
GetPage(
name: '/landing-screen',
page: () => LandingScreen(),
),
GetPage(
name: '/login-screen',
page: () => LoginScreen(),
),
GetPage(
name: '/verification-screen',
page: () => VerificationScreen(),
),
],
);
}
}
以下是着陆屏幕的代码库:
@override
void didChangeDependencies() async {
super.didChangeDependencies();
if( await NetworkingUtils.checkInternetConnection()){
// if it's connected.
Get.toNamed("/login-screen");
} else {
// if it's not connected.
Get.snackbar(
'Warning', // title
'You don\'t have internet connection',
icon: Icon(Icons.warning,color: Colors.white,),
snackPosition: SnackPosition.BOTTOM,
borderRadius: 0,
showProgressIndicator: true,
mainButton: TextButton(onPressed: (){}, child: Text('Check')),
colorText: Colors.white,
forwardAnimationCurve: Curves.bounceIn,
isDismissible: false,
reverseAnimationCurve: Curves.easeInOut,
);
}
}
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
return Scaffold(
backgroundColor: Colors.lightBlueAccent,
body: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/landing_background.png'),
fit: BoxFit.fill
)
),
child: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: AvatarGlow(
glowColor: Colors.white,
endRadius: width*0.44,
showTwoGlows: true,
repeat: true,
duration: Duration(milliseconds: 1000),
animate: true,
child: ClipRRect(
borderRadius: BorderRadius.circular(200.0),
child: Container(
height: width*0.6,
width: width*0.6,
child: Image.asset('assets/images/smart_city_logo.png'),
),
),
),
),
),
),
);
}
}
以下是登录屏幕的代码库:
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
TextEditingController _phoneNumberController = TextEditingController();
bool isLoading = false;
@override
Widget build(BuildContext context) {
double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
return Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 30),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.asset('assets/images/smart_city_logo_transparent.png',height: width/2.5,fit: BoxFit.fill,),
SizedBox(height: 15,),
Text("LOGIN TO", style: FontConstants.k24Light(fontSize: 38, textColor: Colors.black),),
Text("SMART CITY \nMANAGER", style: FontConstants.k24Bold(fontSize: 35, textColor: ColorConstant.blueLight),textAlign: TextAlign.start,),
Divider(height: 15,color: Colors.black, endIndent: width * 0.80, thickness: 3,),
Text("Smart city manager provides whole information about the flat that you are looking for.", style: FontConstants.k24Light(fontSize: 20, textColor: Colors.black ).copyWith(wordSpacing: 0.5),),
SizedBox(height: 35,),
TextField(
style: FontConstants.k24Light(fontSize: 20, textColor: Colors.black),
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(10)
),
borderSide: BorderSide(
width: 0,
style: BorderStyle.none
)
),
filled: true,
fillColor: ColorConstant.blueGreyLight,
prefixIcon: Icon(Icons.phone_android_sharp, color: Colors.grey,),
hintText: "Phone number...",
),
),
SizedBox(height: 35,),
RaisedGradientButton(
child: Text(
'NEXT',
style: FontConstants.k24Light(
fontSize: 28, textColor: Colors.white),
),
onPressed: () {
Get.toNamed('/verification-screen');
},
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0xff7CD9FF),
Color(0xff3CC6FF),
]),
)
],
),
),
),
),
);
}
}
class LoginScreen扩展StatefulWidget{
@凌驾
_LoginsScreenState createState()=>\u LoginsScreenState();
}
类_LoginScreenState扩展状态{
TextEditingController_phoneNumberController=TextEditingController();
bool isLoading=false;
@凌驾
小部件构建(构建上下文){
double height=MediaQuery.of(context).size.height;
double width=MediaQuery.of(context).size.width;
返回脚手架(
背景颜色:Colors.white,
正文:安全区(
子:SingleChildScrollView(
子:容器(
填充:边缘组。对称(水平:20,垂直:30),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
Image.asset('assets/images/smart\u city\u logo\u transparent.png',高度:width/2.5,适合度:BoxFit.fill,),
尺寸箱(高度:15,),
文本(“登录”,样式:FontConstants.k24Light(fontSize:38,textColor:Colors.black),
Text(“智能城市管理器”,样式:FontConstants.k24Bold(fontSize:35,textColor:ColorConstant.blueLight),textAlign:textAlign.start,),
分隔器(高度:15,颜色:Colors.black,末端缩进:宽度*0.80,厚度:3,),
Text(“智能城市管理器提供有关您正在寻找的公寓的完整信息。”,style:fontstants.k24Light(fontSize:20,textColor:Colors.black)。copyWith(字间距:0.5),
尺寸箱(高度:35,),
文本字段(
样式:FontConstants.k24Light(fontSize:20,textColor:Colors.black),
装饰:输入装饰(
边框:大纲输入边框(
borderRadius:borderRadius.all(
圆形半径(10)
),
边界边(
宽度:0,
样式:BorderStyle.none
)
),
是的,
fillColor:ColorConstant.blueGreyLight,
前缀:图标(Icons.phone\u android\u sharp,颜色:Colors.grey,),
hintText:“电话号码…”,
),
),
尺寸箱(高度:35,),
升起半径按钮(
子:文本(
“下一个”,
样式:FontConstants.k24Light(
字体大小:28,文本颜色:颜色。白色),
),
已按下:(){
Get.toNamed(“/验证屏幕”);
},
梯度:线性梯度(
开始:Alignment.topCenter,
结束:对齐。底部中心,
颜色:[
颜色(0xff7CD9FF),
颜色(0xff3CC6FF),
]),
)
],
),
),
),
),
);
}
}
以下是仅包含一个文本字段的最后一个代码基:
class VerificationScreen extends StatefulWidget {
@override
_VerificationScreenState createState() => _VerificationScreenState();
}
class _VerificationScreenState extends State<VerificationScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Center(
child: TextField(),
),
));
}
}
类验证屏幕扩展StatefulWidget{
@凌驾
_VerificationScreenState createState()=>\u VerificationScreenState();
}
类验证Screenstate扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
儿童:中心(
子项:TextField(),
),
));
}
}
我在登录屏幕上遇到一个问题,那就是小部件树太乱了。
因为didchangedependency方法将被多次调用,并且会对小部件树进行任何更改。
解决方案是使用didChangeWidget而不是DidChangeDependency,并将导航更改为offAndToNamed。代码太多。你们能发布一个最小的、可复制的代码吗?我放置了整个应用程序,因为它不会给我任何错误,我不知道它发生在哪里。