Created
April 15, 2020 11:26
-
-
Save orestesgaolin/b115e68df5c9f1bdc2e7ed18586cc0dd to your computer and use it in GitHub Desktop.
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
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file | |
// for details. All rights reserved. Use of this source code is governed by a | |
// BSD-style license that can be found in the LICENSE file. | |
import 'dart:math' as math; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
MyHomePage({Key key, this.title}) : super(key: key); | |
final String title; | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text(widget.title), | |
), | |
body: CustomScrollView( | |
slivers: <Widget>[ | |
// SliverAppBar( | |
// floating: true, | |
// snap: true, | |
// titleSpacing: 0.0, | |
// title: Container( | |
// color: Colors.blue, | |
// ), | |
// elevation: 0, | |
// backgroundColor: Theme.of(context).scaffoldBackgroundColor, | |
// ), | |
SliverPersistentHeader( | |
floating: true, | |
delegate: SliverAppBarDelegate( | |
minHeight: 60, | |
maxHeight: 60, | |
child: Container( | |
color: Colors.red, | |
child: Center( | |
child: Text( | |
'I want this to appear only after some scroll offset occured'), | |
), | |
), | |
), | |
), | |
SliverPersistentHeader( | |
pinned: true, | |
delegate: SliverAppBarDelegate( | |
minHeight: 60.0, | |
maxHeight: 60.0, | |
child: Container( | |
color: Theme.of(context).scaffoldBackgroundColor, | |
child: Column( | |
children: <Widget>[ | |
Padding( | |
padding: const EdgeInsets.symmetric( | |
horizontal: 16.0, | |
vertical: 8.0, | |
), | |
child: Text('Some large text', | |
style: TextStyle(fontSize: 20)), | |
), | |
Divider(), | |
], | |
), | |
), | |
), | |
), | |
SliverList( | |
delegate: SliverChildBuilderDelegate((context, index) { | |
return ListTile( | |
title: Text('List Tile $index'), | |
); | |
}, childCount: 100), | |
), | |
], | |
), | |
); | |
} | |
} | |
class SliverAppBarDelegate extends SliverPersistentHeaderDelegate { | |
SliverAppBarDelegate({ | |
@required this.minHeight, | |
@required this.maxHeight, | |
@required this.child, | |
}); | |
final double minHeight; | |
final double maxHeight; | |
final Widget child; | |
@override | |
double get minExtent => minHeight; | |
@override | |
double get maxExtent => math.max(maxHeight, minHeight); | |
@override | |
Widget build( | |
BuildContext context, double shrinkOffset, bool overlapsContent) { | |
return SizedBox.expand(child: child); | |
} | |
@override | |
bool shouldRebuild(SliverAppBarDelegate oldDelegate) { | |
return maxHeight != oldDelegate.maxHeight || | |
minHeight != oldDelegate.minHeight || | |
child != oldDelegate.child; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment