在列表顶部放置一个浮动的 app bar

640?wx_fmt=jpeg

为了方便用户查看列表,你可能希望在用户向下滚动列表时隐藏 app bar,尤其在你的 app bar 特别高,导致它占据了很多竖向空间的时候。

一般情况下,你可以通过给 Scaffold 组件设置一个 appBar 属性来创建一个 app bar。这个 app bar 会始终固定在 Scaffold 组件的 body 上方。

把 app bar 从 Scaffold 组件挪到一个 CustomScrollView 里,可以让你创建一个随着你滑动 CustomScrollView 里列表的同时在屏幕外自动随之滚动的 app bar。

640?wx_fmt=gif

下面这篇教程将介绍如何通过 CustomScrollView 来生成一个带有随着用户滑动列表同时会在屏幕外随之滚动的 app bar 的列表。

步骤

1.  创建一个  CustomScrollView
2. 通过  SliverAppBar  来添加一个浮动的 app bar
3. 通过  SliverList  来添加列表

1. 创建一个 CustomScrollView

要创建一个浮动的 app bar,你需要将 app bar 放在一个包含列表的 CustomScrollView 里,这会同步 app bar 和列表的滚动位置。你可以把 CustomScrollView 组件当成一个可以让你把不同类型的可滚动列表和组件混合匹配在一起的 ListView

可以放在 CustomScrollView 里的可滚动列表和组件我们称之为 slivers。有几种类型的 slivers,比如 SliverListSliverGridList 和 SliverAppBar!实际上,ListView 和 GridView 组件底层使用的就是 SliverList 和 SliverGrid

以下例子演示了创建一个包含 SliverAppBar 和 SliverList 的 CustomScrollView。另外你需要删除你之前可能设置在 Scaffold组件上的 app bar!

Scaffold(
  // No appBar property provided, only the body.
  body: CustomScrollView(
    // Add the app bar and list of items as slivers in the next steps.
    slivers: <Widget>[]
  ),
);

2. 使用 SliverAppBar 来添加一个浮动的 app bar

接下来为 CustomScrollView 添加一个 app bar。Flutter 提供开箱即用的 SliverAppBar 组件,与普通的 AppBar 组件非常相似,你可以使用 SliverAppBar 来显示标题、标签、图像等内容。

同时,SliverAppBar 组件也提供一种创建 “浮动” app bar 的能力,当用户向下滚动列表时,app bar 会随之在屏幕外滚动。此外,你可以配置 SliverAppBar 在用户滚动时缩小或展开。

要达到这个效果:

1. 先创建一个只显示标题的 app bar

2. 将 floating 属性设为 true。这使用户在向上滚动列表时能快速显示 app bar。

3. 添加一个 flexibleSpace 组件,这个组件将填充可用的 expandedHeight

CustomScrollView(
  slivers: <Widget>[
    SliverAppBar(
      title: Text('Floating app bar'),
      // Allows the user to reveal the app bar if they begin scrolling back
      // up the list of items.
      floating: true,
      // Display a placeholder widget to visualize the shrinking size.
      flexibleSpace: Placeholder(),
      // Make the initial height of the SliverAppBar larger than normal.
      expandedHeight: 200,
    ),
  ],
);

 小提示

试试 SliverAppBar 支持的各种属性,并使用热重载来查看结果。例如,你可以给 flexibleSpace 提供一个 Image widget 来创建一个背景图像,当它在屏幕外滚动时会缩小尺寸。

3. 使用 SliverList 来添加一个列表

现在你已经创建好一个 app bar,接下来应该给 CustomScrollView 添加一个列表。你有两种选择:用 SliverList 或用 SliverGrid。如果你需要一个一个往下排地显示列表中的内容,应该用 SliverList 组件。如果需要网格状地显示列表中的内容,应该用 SliverGrid 组件。

SliverList 和 SliverGrid 组件都需要一个必要参数:SliverChildDelegate。虽然听起来很花哨,但它只是用来给列表组件 SliverList 或 SliverGrid 提供一个代理。例如,SliverChildBuilderDelegate 允许你创建一组可以在滚动时懒加载的列表项,就和 ListView.builder 组件差不多。

// Create a SliverList.
SliverList(
  // Use a delegate to build items as they're scrolled on screen. 
  delegate: SliverChildBuilderDelegate(
    // The builder function returns a ListTile with a title that
    // displays the index of the current item.
    (context, index) => ListTile(title: Text('Item #$index')),
    // 展示 1000 个列表项(Builds 1000 ListTiles)
    childCount: 1000,
  ),
)

完整样例

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final title = 'Floating App Bar';

    return MaterialApp(
      title: title,
      home: Scaffold(
        // No appbar provided to the Scaffold, only a body with a
        // CustomScrollView.
        body: CustomScrollView(
          slivers: <Widget>[
            // Add the app bar to the CustomScrollView.
            SliverAppBar(
              // Provide a standard title.
              title: Text(title),
              // Allows the user to reveal the app bar if they begin scrolling
              // back up the list of items.
              floating: true,
              // Display a placeholder widget to visualize the shrinking size.
              flexibleSpace: Placeholder(),
              // Make the initial height of the SliverAppBar larger than normal.
              expandedHeight: 200,
            ),
            // Next, create a SliverList
            SliverList(
              // Use a delegate to build items as they're scrolled on screen.
              delegate: SliverChildBuilderDelegate(
                // The builder function returns a ListTile with a title that
                // displays the index of the current item.
                (context, index) => ListTile(title: Text('Item #$index')),
                // Builds 1000 ListTiles
                childCount: 1000,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
640?wx_fmt=png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值