[Flutter组件——Tabbar]
使用
Tabar使用,设置indicator的样式,长短,设置tab选中和未选中的样式,根据数组创建Tabbar。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| import 'package:flutter/material.dart';
class TabbarDemo extends StatefulWidget { TabbarDemo({Key? key}) : super(key: key);
_TabbarDemoState createState() => _TabbarDemoState(); }
class _TabbarDemoState extends State<TabbarDemo> with SingleTickerProviderStateMixin { var tabTextList = <String>[]; var categoryTabs = <Tab>[ Tab( text: "Home", ), ];
var tempCategoryTabs = <Tab>[];
@override void initState() { super.initState(); tabTextList = ["Home", "New", "Hottest"]; tabTextList.forEach((text) { tempCategoryTabs.add( Tab( text: text, ), ); }); setState(() { categoryTabs = tempCategoryTabs; }); }
@override void dispose() { super.dispose(); }
@override Widget build(BuildContext context) { return DefaultTabController( initialIndex: 0, length: categoryTabs.length, child: Scaffold( appBar: AppBar(title: Text("Tabbar")), body: Column( children: [ TabBar( tabs: categoryTabs, isScrollable: true, labelStyle: TextStyle( fontSize: 24.0, ), labelColor: Colors.blueAccent, unselectedLabelStyle: TextStyle(fontSize: 16.0), unselectedLabelColor: Colors.grey, indicator: UnderlineTabIndicator( borderSide: BorderSide(color: Colors.orangeAccent, width: 3.0), insets: EdgeInsets.fromLTRB(20, 0, 20, 0), ), ), Expanded( child: TabBarView( children: tabTextList.map((e) => Center(child: Text(e))).toList(), ), ) ], ), ), ); } }
|
参考