Skip to content

Instantly share code, notes, and snippets.

@normancarcamo
Last active January 26, 2021 23:14
Show Gist options
  • Save normancarcamo/031191ccd2eb2f4c2878c588db506e76 to your computer and use it in GitHub Desktop.
Save normancarcamo/031191ccd2eb2f4c2878c588db506e76 to your computer and use it in GitHub Desktop.
Example of how to compose a TabBar + TabBarView + NestedScrollView + CustomScrollView + SliverAppBar.
import 'dart:math' as math;
import 'package:flutter/material.dart';
void main() => runApp(Main());
class Main extends StatefulWidget {
@override
State<StatefulWidget> createState() => _MainState();
}
class _MainState extends State<Main> with SingleTickerProviderStateMixin {
final _tabs = <String>['Colors', 'Search', 'Settings'];
TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: _tabs.length, vsync: this);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: NestedScrollView(
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
SliverOverlapAbsorber(
handle:
NestedScrollView.sliverOverlapAbsorberHandleFor(context),
sliver: SliverAppBar(
pinned: true,
automaticallyImplyLeading: false,
primary: false,
collapsedHeight: 160.0,
expandedHeight: 260.0,
forceElevated: innerBoxIsScrolled,
flexibleSpace: Container(
alignment: Alignment.bottomCenter,
padding: EdgeInsets.only(
top: MediaQuery.of(context).viewPadding.top),
child: LayoutBuilder(
builder: (context, BoxConstraints constraints) {
final height = constraints.biggest.height.truncate();
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Center(
child: Text('Height: ${height}'),
),
),
Container(
color: Colors.red,
height: 60.0,
child: Material(
color: Colors.transparent,
child: TabBar(
controller: _tabController,
labelStyle: TextStyle(
fontWeight: FontWeight.w700,
),
indicatorWeight: 4.0,
tabs: _tabs.map((tabName) {
return Tab(
child: Text(
tabName,
style: TextStyle(
fontWeight: FontWeight.w800,
),
),
);
}).toList(),
),
),
),
],
);
},
),
),
),
),
];
},
body: TabBarView(
controller: _tabController,
children: _tabs.map((tabName) {
if (tabName == _tabs.first) {
return MyColors();
} else if (tabName == _tabs[1]) {
return Container(color: Colors.green);
} else {
return Container(color: Colors.orange);
}
}).toList(),
),
),
),
);
}
}
class MyColors extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Builder(
builder: (context) {
return CustomScrollView(
slivers: [
SliverOverlapInjector(
handle: NestedScrollView.sliverOverlapAbsorberHandleFor(
context,
),
),
SliverFixedExtentList(
itemExtent: 50.0,
delegate: SliverChildBuilderDelegate(
(context, index) {
final Color color = _randomColor();
return Container(
color: color,
height: 50.0,
child: Text('${index}. Color: $color'),
);
},
childCount: 20,
),
),
],
);
},
);
}
Color _randomColor() {
final colors = <Color>[
Color(0xFFb78443),
Color(0xffFC5457),
Color(0xFF124559),
Color(0xFFbe56ca),
Color(0xFF3e5641),
Color(0xFF4f0fb3),
Color(0xffFC5457),
Color(0xFF222f5b),
Color(0xFFacb873),
Color(0xFF2979FF),
Color(0xffFC5457),
Color(0xFF680cee),
Color(0xFF6464dd),
Color(0xFF1d4ffe),
Color(0xFFacb873),
Color(0xffFC5457),
Color(0xFF6464dd),
Color(0xFFbe56ca),
Color(0xffFC5457),
Color(0xFF6699ee),
Color(0xFF2979FF),
Color(0xffF2C573),
Color(0xffE37338),
Color(0xffFC5457),
Color(0xfffe1d4f),
Color(0xFF2979FF),
];
return colors[math.Random().nextInt(colors.length)];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment