Skip to content

Instantly share code, notes, and snippets.

@daveols
Last active February 29, 2024 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save daveols/8932266088be767105b92a9c54cf0f9e to your computer and use it in GitHub Desktop.
Save daveols/8932266088be767105b92a9c54cf0f9e to your computer and use it in GitHub Desktop.
Scrollable tabs within DraggableScrollableSheet
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: DraggableScrollableSheet(
initialChildSize: 0.6,
maxChildSize: 0.9,
minChildSize: 0.6,
builder: (context, scrollController) => DefaultTabController(
length: 2,
child: _buildNestedScrollView(scrollController),
// child: _buildFlatCustomScrollView(scrollController),
// child: _buildTabsCustomScrollView(scrollController),
),
),
);
}
NestedScrollView _buildNestedScrollView(ScrollController scrollController) {
return NestedScrollView(
// controller: scrollController,
headerSliverBuilder: (context, innerBoxIsScrolled) {
print('innerBoxIsScrolled $innerBoxIsScrolled');
return [
SliverOverlapAbsorber(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
toolbarHeight: 0.0,
pinned: true,
primary: false,
forceElevated: innerBoxIsScrolled,
bottom: TabBar(
tabs: [
Tab(text: 'Foo'),
Tab(text: 'Bar'),
],
),
),
),
];
},
body: TabBarView(
children: [
ListView.builder(
controller: scrollController,
physics: ClampingScrollPhysics(),
padding: EdgeInsets.zero,
itemBuilder: (context, index) => Container(
color: Colors.primaries[index % 10],
height: 150.0,
),
),
ListView.builder(
// controller: scrollController,
physics: ClampingScrollPhysics(),
padding: EdgeInsets.zero,
itemBuilder: (context, index) => Container(
color: Colors.primaries[index % 10],
height: 25.0,
),
),
],
),
);
}
CustomScrollView _buildTabsCustomScrollView(
ScrollController scrollController) {
return CustomScrollView(
// controller: scrollController,
physics: ClampingScrollPhysics(),
slivers: [
SliverAppBar(
toolbarHeight: 0.0,
pinned: true,
primary: false,
bottom: TabBar(
tabs: [
Tab(text: 'Foo'),
Tab(text: 'Bar'),
],
),
),
SliverFillRemaining(
child: TabBarView(
children: [
ListView.builder(
controller: scrollController,
physics: ClampingScrollPhysics(),
padding: EdgeInsets.zero,
itemBuilder: (context, index) => Container(
color: Colors.primaries[index % 10],
height: 150.0,
),
),
ListView.builder(
controller: scrollController,
physics: ClampingScrollPhysics(),
padding: EdgeInsets.zero,
itemBuilder: (context, index) => Container(
color: Colors.primaries[index % 10],
height: 25.0,
),
),
],
),
)
],
);
}
CustomScrollView _buildFlatCustomScrollView(
ScrollController scrollController) {
return CustomScrollView(
controller: scrollController,
physics: ClampingScrollPhysics(),
slivers: [
SliverAppBar(
toolbarHeight: 0.0,
pinned: true,
primary: false,
bottom: TabBar(
tabs: [
Tab(text: 'Foo'),
Tab(text: 'Bar'),
],
),
),
SliverList(
delegate: SliverChildBuilderDelegate(
(context, index) {
return Container(
color: Colors.primaries[index % 10],
height: 150.0,
);
},
),
)
],
);
}
}
@Matix-Media
Copy link

Hi, first of all, thank you for your work @daveols . I have one question though. What are the other functions used for? Why are they not used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment