【问题标题】:How to add a fixed bar between a sliver bar and the scrolling content如何在条子条和滚动内容之间添加固定条
【发布时间】:2020-08-11 19:29:59
【问题描述】:

我在我的 Flutter 移动应用中有一个视图:

  1. 顶栏是一个条形栏(标题+标签)[图片上的红色框]
  2. 可滚动的数据列表 [图片上的绿色框]

当我滚动时,标签消失,只留下页面标题。

目标:

我想添加一个标题 [图片上的蓝色框],它在我滚动时不会消失,并且停留在条子栏和可滚动项之间。

我似乎只能将标题放在选项卡上方的条子栏的标题内,或者放在可滚动列表中,在这种情况下它会随着我滚动而消失

【问题讨论】:

  • 你也可以使用SliverToBoxAdapter小部件

标签: flutter


【解决方案1】:

最终使用了一个粘性标题:https://pub.dev/packages/flutter_sticky_header 并保持相同的标题而不通过滚动更改它

【讨论】:

    【解决方案2】:

    您可以轻松地自行实现。您需要的小部件称为SliverPersistentHeader,pinned 参数等于true 和SliverPersistentHeaderDelegate 的自定义实现。

    看看这个例子:

    https://dartpad.dartlang.org/6e0f6b4a212e572c7b74e5f301abc71b

    import 'dart:math' as math;
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: CustomScrollView(
            slivers: <Widget>[
              SliverPersistentHeader(
                floating: true,
                delegate: SliverAppBarDelegate(
                  minHeight: 60,
                  maxHeight: 60,
                  child: Container(
                    color: Colors.red,
                    child: Center(
                      child: Text(
                          'This will hide'),
                    ),
                  ),
                ),
              ),
              SliverPersistentHeader(
                pinned: true,
                delegate: SliverAppBarDelegate(
                  minHeight: 60.0,
                  maxHeight: 60.0,
                  child: Container(
                    color: Theme.of(context).scaffoldBackgroundColor,
                    child: Column(
                      children: <Widget>[
                        Padding(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 16.0,
                            vertical: 8.0,
                          ),
                          child: Text('This will remain visible',
                              style: TextStyle(fontSize: 20)),
                        ),
                        Divider(),
                      ],
                    ),
                  ),
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate((context, index) {
                  return ListTile(
                    title: Text('List Tile $index'),
                  );
                }, childCount: 100),
              ),
            ],
          ),
        );
      }
    }
    
    class SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
      SliverAppBarDelegate({
        @required this.minHeight,
        @required this.maxHeight,
        @required this.child,
      });
      final double minHeight;
      final double maxHeight;
      final Widget child;
    
      @override
      double get minExtent => minHeight;
      @override
      double get maxExtent => math.max(maxHeight, minHeight);
      @override
      Widget build(
          BuildContext context, double shrinkOffset, bool overlapsContent) {
        return SizedBox.expand(child: child);
      }
    
      @override
      bool shouldRebuild(SliverAppBarDelegate oldDelegate) {
        return maxHeight != oldDelegate.maxHeight ||
            minHeight != oldDelegate.minHeight ||
            child != oldDelegate.child;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多