how to implement a sliverAppBar with a tabBar

前端 未结 2 1555
無奈伤痛
無奈伤痛 2020-12-10 02:30

the flutter document show a demo for SliverAppBar + TabBar + TabBarView with ListView use NestedScrollView, and it\'s a b

相关标签:
2条回答
  • 2020-12-10 02:44

    Here is an example for TabView with SilverAppBar

    class SilverAppBarWithTabBarScreen extends StatefulWidget {
      @override
      _SilverAppBarWithTabBarState createState() => _SilverAppBarWithTabBarState();
    }
    
    class _SilverAppBarWithTabBarState extends State<SilverAppBarWithTabBarScreen>
        with SingleTickerProviderStateMixin {
      TabController controller;
    
      @override
      void initState() {
        super.initState();
        controller = new TabController(length: 3, vsync: this);
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new CustomScrollView(
            slivers: <Widget>[
              new SliverAppBar(
                title: Text("Silver AppBar With ToolBar"),
                pinned: true,
                expandedHeight: 160.0,
                bottom: new TabBar(
                  tabs: [
                    new Tab(text: 'Tab 1'),
                    new Tab(text: 'Tab 2'),
                    new Tab(text: 'Tab 3'),
                  ],
                  controller: controller,
                ),
              ),
              new SliverList(
              new SliverFillRemaining(
            child: TabBarView(
              controller: controller,
              children: <Widget>[
                   Text("Tab 1"),
                   Text("Tab 2"),
                   Text("Tab 3"),
                 ],
                ),
              ),
            ],
          ),
        );
      }
    }
    
    0 讨论(0)
  • 2020-12-10 02:46

    Use NestedScrollView, here is the working code.

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: DefaultTabController(
          length: 2,
          child: NestedScrollView(
            headerSliverBuilder: (context, value) {
              return [
                SliverAppBar(
                  bottom: TabBar(
                    tabs: [
                      Tab(icon: Icon(Icons.call), text: "Call"),
                      Tab(icon: Icon(Icons.message), text: "Message"),
                    ],
                  ),
                ),
              ];
            },
            body: TabBarView(
              children: [
                CallPage(),
                MessagePage(),
              ],
            ),
          ),
        ),
      );
    }
    
    0 讨论(0)
提交回复
热议问题