简体   繁体   中英

Mouse scroll not work on SliverAppbar() or SliverAppbar() floating not working Flutter Web

When i use SliverAppBar() in a CustomScrollView() then the SliverAppBar() floating:true is not working by the mouse scroll

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),
  ),
],

),

I have solved the problem

Create a Class and give its name as you want.

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

Now use your class in as 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),
          ),
        ],
      ),

Or you can see the DartPad example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM