Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ambrizals/7bb1ff633e723c9d9bde02d7e62cd3b8 to your computer and use it in GitHub Desktop.
Save ambrizals/7bb1ff633e723c9d9bde02d7e62cd3b8 to your computer and use it in GitHub Desktop.
Collapsing AppBar when scrolling with SliverAppBar
import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
class MaterialsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialPageView();
}
}
class MaterialPageView extends StatefulWidget {
@override
_MaterialPageViewState createState() => _MaterialPageViewState();
}
class _MaterialPageViewState extends State<MaterialPageView> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: DefaultTabController(
length: 2,
child: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverAppBar(
floating: true,
pinned: true,
backgroundColor: Colors.grey[300],
iconTheme: IconThemeData(color: Colors.black),
actionsIconTheme: IconThemeData(color: Colors.black),
actions: [
IconButton(icon: Icon(FontAwesome5.question_circle), onPressed: null)
],
flexibleSpace: FlexibleSpaceBar(
title: SafeArea(
child: Container(
alignment: Alignment.centerLeft,
margin: EdgeInsets.only(top: 8.333),
child: Text('Material', style: TextStyle(fontSize: 14, color: Colors.black)),
),
),
),
bottom: TabBar(
labelColor: Colors.black,
tabs: [
Tab(
child: Container(
child: Text('Daftar Material'),
),
),
Tab(
child: Container(
child: Text('Daftar Supplier'),
),
)
],
),
),
];
},
body: Container(
margin: EdgeInsets.all(6),
child: TabBarView(
children: [
Text('Coba'),
Text('Supplier')
],
),
)
)
),
);
}
}
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate {
_SliverAppBarDelegate(this._tabBar);
final TabBar _tabBar;
@override
double get minExtent => _tabBar.preferredSize.height;
@override
double get maxExtent => _tabBar.preferredSize.height;
@override
Widget build(
BuildContext context, double shrinkOffset, bool overlapsContent) {
return new Container(
color: Colors.white,
child: _tabBar,
);
}
@override
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) {
return false;
}
}
@ambrizals
Copy link
Author

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