鼠标滚轮在Flutter Web中无法使用SliverAppbar()或SliverAppbar()浮动不起作用

3

当我在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),
  ),
],

),

1个回答

0

我已经解决了这个问题

创建一个类,并根据你的想法给它命名。

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的示例

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接