我正在写一个有颤动的应用程序,有4个标签视图,有点像股票Android手机应用程序或时钟应用程序.其中一个视图散列浮动操作按钮,按下该按钮将在列表中添加一些文本.但是,当我滚动到其他视图之一并返回时,所有文本都消失了.有没有办法解决这个问题?
这是我的代码:
import 'package:Flutter/material.dart';
import 'Screens/Dashboard.dart';
import 'Screens/CreateQuestionnaire.dart';
import 'Screens/AccountScreen.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
createState() => new MyAppState();
}
class MyAppState extends State<MyApp> {
final primaryColour = const Color(0xFF5CA1CA);
final secondaryColour = const Color(0xFFC36B42);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new DefaultTabController(
length: 4,
child: new Scaffold(
appBar: new AppBar(
actions: <Widget>[
new IconButton(icon: new Icon(Icons.account_circle),
onpressed: (){
Navigator.push(context, new MaterialPageRoute(builder: (context) => new AccountScreen()));
}),
],
bottom: new TabBar(
tabs: <Widget>[
new Tab(icon: new Icon(Icons.home)),
new Tab(icon: new Icon(Icons.contacts)),
new Tab(icon: new Icon(Icons.description)),
new Tab(icon: new Icon(Icons.settings))
],
),
title: new Text("NLPro Questionnaire"),
),
body: new TabBarView(
children: <Widget>[
new Dashboard(),
new CreateQuestionnaire(),
new Text("Surveys"),
new Text("Settings")
],
),
),
),
theme:new ThemeData(
primaryColor: primaryColour,
accentColor: secondaryColour,
),
);
}
}
解决方法:
您需要在有状态窗口小部件上使用AutomaticKeepAliveClientMixin并实现名为wantKeepAlive的覆盖方法
class MyApp extends StatefulWidget {
@override
createState() => new MyAppState();
}
将AutomaticKeepAliveClientMixin与您的类扩展状态和ov一起使用
class MyAppState extends State<MyApp> with AutomaticKeepAliveClientMixin<MyApp>{
//your existing code.....
@override
bool get wantKeepAlive => true; //by default it will be null, change it to true.
//your existing code......
}
在将wantKeepAlive设置为true时,initState方法将在创建时仅运行一次.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。