
上QQ阅读APP看书,第一时间看更新
3.2.5 TabBar
TabBar是一排水平的标签,可以来回切换。TabBar主要属性参见表3-15。
表3-15 TabBar属性


各门功课名称导航代码如下:
import 'package:flutter/material.dart'; class TabBarDemo extends StatefulWidget { @override State<StatefulWidget> createState() => _TabBar(); } class _TabBar extends State<TabBarDemo> { final List<String> _tabValues = [ '语文', '英语', '化学', '物理', '数学', '生物', '体育', '经济', ]; TabController _controller; @override void initState() { super.initState(); _controller = TabController( length: _tabValues.length, vsync: ScrollableState(), ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('TabBar'), bottom: TabBar( tabs: _tabValues.map((f) { return Text(f); }).toList(), controller: _controller, indicatorColor: Colors.red, indicatorSize: TabBarIndicatorSize.tab, isScrollable: true, labelColor: Colors.red, unselectedLabelColor: Colors.black, indicatorWeight: 5.0, labelStyle: TextStyle(height: 2), ), ), body: TabBarView( controller: _controller, children: _tabValues.map((f) { return Center( child: Text(f), ); }).toList(), ), ); } }
运行效果如图3-19所示。

图3-19 TabBar效果