Created
August 13, 2022 13:36
-
-
Save Roaa94/a25f725de2142c782b9272d80b53c5a5 to your computer and use it in GitHub Desktop.
Animated Scroll Views
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'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Animated Scroll View Item', | |
debugShowCheckedModeBanner: false, | |
home: HomePage(), | |
); | |
} | |
} | |
class HomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return const Scaffold( | |
body: SafeArea( | |
bottom: false, | |
child: AnimatedListView(), | |
// child: AnimatedGridView(), | |
// child: AnimatedHorizontalListView(), | |
), | |
); | |
} | |
} | |
class AnimatedScrollViewItem extends StatefulWidget { | |
const AnimatedScrollViewItem({ | |
Key? key, | |
required this.child, | |
}) : super(key: key); | |
final Widget child; | |
@override | |
State<AnimatedScrollViewItem> createState() => _AnimatedScrollViewItemState(); | |
} | |
class _AnimatedScrollViewItemState extends State<AnimatedScrollViewItem> | |
with SingleTickerProviderStateMixin { | |
late final AnimationController _animationController; | |
late final Animation<double> _scaleAnimation; | |
@override | |
void initState() { | |
super.initState(); | |
_animationController = AnimationController( | |
vsync: this, | |
duration: const Duration(milliseconds: 300), | |
)..forward(); | |
_scaleAnimation = Tween<double>(begin: 0.5, end: 1).animate( | |
CurvedAnimation( | |
parent: _animationController, | |
curve: Curves.easeInOut, | |
), | |
); | |
} | |
@override | |
void dispose() { | |
_animationController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return ScaleTransition( | |
scale: _scaleAnimation, | |
child: widget.child, | |
); | |
} | |
} | |
class AnimatedListView extends StatelessWidget { | |
const AnimatedListView({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
cacheExtent: 0, | |
padding: const EdgeInsets.symmetric(horizontal: 17, vertical: 20), | |
itemCount: 100, | |
itemBuilder: (context, index) => | |
const AnimatedScrollViewItem(child: ListItem()), | |
); | |
} | |
} | |
class AnimatedGridView extends StatelessWidget { | |
const AnimatedGridView({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return GridView.builder( | |
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 4, | |
mainAxisSpacing: 15, | |
crossAxisSpacing: 15, | |
), | |
cacheExtent: 0, | |
padding: const EdgeInsets.symmetric(horizontal: 17, vertical: 20), | |
itemCount: 200, | |
itemBuilder: (context, index) { | |
return const AnimatedScrollViewItem(child: ListItem()); | |
}, | |
); | |
} | |
} | |
class AnimatedHorizontalListView extends StatelessWidget { | |
const AnimatedHorizontalListView({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return SizedBox( | |
height: 220, | |
child: ListView.builder( | |
cacheExtent: 0, | |
scrollDirection: Axis.horizontal, | |
padding: const EdgeInsets.symmetric(horizontal: 17, vertical: 20), | |
itemCount: 100, | |
itemBuilder: (context, index) { | |
return const AnimatedScrollViewItem( | |
child: Padding( | |
padding: EdgeInsets.only(right: 10), | |
child: SizedBox( | |
width: 100, | |
child: ListItem(), | |
), | |
), | |
); | |
}, | |
), | |
); | |
} | |
} | |
class ListItem extends StatelessWidget { | |
const ListItem({ | |
Key? key, | |
this.title = '', | |
}) : super(key: key); | |
final String title; | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
margin: const EdgeInsets.only(bottom: 15), | |
padding: const EdgeInsets.all(20), | |
decoration: BoxDecoration( | |
color: Colors.blue.shade100, | |
borderRadius: BorderRadius.circular(15), | |
), | |
child: Text(title), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment