Created
November 10, 2021 04:09
-
-
Save buddypia/707900dc8ed801242194834f0ed713fb to your computer and use it in GitHub Desktop.
SliverAppBarのVariation1
This file contains 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
// https://stackoverflow.com/questions/50909791/flutter-change-text-when-flexiblespacebar-is-collapsed/63407889#63407889 | |
import 'dart:math' as math; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: Scaffold( | |
body: SafeArea( | |
child: MyHomePage(), | |
), | |
), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
ScrollController controller = ScrollController(); | |
@override | |
Widget build(BuildContext context) { | |
return CustomScrollView( | |
physics: ClampingScrollPhysics(), | |
controller: controller, | |
slivers: [ | |
SliverAppBar( | |
expandedHeight: 220.0, | |
floating: true, | |
pinned: true, | |
elevation: 50, | |
backgroundColor: Colors.pink, | |
leading: IconButton( | |
icon: Icon(Icons.menu), | |
onPressed: () {}, | |
), | |
flexibleSpace: _MyAppSpace(), | |
), | |
SliverList( | |
delegate: SliverChildListDelegate( | |
List.generate( | |
200, | |
(index) => Card( | |
child: Padding( | |
padding: EdgeInsets.all(10), | |
child: Text('text $index'), | |
), | |
), | |
), | |
), | |
) | |
], | |
); | |
} | |
} | |
class _MyAppSpace extends StatelessWidget { | |
const _MyAppSpace({Key key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return LayoutBuilder( | |
builder: (context, c) { | |
final settings = context | |
.dependOnInheritedWidgetOfExactType<FlexibleSpaceBarSettings>(); | |
final deltaExtent = settings.maxExtent - settings.minExtent; | |
final t = | |
(1.0 - (settings.currentExtent - settings.minExtent) / deltaExtent) | |
.clamp(0.0, 1.0) as double; | |
final fadeStart = math.max(0.0, 1.0 - kToolbarHeight / deltaExtent); | |
const fadeEnd = 1.0; | |
final opacity = 1.0 - Interval(fadeStart, fadeEnd).transform(t); | |
return Stack( | |
children: [ | |
Center( | |
child: Opacity( | |
opacity: 1 - opacity, | |
child: getTitle( | |
'Collapsed Title', | |
)), | |
), | |
Opacity( | |
opacity: opacity, | |
child: Stack( | |
alignment: Alignment.bottomCenter, | |
children: [ | |
getImage(), | |
getTitle( | |
'Expended Title', | |
) | |
], | |
), | |
), | |
], | |
); | |
}, | |
); | |
} | |
Widget getImage() { | |
return Container( | |
width: double.infinity, | |
child: Image.network( | |
'https://source.unsplash.com/daily?code', | |
fit: BoxFit.cover, | |
), | |
); | |
} | |
Widget getTitle(String text) { | |
return Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
text, | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
color: Colors.white, | |
fontSize: 26.0, | |
fontWeight: FontWeight.bold, | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment