Last active
February 16, 2019 09:22
-
-
Save zmtzawqlp/c4cd981274723329f50c32397a024239 to your computer and use it in GitHub Desktop.
custom_scroll_app_bar
This file contains hidden or 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'; | |
import 'dart:math'; | |
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( | |
body: Stack( | |
children: <Widget>[ | |
Positioned( | |
top: 0.0, | |
right: 0.0, | |
left: 0.0, | |
bottom: 0.0, | |
child: CustomScrollView( | |
slivers: <Widget>[ | |
MySliverAppBar(), | |
SliverToBoxAdapter( | |
child: Container( | |
height: 800.0, | |
color: Colors.green, | |
child: Text("我是下面的列表"), | |
), | |
) | |
], | |
), | |
), | |
Positioned( | |
bottom: 0.0, | |
right: 0.0, | |
child: Container( | |
child: Text("我是悬浮的文字"), | |
), | |
) | |
], | |
)); | |
} | |
} | |
class MySliverAppBar extends StatefulWidget { | |
MySliverAppBar(); | |
@override | |
_MySliverAppBarState createState() => _MySliverAppBarState(); | |
} | |
class _MySliverAppBarState extends State<MySliverAppBar> { | |
@override | |
Widget build(BuildContext context) { | |
var topPadding = MediaQuery.of(context).padding.top; | |
return SliverPersistentHeader( | |
pinned: true, | |
delegate: MySliverPersistentHeaderDelegate( | |
topPadding: topPadding, expandedHeight: 400.0), | |
); | |
} | |
@override | |
void didChangeDependencies() { | |
// TODO: implement didChangeDependencies | |
super.didChangeDependencies(); | |
} | |
} | |
class MySliverPersistentHeaderDelegate extends SliverPersistentHeaderDelegate { | |
final Color backgroundColor; | |
final double expandedHeight; | |
final Widget action; | |
final Widget leading; | |
final Widget title; | |
final Widget background; | |
final double topPadding; | |
MySliverPersistentHeaderDelegate( | |
{this.backgroundColor, | |
this.expandedHeight, | |
this.action, | |
this.leading, | |
this.title, | |
this.background, | |
this.topPadding}); | |
@override | |
Widget build( | |
BuildContext context, double shrinkOffset, bool overlapsContent) { | |
final ThemeData themeData = Theme.of(context); | |
var offset = -shrinkOffset; | |
// TODO: implement build | |
List<Widget> rowChild = new List<Widget>(); | |
rowChild.add(leading ?? Container()); | |
rowChild.add(action ?? Container()); | |
var color = backgroundColor ?? themeData.primaryColor; | |
//color = color.withOpacity(shrinkOffset / expandedHeight); | |
// print((shrinkOffset + kToolbarHeight + topPadding)); | |
// print(shrinkOffset); | |
//SliverAppBar(flexibleSpace: FlexibleSpaceBar(),) | |
var child = Container( | |
//height: expandedHeight, | |
color: Colors.yellow, | |
child: Stack( | |
//fit: StackFit.expand, | |
children: <Widget>[ | |
Positioned( | |
top: kToolbarHeight + topPadding + offset * 0.8, | |
left: 0.0, | |
right: 0.0, | |
child: Container( | |
height: 200.0, | |
color: Colors.blue, | |
alignment: Alignment.bottomCenter, | |
child: Text( | |
"这是一个测试测试这是一个测试测试这是一个测试测试这是一个测试测试这是一个测试测试", | |
), | |
), | |
), | |
Positioned( | |
left: 0.0, | |
right: 0.0, | |
bottom: 0.0, | |
child: Container( | |
height: 200.0, | |
color: Colors.red, | |
alignment: Alignment.bottomCenter, | |
child: Text("我是悬浮的那个东西"), | |
), | |
), | |
Positioned( | |
top: 0.0, | |
left: 0.0, | |
right: 0.0, | |
child: Container( | |
color: Colors.deepPurpleAccent, | |
height: kToolbarHeight + topPadding, | |
child: Row( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
children: <Widget>[ | |
BackButton(), | |
Text( | |
"我是假装的Appbar", | |
textAlign: TextAlign.center, | |
) | |
], | |
)), | |
) | |
], | |
), | |
); | |
return Material( | |
child: ClipRect( | |
child: child, | |
)); | |
} | |
@override | |
// TODO: implement maxExtent | |
double get maxExtent => kToolbarHeight + topPadding + expandedHeight; | |
@override | |
// TODO: implement minExtent | |
double get minExtent => kToolbarHeight + topPadding + 100.0; | |
@override | |
bool shouldRebuild(MySliverPersistentHeaderDelegate oldDelegate) { | |
// TODO: implement shouldRebuild | |
return expandedHeight != oldDelegate.expandedHeight || | |
action != oldDelegate.action || | |
leading != oldDelegate.leading || | |
title != oldDelegate.title || | |
backgroundColor != oldDelegate.backgroundColor || | |
background != oldDelegate.background || | |
topPadding != oldDelegate.topPadding; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment