AppBar属性概要

  • Material Design风格的顶部导航栏。

    1. AppBar({
    2. // 键
    3. Key key,
    4. // 左侧按钮(默认为返回按钮、左侧抽屉按钮)
    5. this.leading,
    6. // 与leading、actions相关,用于自动推断leading的值(如返回按钮,打开draw的按钮等,或空缺)
    7. this.automaticallyImplyLeading = true,
    8. // 顶部导航栏标题
    9. this.title,
    10. // 顶部导航栏右侧菜单按钮
    11. this.actions,
    12. // 灵活空间
    13. this.flexibleSpace,
    14. // toolbar下面固定的空间(常用于设置TabBar)
    15. this.bottom,
    16. // AppBar的海拔高度和阴影有关,值为0时无投影,值越大投影范围越大且颜色越浅
    17. this.elevation,
    18. // AppBar的形状
    19. this.shape,
    20. // 整个AppBar的背景颜色
    21. this.backgroundColor,
    22. // 设置状态栏图标为暗色还是亮色
    23. this.brightness,
    24. // 设置Icons的样式(透明度、颜色及大小)
    25. this.iconTheme,
    26. // 顶部右侧按钮主题(颜色、透明度、大小)
    27. this.actionsIconTheme,
    28. // 设置标题字体样式
    29. this.textTheme,
    30. // 设置是否有状态栏高度
    31. this.primary = true,
    32. // 设置标题是否居中
    33. this.centerTitle,
    34. // 设置在没有leading和actions时是否留出空间,入围0.0则全部为title的空间
    35. this.titleSpacing = NavigationToolbar.kMiddleSpacing,
    36. // 设置AppBar的透明度
    37. this.toolbarOpacity = 1.0,
    38. // 设置bottom的透明度
    39. this.bottomOpacity = 1.0,
    40. })
  • 下图为AppBar的样图

image.png

TabBar(bottom属性)使用示例

  1. class MyTabbedPage extends StatefulWidget {
  2. const MyTabbedPage({ Key key }) : super(key: key);
  3. @override
  4. _MyTabbedPageState createState() => _MyTabbedPageState();
  5. }
  6. // 混合 SingleTickerProviderStateMixin 类的特性
  7. class _MyTabbedPageState extends State<MyTabbedPage> with SingleTickerProviderStateMixin {
  8. // TabBar的子项
  9. final List<Tab> myTabs = <Tab>[
  10. Tab(text: 'LEFT'),
  11. Tab(text: 'RIGHT'),
  12. ];
  13. // TabBar控制器
  14. TabController _tabController;
  15. @override
  16. void initState() {
  17. super.initState();
  18. // 在initState中初始化TabController
  19. _tabController = TabController(vsync: this, length: myTabs.length);
  20. }
  21. @override
  22. void dispose() {
  23. // 在dispose中解除回收TabController
  24. _tabController.dispose();
  25. super.dispose();
  26. }
  27. @override
  28. Widget build(BuildContext context) {
  29. return Scaffold(
  30. appBar: AppBar(
  31. // 在AppBar.bottom属性中使用TabBar
  32. bottom: TabBar(
  33. controller: _tabController,
  34. tabs: myTabs,
  35. ),
  36. ),
  37. // 与TabBar联动的TabBarView设置
  38. body: TabBarView(
  39. controller: _tabController,
  40. children: myTabs.map((Tab tab) {
  41. return Center(
  42. child: Text(
  43. 'This is the ${tab.text.toLowerCase()} tab',
  44. style: const TextStyle(fontSize: 36),
  45. ),
  46. );
  47. }).toList(),
  48. ),
  49. );
  50. }
  51. }