Created
October 10, 2023 18:07
-
-
Save iapicca/a9e6b2f6ba0949b6ba0fbad4df56857f to your computer and use it in GitHub Desktop.
issue 40740 workaround
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(const NestedScrollViewExampleApp()); | |
class NestedScrollViewExampleApp extends StatelessWidget { | |
const NestedScrollViewExampleApp({super.key}); | |
@override | |
Widget build(context) => const MaterialApp( | |
home: NestedScrollViewExample(), | |
); | |
} | |
class NestedScrollViewExample extends StatelessWidget { | |
final List<String> tabs; | |
final String title; | |
final double expandedHeight; | |
const NestedScrollViewExample({ | |
super.key, | |
this.tabs = const ['Tab 1', 'Tab 2'], | |
this.title = 'Books', | |
this.expandedHeight = 150, | |
}); | |
@override | |
Widget build(context) => DefaultTabController( | |
length: tabs.length, | |
child: Scaffold( | |
body: NestedScrollView( | |
headerSliverBuilder: (context, innerBoxIsScrolled) => [ | |
SliverOverlapAbsorber( | |
handle: | |
NestedScrollView.sliverOverlapAbsorberHandleFor(context), | |
sliver: SliverAppBar( | |
title: Text(title), | |
pinned: true, | |
expandedHeight: expandedHeight, | |
forceElevated: innerBoxIsScrolled, | |
bottom: TabBar( | |
tabs: [ | |
for (final tab in tabs) Tab(text: tab), | |
], | |
), | |
), | |
), | |
], | |
body: TabBarView( | |
children: [ | |
for (final name in tabs) TabBarViewWidget(name: name), | |
], | |
), | |
), | |
), | |
); | |
} | |
class TabBarViewWidget extends StatefulWidget { | |
final String name; | |
final double padding; | |
final double itemExtent; | |
final int childCount; | |
const TabBarViewWidget({ | |
super.key, | |
required this.name, | |
this.padding = 8, | |
this.itemExtent = 48, | |
this.childCount = 30, | |
}); | |
@override | |
State<TabBarViewWidget> createState() => _TabBarViewWidgetState(); | |
} | |
class _TabBarViewWidgetState extends State<TabBarViewWidget> { | |
late final ScrollController _controller; | |
@override | |
void initState() { | |
super.initState(); | |
_controller = ScrollController(); | |
} | |
@override | |
void dispose() { | |
_controller.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(context) => SafeArea( | |
top: false, | |
bottom: false, | |
child: CustomScrollView( | |
controller: _controller, | |
key: PageStorageKey<String>(widget.name), | |
slivers: <Widget>[ | |
SliverOverlapInjector( | |
handle: NestedScrollView.sliverOverlapAbsorberHandleFor( | |
context, | |
), | |
), | |
SliverPadding( | |
padding: EdgeInsets.all(widget.padding), | |
sliver: SliverFixedExtentList( | |
itemExtent: widget.itemExtent, | |
delegate: SliverChildBuilderDelegate( | |
(context, index) => ListTile( | |
title: Text('Item $index'), | |
), | |
childCount: widget.childCount, | |
), | |
), | |
), | |
], | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment