|
1 | 1 | import 'package:flutter/material.dart';
|
| 2 | +import 'package:get/get.dart'; |
2 | 3 |
|
3 |
| -class AnimatedListScreen extends StatefulWidget { |
4 |
| - const AnimatedListScreen({Key? key, required this.title}) : super(key: key); |
| 4 | +import 'animated_list_controller.dart'; |
5 | 5 |
|
| 6 | +class AnimatedListScreen extends StatelessWidget { |
6 | 7 | final String title;
|
7 | 8 |
|
8 |
| - @override |
9 |
| - _AnimatedListScreenState createState() => _AnimatedListScreenState(); |
10 |
| -} |
| 9 | + AnimatedListScreen({Key? key, required this.title}) : super(key: key); |
11 | 10 |
|
12 |
| -class _AnimatedListScreenState extends State<AnimatedListScreen> { |
13 |
| - double _topPadding = 15.0; |
14 |
| - final _duration = const Duration(milliseconds: 300); |
| 11 | + final AnimatedListController controller = Get.put(AnimatedListController()); |
15 | 12 |
|
16 | 13 | @override
|
17 | 14 | Widget build(BuildContext context) {
|
18 | 15 | return Scaffold(
|
19 | 16 | appBar: AppBar(
|
20 |
| - title: Text(widget.title), |
| 17 | + title: Text(title), |
21 | 18 | ),
|
22 | 19 | body: customAnimatedList,
|
23 | 20 | );
|
24 | 21 | }
|
25 | 22 |
|
26 |
| - _scrollUpdated(ScrollMetrics metrics) { |
27 |
| - setState(() { |
28 |
| - if (_topPadding == 20.0) return; |
29 |
| - _topPadding = 20.0; |
30 |
| - }); |
31 |
| - } |
32 |
| - |
33 |
| - _scrollEnded(ScrollMetrics metrics) { |
34 |
| - setState(() { |
35 |
| - _topPadding = 15.0; |
36 |
| - }); |
37 |
| - } |
38 |
| - |
39 | 23 | Widget get customAnimatedList => NotificationListener<ScrollNotification>(
|
40 | 24 | onNotification: (scrollNotification) {
|
41 | 25 | if (scrollNotification is ScrollUpdateNotification) {
|
42 |
| - _scrollUpdated(scrollNotification.metrics); |
| 26 | + controller.scrollUpdated(scrollNotification.metrics); |
43 | 27 | } else if (scrollNotification is ScrollEndNotification) {
|
44 |
| - _scrollEnded(scrollNotification.metrics); |
| 28 | + controller.scrollEnded(scrollNotification.metrics); |
45 | 29 | }
|
46 | 30 | return true;
|
47 | 31 | },
|
48 | 32 | child: ListView.builder(
|
49 | 33 | itemCount: 25,
|
50 | 34 | itemBuilder: (context, index) {
|
51 |
| - return AnimatedPadding( |
52 |
| - padding: EdgeInsets.only(top: _topPadding), |
53 |
| - duration: _duration, |
54 |
| - child: Padding( |
55 |
| - padding: const EdgeInsets.symmetric(horizontal: 8.0), |
56 |
| - child: Container( |
57 |
| - decoration: BoxDecoration( |
58 |
| - borderRadius: const BorderRadius.all( |
59 |
| - Radius.circular(12), |
60 |
| - ), |
61 |
| - boxShadow: [ |
62 |
| - BoxShadow( |
63 |
| - color: Colors.grey.withOpacity(0.5), |
64 |
| - spreadRadius: 5, |
65 |
| - blurRadius: 7, |
66 |
| - offset: |
67 |
| - const Offset(0, 3), // changes position of shadow |
| 35 | + return Obx( |
| 36 | + () => AnimatedPadding( |
| 37 | + padding: EdgeInsets.only(top: controller.topPadding.value), |
| 38 | + duration: controller.duration, |
| 39 | + child: Padding( |
| 40 | + padding: const EdgeInsets.symmetric(horizontal: 8.0), |
| 41 | + child: Container( |
| 42 | + decoration: BoxDecoration( |
| 43 | + borderRadius: const BorderRadius.all( |
| 44 | + Radius.circular(12), |
68 | 45 | ),
|
69 |
| - ], |
70 |
| - color: index % 2 == 0 ? Colors.black38 : Colors.blue), |
71 |
| - height: 100, |
72 |
| - width: 100, |
| 46 | + boxShadow: [ |
| 47 | + BoxShadow( |
| 48 | + color: Colors.grey.withOpacity(0.5), |
| 49 | + spreadRadius: 5, |
| 50 | + blurRadius: 7, |
| 51 | + offset: const Offset( |
| 52 | + 0, 3), // changes position of shadow |
| 53 | + ), |
| 54 | + ], |
| 55 | + color: index % 2 == 0 ? Colors.black38 : Colors.blue), |
| 56 | + height: 100, |
| 57 | + width: 100, |
| 58 | + ), |
73 | 59 | ),
|
74 | 60 | ),
|
75 | 61 | );
|
|
0 commit comments