Skip to content

Instantly share code, notes, and snippets.

@mrphu3074
Created February 26, 2019 15:34
Show Gist options
  • Save mrphu3074/ee207440d3632fe71e9fbb7c1e736958 to your computer and use it in GitHub Desktop.
Save mrphu3074/ee207440d3632fe71e9fbb7c1e736958 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: CustomScrollView(
slivers: <Widget>[
SliverPersistentHeader(
pinned: true,
delegate: CollapsedAppBar(),
),
SliverFixedExtentList(
itemExtent: 48,
delegate: SliverChildBuilderDelegate((context, i) {
return ListTile(
title: Text("Item $i"),
);
}),
)
],
) // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class CollapsedAppBar extends SliverPersistentHeaderDelegate {
double _maxHeight = 200.0;
final _searchBoxTween = SizeTween(begin: Size(300, 56), end: Size(0, 56));
final _menuAlignTween = AlignmentTween(begin: Alignment.bottomLeft, end: Alignment(-0.4, -0.7));
final _menuActionSize = Tween(begin: 24.0, end: 24.0);
final _menuActionOpacityTween = Tween(begin: 1.0, end: 0.0);
final _menuActionColorTween = ColorTween(begin: Colors.indigo[900], end: Colors.white70);
@override
// TODO: implement maxExtent
double get maxExtent => _maxHeight;
@override
// TODO: implement minExtent
double get minExtent => 100.0;
@override
bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
// TODO: implement shouldRebuild
return true;
}
@override
Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
double screenWidth = MediaQuery.of(context).size.width;
double progress = shrinkOffset / _maxHeight;
final searchBoxSize = _searchBoxTween.lerp(progress);
final menuAlign = _menuAlignTween.lerp(progress);
final menuActionSize = _menuActionSize.lerp(progress);
final menuActionOpacity = _menuActionOpacityTween.lerp(progress);
final menuActionColor = _menuActionColorTween.lerp(progress);
final menuActionBoxWidth = Tween(begin: screenWidth, end: screenWidth * 0.8).lerp(progress);
return Stack(
fit: StackFit.expand,
children: <Widget>[
Container(
color: Colors.indigo,
),
Positioned(
top: 8,
left: 8,
width: searchBoxSize.width,
child: SizedBox.fromSize(
size: searchBoxSize,
child: Container(
child: TextField(
decoration: InputDecoration(
filled: true,
fillColor: Colors.indigo[800],
prefixIcon: Icon(Icons.search, color: Colors.white70)),
),
),
),
),
Align(
alignment: menuAlign,
child: Container(
width: menuActionBoxWidth,
margin: EdgeInsets.only(bottom: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Container(
padding: EdgeInsets.all(16.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
color: Colors.white.withOpacity(menuActionOpacity),
),
child: Icon(
Icons.map,
color: menuActionColor,
size: menuActionSize.toDouble(),
)),
Container(
padding: EdgeInsets.all(16.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
color: Colors.white.withOpacity(menuActionOpacity),
),
child: Icon(
Icons.play_circle_filled,
color: menuActionColor,
size: menuActionSize.toDouble(),
)),
Container(
padding: EdgeInsets.all(16.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
color: Colors.white.withOpacity(menuActionOpacity),
),
child: Icon(
Icons.business,
color: menuActionColor,
size: menuActionSize.toDouble(),
)),
Container(
padding: EdgeInsets.all(16.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.0),
color: Colors.white.withOpacity(menuActionOpacity),
),
child: Icon(
Icons.opacity,
color: menuActionColor,
size: menuActionSize.toDouble(),
)),
],
),
),
)
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment