【问题标题】:[Flutter]How to keep the flexibleSpace of the SliverAppBar expanded when scrolling down in the NestedScrollView?[Flutter]SliverAppBar在NestedScrollView中向下滚动时如何保持flexibleSpace展开?
【发布时间】:2019-11-29 20:31:44
【问题描述】:

如何在 NestedScrollView 中向下滚动时保持 SliverAppBar 展开?

我想更改名为“_locked”的标志来锁定 SliverAppBar 的高度,但我不知道该怎么做。

可能需要使用 SliverPersistentHeader 小部件?

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final double expanded = 200;
  bool _locked = false;

  @override
  Widget build(BuildContext context) {
    final top = MediaQuery.of(context).padding.top;
    return Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return [
            SliverAppBar(
              pinned: true,
              floating: true,
              title: Text(widget.title),
              expandedHeight: expanded - top,
              flexibleSpace: LayoutBuilder(
                builder: (BuildContext context, BoxConstraints constraints) {
                  return Container(height: expanded, color: Colors.red);
                },
              ),
            ),
          ];
        },
        body: ListView(
          children: ListTile.divideTiles(
              context: context,
              tiles: List.generate(
                  50,
                  (index) => ListTile(
                        title: Text('Index: $index'),
                      ))).toList(),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(_locked ? Icons.lock_open : Icons.lock_outline),
        onPressed: () {
          /// Switch the flag
          setState(() {
            _locked = !_locked;
          });
        },
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我相信您想将_locked 绑定到SliverAppBarpinned 属性。

    我链接的文档中包含一个 gif 示例!

    【讨论】:

    • 'keep expand'是指CustomScrollView向下滚动时始终保持SliverAppBar的flexibleSpace扩展,而不只是让appbar固定在顶部位置。
    猜你喜欢
    • 1970-01-01
    • 2019-08-06
    • 2021-02-05
    • 2019-05-26
    • 2021-03-21
    • 2022-10-15
    • 1970-01-01
    • 2021-03-13
    • 1970-01-01
    相关资源
    最近更新 更多