Mag*_*ian 4 keyboard flutter statefulwidget
在我的配置文件页面中,当使用单击编辑图标时,我具有使用布尔条件的文本选项,将文本窗口小部件更改为文本
widget.isUpdate ? new Flexible(child: new TextField()) : Text("Text Widget")
字段窗口小部件,但当文本字段聚焦时,键盘已打开,此时StatefulWidget重新创建,因此布尔值再次变为false,然后文本字段移至文本窗口小部件。仅当页面具有导航器推入页面(第二页)时,才会发生这种情况
Navigator.push(context,MaterialPageRoute(builder: (context) => UpdateProfile()))
Run Code Online (Sandbox Code Playgroud)
如果此页面为默认主页,则工作正常。我没有犯什么错误。
代码:
import 'package:flutter/material.dart';
class UpdateProfile extends StatefulWidget {
bool isUpdate = false;
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return UpdateProfileState();
}
}
class UpdateProfileState extends State<UpdateProfile> {
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Update"),
elevation: 2.0),
body: Container(
width: double.infinity,
height: double.infinity,
color: Colors.red,
margin: const EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
widget.isUpdate ? new Flexible(child: new TextField()) : Text("Text Widget"),
GestureDetector(
child: IconTheme(
data: IconThemeData(color: Color(0xFFffffff)),
child: Icon(Icons.edit)),
onTap: () {
setState(() {
widget.isUpdate = !widget.isUpdate;
});
},
)
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
如果我设置为主页,则如下所示正常工作
import 'package:expense_manager_app/page/splash_page.dart';
import 'package:expense_manager_app/page/update_profile.dart';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
brightness: Brightness.dark,
primaryColor: Colors.red[500],
accentColor: Colors.green[900],
),
home: UpdateProfile(),
);
}
}
Run Code Online (Sandbox Code Playgroud)
您应该将变量移到isUpdate
州内,记住小部件是不可变的。
class UpdateProfileState extends State<UpdateProfile> {
bool isUpdate = false;
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Update"),
elevation: 2.0),
body: Container(
width: double.infinity,
height: double.infinity,
color: Colors.red,
margin: const EdgeInsets.all(10.0),
child: Row(
children: <Widget>[
isUpdate ? new Flexible(child: new TextField()) : Text("Text Widget"),
GestureDetector(
child: IconTheme(
data: IconThemeData(color: Color(0xFFffffff)),
child: Icon(Icons.edit)),
onTap: () {
setState(() {
isUpdate = !isUpdate;
});
},
)
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
并更改此:
Navigator.push(context,MaterialPageRoute(builder: (context) => UpdateProfile()))
Run Code Online (Sandbox Code Playgroud)
对此:
final page = UpdateProfile();
Navigator.push(context,MaterialPageRoute(builder: (context) => page ))
Run Code Online (Sandbox Code Playgroud)