【问题标题】:How can i animate a SliverAppBar to hide it on scrolling a TabBarView我如何为 SliverAppBar 设置动画以在滚动 TabBarView 时隐藏它
【发布时间】:2021-10-13 05:26:44
【问题描述】:

我想为在 NestedScrollView 中定义的 SliverAppBar 设置动画。 我希望在滚动到相机选项卡时拥有与 Whatsapp 相同的动画,但禁止使用动画小部件作为 Slivers 父级。我怎样才能做到这一点? here 是一种昂贵的方式,但它对性能不友好

【问题讨论】:

    标签: flutter flutter-animation flutter-sliver


    【解决方案1】:

    您可以像这样设置应用栏以在用户滚动视图时隐藏 AppBar

    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        const 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: [
                // Add the app bar to the CustomScrollView.
                const 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,
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 这不是我真正想要实现的。但无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 2019-08-06
    • 2021-03-13
    • 1970-01-01
    • 2021-06-23
    • 2014-02-02
    • 2015-08-22
    • 2012-09-03
    相关资源
    最近更新 更多