大佬教程收集整理的这篇文章主要介绍了tabs – Flutter – 使用具有动态Tab视图的TabBarView小部件实现导航抽屉,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有Drawer小部件的Flutter应用程序.我正在使用这个小部件来制作一个导航抽屉,抽屉有一个ListView作为子节点,ListView包含用户可以选择的视图选项(A,B和C).
由于应用程序的主页面(MyHomePagE)从StatefulWidget扩展到我加载新视图的唯一方法是调用setState方法来分配我的“控制变量”(viewName)的新值,然后我期待Flutter执行MyHomePage的Widget构建(BuildContext context)方法,但这次使用viewName的新值.
所有上述工作都按预期工作,问题是在Scaffold的body字段中我有一个TabBarView小部件,因为我想向用户显示每个视图有两个选项卡(Tab 1和Tab 2)的视图(A,B和C).
TabBarView子项是:
– Tab 1的StatefulTab对象
– Tab 2的简单中心小部件
我想在这里演示的是,当你点击抽屉的一个选项(例如加载B视图)时,Tab 2会改变为预期值,但是Tab 1(包含有状态小部件)不会改变它的值.你点击抽屉的任何其他选项
注意:我必须为3个视图使用相同的StatefulTab小部件才能重用代码,因为3个视图的唯一值是viewName变量.
这是代码:
@H_871_5@main.dart
import 'package:Flutter/material.dart'; import 'package:Flutter_test/StatefulTab.dart'; void main() { runApp(new MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( title: 'Flutter Demo',theme: new ThemeData( priMarySwatch: Colors.blue,),home: new MyHomePage(viewName: 'A'),); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key,this.viewNamE}) : super(key: key); final String viewName; @override _MyHomePageState createState() => new _MyHomePageState(viewName: viewName); } class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStatemixin { String viewName; _MyHomePageState({Key key,this.viewNamE}); TabController controller; @override void initState() { super.initState(); controller = new TabController( length: 2,vsync: this,); } @override void dispose() { controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { var tabs = <Tab>[ new Tab(icon: new Icon(Icons.homE),text: 'Tab 1'),new Tab(icon: new Icon(Icons.account_@L_772_26@),text: 'Tab 2') ]; return new Scaffold( appBar: new AppBar( title: new Text(viewName),body: new TabBarView(controller: controller,children: <Widget>[ new StatefulTab(viewName: viewName),new Center(child: new Text('This is the Tab 2 for view $viewName')) ]),bottomNavigationBar: new Material( color: Colors.blue,child: new TabBar(controller: controller,tabs: tabs),drawer: new Drawer( child: new ListView( children: <Widget>[ new Padding( padding: new EdgeInsets.only(top: 50.0),new ClipRect( child: new column( children: <Widget>[ new ListTile( title: new Text('Tap to load view: A'),onTap: () => _loadView('A',context),new ListTile( title: new Text('Tap to load view: B'),onTap: () => _loadView('B',new ListTile( title: new Text('Tap to load view: C'),onTap: () => _loadView('C',],); } _loadView(String view,BuildContext context) { Navigator.of(context).pop(); setState(() { if (view == 'A') { viewName = 'A'; } else if (view == 'B') { viewName = 'B'; } else if (view == 'C') { viewName = 'C'; } }); } }
StatefulTab.dart
import 'package:Flutter/material.dart'; class StatefulTab extends StatefulWidget { String viewName; StatefulTab({Key key,this.viewNamE}) : super(key: key); @override StatefulTabState createState() => new StatefulTabState(viewName: viewName); } class StatefulTabState extends State<StatefulTab> { String viewName; StatefulTabState({Key key,this.viewNamE}); @override Widget build(BuildContext context) { return new Center( child: new Text('This is the Tab 1 for View $viewName'),); } }
我如何告诉Flutter为Tab 1的有状态wdiget获取新值?
有没有更好的方法来实现具有动态视图的导航抽屉?
提前致谢!
在各个地方进一步调查和各种打印语句使我得出结论,在单击其中一个抽屉按钮后不调用StatefulTabState的构造函数.以下是StatefulTab的一个工作示例:
class StatefulTab extends StatefulWidget { String viewName; StatefulTab({Key key,this.viewNamE}) : super(key: key); @override StatefulTabState createState() => new StatefulTabState(); } class StatefulTabState extends State<StatefulTab> { @override Widget build(BuildContext context) { return new Center( child: new Text('This is the Tab 1 for View ${widget.viewNamE}'),); } }
如果您有任何疑问,请随时发表评论.看看这个tutorial/documentation可能是有益的.
以上是大佬教程为你收集整理的tabs – Flutter – 使用具有动态Tab视图的TabBarView小部件实现导航抽屉全部内容,希望文章能够帮你解决tabs – Flutter – 使用具有动态Tab视图的TabBarView小部件实现导航抽屉所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。