Skip to content

Commit bac4fdc

Browse files
committed
feat: add get controller instead of stateful widget
1 parent b81d838 commit bac4fdc

5 files changed

+60
-47
lines changed

lib/animated_list_app.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import 'package:animated_list/screens/animated_list_screen.dart';
22
import 'package:flutter/material.dart';
3+
import 'package:get/get.dart';
34

45
class AnimatedListApp extends StatelessWidget {
56
const AnimatedListApp({Key? key}) : super(key: key);
67

78
@override
89
Widget build(BuildContext context) {
9-
return MaterialApp(
10+
return GetMaterialApp(
1011
title: 'Flutter Demo',
1112
debugShowCheckedModeBanner: false,
1213
theme: ThemeData(
1314
primarySwatch: Colors.blue,
1415
),
15-
home: const AnimatedListScreen(title: 'Flutter Animated List Example'),
16+
home: AnimatedListScreen(title: 'Flutter Animated List Example'),
1617
);
1718
}
1819
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:get/get.dart';
3+
4+
class AnimatedListController extends GetxController {
5+
RxDouble topPadding = 15.0.obs;
6+
final duration = const Duration(milliseconds: 300);
7+
8+
scrollUpdated(ScrollMetrics metrics) {
9+
if (topPadding.value == 25.0) return;
10+
topPadding.value = 25.0;
11+
}
12+
13+
scrollEnded(ScrollMetrics metrics) {
14+
topPadding.value = 15;
15+
}
16+
}

lib/screens/animated_list_screen.dart

+31-45
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,61 @@
11
import 'package:flutter/material.dart';
2+
import 'package:get/get.dart';
23

3-
class AnimatedListScreen extends StatefulWidget {
4-
const AnimatedListScreen({Key? key, required this.title}) : super(key: key);
4+
import 'animated_list_controller.dart';
55

6+
class AnimatedListScreen extends StatelessWidget {
67
final String title;
78

8-
@override
9-
_AnimatedListScreenState createState() => _AnimatedListScreenState();
10-
}
9+
AnimatedListScreen({Key? key, required this.title}) : super(key: key);
1110

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());
1512

1613
@override
1714
Widget build(BuildContext context) {
1815
return Scaffold(
1916
appBar: AppBar(
20-
title: Text(widget.title),
17+
title: Text(title),
2118
),
2219
body: customAnimatedList,
2320
);
2421
}
2522

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-
3923
Widget get customAnimatedList => NotificationListener<ScrollNotification>(
4024
onNotification: (scrollNotification) {
4125
if (scrollNotification is ScrollUpdateNotification) {
42-
_scrollUpdated(scrollNotification.metrics);
26+
controller.scrollUpdated(scrollNotification.metrics);
4327
} else if (scrollNotification is ScrollEndNotification) {
44-
_scrollEnded(scrollNotification.metrics);
28+
controller.scrollEnded(scrollNotification.metrics);
4529
}
4630
return true;
4731
},
4832
child: ListView.builder(
4933
itemCount: 25,
5034
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),
6845
),
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+
),
7359
),
7460
),
7561
);

pubspec.lock

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ packages:
7474
description: flutter
7575
source: sdk
7676
version: "0.0.0"
77+
get:
78+
dependency: "direct main"
79+
description:
80+
name: get
81+
url: "https://pub.dartlang.org"
82+
source: hosted
83+
version: "4.6.1"
7784
lints:
7885
dependency: transitive
7986
description:

pubspec.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ dependencies:
3535
# Use with the CupertinoIcons class for iOS style icons.
3636
cupertino_icons: ^1.0.2
3737

38+
#
39+
get: 4.6.1
40+
3841
dev_dependencies:
3942
flutter_test:
4043
sdk: flutter

0 commit comments

Comments
 (0)