一个应用顶部栏的通用结构,可在指定的部位放置相应的组件,常用于Scaffold组件中。

相关组件

Scaffold

AppBar基本使用

  1. <br />【leading】 : 左侧组件 【Widget】<br />【title】 : 中间组件 【Widget】<br />【actions】 : 右侧组件 【List<Widget><br />【elevation】 : 影深 【double】<br />【shape】 : 形状 【ShapeBorder】<br />【backgroundColor】 : 影深 【背景色】<br />【centerTitle】 : 中间是否居中 【bool】<br />![97.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589446797731-f90b3bf3-3b33-4cd3-a318-c7c4e10d1b37.gif#align=left&display=inline&height=310&margin=%5Bobject%20Object%5D&name=97.gif&originHeight=310&originWidth=404&size=566214&status=done&style=none&width=404)
import 'package:flutter/material.dart';
import '../PopupMenuButton/node1_base.dart';
class CustomAppBar extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AppBar(
      title: Text('风雅六社'),
      leading: BackButton(),
      backgroundColor: Colors.amber[500],
      elevation: 2,
      centerTitle: true,
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
        topLeft: Radius.circular(20),
        bottomRight: Radius.circular(20),
        topRight: Radius.circular(5),
        bottomLeft: Radius.circular(5),
      )),
      actions: <Widget>[
        IconButton(
            icon: Icon(Icons.star),
            tooltip: 'like',
            onPressed: () {
              // do nothing
            }),
        CustomPopupMenuButton()
      ],
    );
  }
}

AppBar与TabBar、TabBarView联用

   <br />【bottom】 : 底部组件   【PreferredSizeWidget】<br />![98.gif](https://cdn.nlark.com/yuque/0/2020/gif/326147/1589446852781-277829f5-bfa1-478a-b929-2ff972cf799f.gif#align=left&display=inline&height=490&margin=%5Bobject%20Object%5D&name=98.gif&originHeight=490&originWidth=404&size=1180579&status=done&style=none&width=404)
import 'package:flutter/material.dart';
import '../PopupMenuButton/node1_base.dart';
class TabAppBar extends StatefulWidget {
  @override
  _TabAppBarState createState() => _TabAppBarState();
}

class _TabAppBarState extends State<TabAppBar>
    with SingleTickerProviderStateMixin {
  final tabs = ['风画庭', '雨韵舍', '雷鸣殿', '电疾堂', '霜寒阁', '雪月楼'];
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(vsync: this, length: tabs.length);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          height: 180,
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(
                    "assets/images/sabar.jpg",
                  ),
                  fit: BoxFit.cover)),
          child: _buildAppBar(),
        ),
        Container(
            height: 150, color: Color(0xff916BF0), child: _buildTableBarView())
      ],
    );
  }

  Widget _buildAppBar() => AppBar(
        title: Text('风雅六社'),
        elevation: 1,
        leading: BackButton(),
        backgroundColor: Colors.amber[500].withAlpha(33),
        centerTitle: true,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.only(
          topLeft: Radius.circular(20),
          topRight: Radius.circular(20),
        )),
        actions: <Widget>[
          IconButton(
              icon: Icon(Icons.star),
              tooltip: 'like',
              onPressed: () {
                // do nothing
              }),
          CustomPopupMenuButton()
        ],
        bottom: TabBar(
          isScrollable: true,
          controller: _tabController,
          indicatorColor: Colors.orangeAccent,
          tabs: tabs.map((e) => Tab(text: e)).toList(),
        ),
      );

  Widget _buildTableBarView() => TabBarView(
      controller: _tabController,
      children: tabs
          .map((e) => Center(
                  child: Text(
                e,
                style: TextStyle(color: Colors.white, fontSize: 20),
              )))
          .toList());
}