flutter TabBarView 动态添加删除页面的示例代码
在TabBarView 动态添加页面后删除其中一个页面会导致后面的页面状态错误或删除的页面不正确。出现这种问题是由于创建子页面时没有为子页面设置唯一的key导致的。下面是错误的代码:
void addNewPage() { _pageCount++; setState(() { String title = "页面$_pageCount"; PageContent page = PageContent(data: title, pageId: _pageCount,); PageData data = PageData(data: title, pageId: _pageCount, content: page); listPages.add(data); nowIndex = listPages.length -1; resetTabController(); }); }
如上面的代码所示, 在创建PageContent 组件时如果没有指定全局唯一的key, 关闭页面时就会导致后面的页面被再次build或删除错误的页面,正确的代码如下
void addNewPage() { _pageCount++; setState(() { String title = "页面$_pageCount"; PageContent page = PageContent(data: title, pageId: _pageCount, key: ValueKey(title),); PageData data = PageData(data: title, pageId: _pageCount, content: page); listPages.add(data); nowIndex = listPages.length -1; resetTabController(); }); }
指定了全局唯一key后在删除子页面,后续页面就可以正常显示。
所有代码如下
import 'package:flutter/material.dart'; void main() { runApp(const MainApp()); } class MainApp extends StatelessWidget { const MainApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.white), primaryColor: Colors.white, scaffoldBackgroundColor: Colors.white, dialogBackgroundColor: Colors.white, useMaterial3: true, ), home: const PageMain(), /* home: ChangeNotifierProvider( create: (context) => HomeProvider(), builder: (context, child) => const HomePage(), ), */ ); } } class PageData { final String data; final int pageId; final Widget content; PageData({ required this.data, required this.pageId, required this.content, }); } class _StatePageMain extends State<PageMain> with TickerProviderStateMixin, AutomaticKeepAliveClientMixin { final List<PageData> listPages = <PageData>[]; int nowIndex = 0; int _pageCount = 0; TabController? tabController; @override void initState() { super.initState(); setState(() { tabController = TabController(length: listPages.length, vsync: this); }); } @override bool get wantKeepAlive => true; void addNewPage() { _pageCount++; setState(() { String title = "页面$_pageCount"; PageContent page = PageContent(data: title, pageId: _pageCount, key: ValueKey(title),); PageData data = PageData(data: title, pageId: _pageCount, content: page); listPages.add(data); nowIndex = listPages.length -1; resetTabController(); }); } //选中某个页面 void onSelectPage(PageData page) { //页面已经选中 int selIndex = 0; for (int index = 0; index < listPages.length; index++) { PageData item = listPages[index]; if (item.pageId == page.pageId) { selIndex = index; break; } } //选中页面没有更改 if (selIndex == nowIndex) { return; } setState(() { nowIndex = selIndex; tabController?.animateTo(nowIndex); }); } //关闭页面 void onClosePage(PageData data) { int closedIndex = 0; for (int index = 0; index < listPages.length; index++) { PageData item = listPages[index]; if (item.pageId == data.pageId) { closedIndex = index; break; } } setState(() { listPages.removeAt(closedIndex); if (closedIndex <= nowIndex) { nowIndex--; } if (nowIndex < 0) { nowIndex = 0; } else if (nowIndex >= listPages.length) { nowIndex = listPages.length -1; } resetTabController(); }); } void resetTabController() { if (tabController?.length != listPages.length) { tabController?.dispose(); tabController = TabController( length: listPages.length, vsync: this, initialIndex: nowIndex, ); } } @override Widget build(BuildContext context) { super.build(context); return Scaffold( appBar: AppBar( bottom: PreferredSize( preferredSize: const Size.fromHeight(40), child: TabBar( controller: tabController, tabs: listPages.map((item) => Tab(child: TitleBarItem(data: item, closeCallback: (data) => onClosePage(data)),)).toList(), ), ), ), body: TabBarView( controller: tabController, children: listPages.map((item) => item.content).toList(), ), floatingActionButton: FloatingActionButton( onPressed: () => addNewPage(), tooltip: 'Increment', child: const Icon(Icons.add), ), ); } } class PageMain extends StatefulWidget { const PageMain({super.key}); @override State<PageMain> createState() => _StatePageMain(); } class _StatePageContent extends State<PageContent> with AutomaticKeepAliveClientMixin { List<String> listItems = <String>[]; @override void initState() { print("初始化页面内容控制器:${widget.data}"); setState(() { for (int index = 0; index <= 30; index++) { listItems.add("${widget.data} - $index"); } }); super.initState(); } @override void dispose() { print("释放页面内容控制器:${widget.data}"); super.dispose(); } @override bool get wantKeepAlive => true; @override Widget build(BuildContext context) { print("Build页面 ${widget.data}"); return Container( alignment: Alignment.center, child: Column( children: [ Text(widget.data), Expanded( child: ListView.builder( itemExtent: 30, itemCount: listItems.length, itemBuilder: (context, index) { return Text(listItems[index]); } ), ), ], ), ); } } class PageContent extends StatefulWidget { final int pageId; final String data; const PageContent({super.key, required this.data, required this.pageId}); @override State<PageContent> createState() { return _StatePageContent(); } } typedef ClickCallback = void Function(PageData data); class TitleBarItem extends StatelessWidget { final PageData data; final ClickCallback closeCallback; const TitleBarItem({ super.key, required this.data, required this.closeCallback, }); @override Widget build(BuildContext context) { return SizedBox( width: 200, child: Row( children: [ Expanded(child: Text(data.data)), IconButton( onPressed: () => closeCallback(data), icon: const Icon(Icons.close)) ], ), ); } }
到此这篇关于flutter TabBarView 动态添加删除页面的文章就介绍到这了,更多相关flutter TabBarView 删除页面内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Springboot之@Controller注解不生效问题及解决
这篇文章主要介绍了Springboot之@Controller注解不生效问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-10-10微信开发准备第二步 springmvc mybatis项目结构搭建
这篇文章主要为大家详细介绍了微信开发准备第二步,springmvc和mybatis项目结构的搭建,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-04-04最新hadoop安装教程及hadoop的命令使用(亲测可用)
这篇文章主要介绍了最新hadoop安装教程(亲测可用),本文主要讲解了如何安装hadoop、使用hadoop的命令及遇到的问题解决,需要的朋友可以参考下2022-06-06
最新评论