我正在写一个有颤动的应用程序,有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, ), ); }}
enter image description here
解决方法:
您需要在有状态窗口小部件上使用automaticKeepAliveClIEntMixin并实现名为wantKeepAlive的覆盖方法
class MyApp extends StatefulWidget {@overrIDecreateState() => new MyAppState();}
将automaticKeepAliveClIEntMixin与您的类扩展状态和ov一起使用
class MyAppState extends State<MyApp> with automaticKeepAliveClIEntMixin<MyApp>{//your existing code.....@overrIDebool get wantKeepAlive => true; //by default it will be null, change it to true.//your existing code......}
在将wantKeepAlive设置为true时,initState方法将在创建时仅运行一次.
总结以上是内存溢出为你收集整理的android – Flutter:刷卡后默认标签栏控制器不保持状态全部内容,希望文章能够帮你解决android – Flutter:刷卡后默认标签栏控制器不保持状态所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)