有没有办法让标签栏停留在顶部,而工具栏滚动?

时间:2017-06-09 21:17:35

标签: dart flutter

我正在尝试执行与此处材料指南中所述相同的滚动技术:“带标签的应用栏”部分中的https://material.io/guidelines/patterns/scrolling-techniques.html#scrolling-techniques-behavior

无法找到任何示例。 Flutter目前有可能吗?

1 个答案:

答案 0 :(得分:4)

您希望标签栏保持固定在顶部,同时工具栏滚动。您可以使用SliverAppBar pinned: truefloating: true来完成此操作。该示例的完整代码如下所示。

screenshot

import 'package:flutter/material.dart';

main() async {
  runApp(new MaterialApp(
    home: new MyHomePage(),
  ));
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
      length: 2,
      child: new Scaffold(
        body: new NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              new SliverAppBar(
                title: const Text('Tabs and scrolling'),
                forceElevated: innerBoxIsScrolled,
                pinned: true,
                floating: true,
                bottom: new TabBar(
                  tabs: <Tab>[
                    new Tab(text: 'green'),
                    new Tab(text: 'purple'),
                  ],
                ),
              ),
            ];
          },
          body: new TabBarView(
            children: <Widget>[
              new Center(
                child: new Container(
                  height: 1000.0,
                  color: Colors.green.shade200,
                  child: new Center(
                    child: new FlutterLogo(colors: Colors.green),
                  ),
                ),
              ),
              new Center(
                child: new Container(
                  height: 1000.0,
                  color: Colors.purple.shade200,
                  child: new Center(
                    child: new FlutterLogo(colors: Colors.purple),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}