Last active
November 14, 2020 11:18
-
-
Save osaxma/26671eeec673a663b26c5dda68c934c2 to your computer and use it in GitHub Desktop.
Expand Shrink Cards with Scrolling in ListView
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
// answer for: | |
// https://stackoverflow.com/questions/64826393/how-can-i-implement-this-specific-scroll-animation-using-flutter/ | |
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: Center(child: Container(width: 300, child: Example())), | |
); | |
} | |
} | |
class Example extends StatefulWidget { | |
@override | |
_ExampleState createState() => _ExampleState(); | |
} | |
class _ExampleState extends State<Example> { | |
double spaceBetween = 10.0; | |
final _duration = Duration(milliseconds: 200); | |
_onStartScroll(ScrollMetrics metrics) { | |
// if you need to do something at the start | |
} | |
_onUpdateScroll(ScrollMetrics metrics) { | |
// do your magic here to change the value | |
if(spaceBetween == 30.0) return; | |
spaceBetween = 30.0; | |
setState((){}); | |
} | |
_onEndScroll(ScrollMetrics metrics) { | |
// do your magic here to return the value to normal | |
spaceBetween = 10.0; | |
setState((){}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return NotificationListener<ScrollNotification>( | |
onNotification: (scrollNotification) { | |
if (scrollNotification is ScrollStartNotification) { | |
_onStartScroll(scrollNotification.metrics); | |
} else if (scrollNotification is ScrollUpdateNotification) { | |
_onUpdateScroll(scrollNotification.metrics); | |
} else if (scrollNotification is ScrollEndNotification) { | |
_onEndScroll(scrollNotification.metrics); | |
} | |
return true; // see docs | |
}, | |
child: ListView( | |
children: [ | |
Container(height: 100, width: 100, color: Colors.red), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.blue), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.red), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.blue), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.red), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.blue), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.red), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.blue), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.red), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
Container(height: 100, width: 100, color: Colors.blue), | |
AnimatedContainer(duration: _duration, height: spaceBetween), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment