【问题标题】:Mouse scroll not work on SliverAppbar() or SliverAppbar() floating not working Flutter Web鼠标滚动在 SliverAppbar() 或 SliverAppbar() 上不起作用 Flutter Web 浮动不起作用
【发布时间】:2020-07-31 14:36:30
【问题描述】:

当我在 CustomScrollView() 中使用 SliverAppBar() 然后 SliverAppBar() floating:true鼠标滚动不起作用

CustomScrollView(

slivers: <Widget>[

  SliverAppBar(
    floating:true,
    expandedHeight: 150.0,
    flexibleSpace: const FlexibleSpaceBar(
      title: Text('Available seats'),
    ),
    actions: <Widget>[
       IconButton(
          icon: const Icon(Icons.add_circle),
          tooltip: 'Add new entry',
          onPressed: () { /* ... */ },
       ),
   ]
  ),
  
  SliverList(
    delegate: SliverChildBuilderDelegate((context, index) {
      return ListTile(
        title: Text('List Tile $index'),
      );
    }, childCount: 100),
  ),
],

),

【问题讨论】:

    标签: android flutter dart sliverappbar customscrollview


    【解决方案1】:

    我已经解决了问题

    创建一个类并根据需要为其命名。

    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;
      }
    }
    

    现在使用你的类作为 SliverAppBar()

    CustomScrollView(
            slivers: <Widget>[
    //           SliverAppBar(
    //             floating: true,
    //             snap: true,
    //             titleSpacing: 0.0,
    //             title: Container(
    //               color: Colors.blue,
    //             ),
    //             elevation: 0,
    //             backgroundColor: Theme.of(context).scaffoldBackgroundColor,
    //           ),
              SliverPersistentHeader(
                floating: true,
                delegate: SliverAppBarDelegate(
                  minHeight: 60,
                  maxHeight: 60,
                  child: Container(
                    color: Colors.red,
                    child: Center(
                      child: Text(
                          'I want this to appear only after some scroll offset occured'),
                    ),
                  ),
                ),
              ),
              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('Some large text',
                              style: TextStyle(fontSize: 20)),
                        ),
                        Divider(),
                      ],
                    ),
                  ),
                ),
              ),
              SliverList(
                delegate: SliverChildBuilderDelegate((context, index) {
                  return ListTile(
                    title: Text('List Tile $index'),
                  );
                }, childCount: 100),
              ),
            ],
          ),
    

    或者您可以查看DartPad 示例

    【讨论】:

      猜你喜欢
      • 2020-11-15
      • 2020-02-17
      • 2020-10-07
      • 2022-01-18
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多