Skip to content

Commit 17f0ec9

Browse files
committed
Optimized HFD MiniApp with providers
1 parent cc1ca2a commit 17f0ec9

File tree

4 files changed

+122
-93
lines changed

4 files changed

+122
-93
lines changed

lib/MiniApps/HealtyFoodDelivery/Screens/DetailScreen/HFDDetailScreen.dart

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:ui';
22
import 'package:flutter/material.dart';
3+
import 'package:provider/provider.dart';
34

45
import 'package:flutter_uis/configs/AppDimensions.dart';
56
import 'package:flutter_uis/configs/App.dart';
@@ -17,41 +18,25 @@ import 'widgets/HFDDetailScreenBackground.dart';
1718
import 'widgets/HFDDetailScreenHeader.dart';
1819
import 'widgets/HFDDetailScreenBody.dart';
1920
import 'Dimensions.dart';
21+
import 'Provider.dart';
2022

2123
enum AnimProp {
2224
base,
2325
circle,
2426
bars,
2527
}
2628

27-
class HFDDetailScreen extends StatefulWidget {
28-
HFDDetailScreen({Key key}) : super(key: key);
29-
30-
@override
31-
_HFDDetailScreenState createState() => _HFDDetailScreenState();
32-
}
33-
34-
class _HFDDetailScreenState extends State<HFDDetailScreen>
35-
with SingleTickerProviderStateMixin {
36-
ScrollController scrollController = ScrollController();
37-
double offset = 0.0;
38-
29+
class HFDDetailScreen extends StatelessWidget {
3930
@override
40-
initState() {
41-
this.scrollController.addListener(() {
42-
setState(() {
43-
this.offset = this.scrollController.offset;
44-
});
45-
});
46-
47-
super.initState();
48-
}
49-
50-
@override
51-
dispose() {
52-
super.dispose();
31+
Widget build(BuildContext context) {
32+
return ChangeNotifierProvider<HFDDetailState>(
33+
create: (_) => HFDDetailState(),
34+
child: _Body(),
35+
);
5336
}
37+
}
5438

39+
class _Body extends StatelessWidget {
5540
@override
5641
Widget build(BuildContext context) {
5742
Dimensions.init(context);
@@ -90,7 +75,15 @@ class _HFDDetailScreenState extends State<HFDDetailScreen>
9075
baseDuration,
9176
);
9277

93-
return Container(
78+
return NotificationListener<ScrollNotification>(
79+
onNotification: (notification) {
80+
if (notification is ScrollUpdateNotification) {
81+
HFDDetailState.state(context).setOffset(
82+
notification.metrics.pixels,
83+
);
84+
}
85+
return true;
86+
},
9487
child: Screen(
9588
theme: Theme.of(context).copyWith(
9689
accentColor: theme.primary,
@@ -99,19 +92,17 @@ class _HFDDetailScreenState extends State<HFDDetailScreen>
9992
textStyle: textStyle,
10093
fontFamily: 'Nunito',
10194
builder: (_) {
102-
double safeOffset = this.offset;
103-
104-
if (safeOffset > Dimensions.coverImageHeight) {
105-
safeOffset = Dimensions.coverImageHeight;
106-
}
107-
10895
return SingleChildScrollView(
109-
controller: this.scrollController,
96+
physics: BouncingScrollPhysics(),
11097
child: Stack(
11198
children: <Widget>[
112-
HFDDetailScreenBackground(
113-
item: item,
114-
offset: this.offset,
99+
Consumer<HFDDetailState>(
100+
builder: (context, state, child) {
101+
return HFDDetailScreenBackground(
102+
offset: state.offset,
103+
item: item,
104+
);
105+
},
115106
),
116107
Container(
117108
margin: EdgeInsets.only(
@@ -188,15 +179,16 @@ class _HFDDetailScreenState extends State<HFDDetailScreen>
188179
],
189180
),
190181
),
191-
Padding(
192-
padding: EdgeInsets.only(
193-
top: safeOffset < 0 ? 0 : safeOffset,
194-
),
195-
),
196-
Padding(
197-
padding: EdgeInsets.only(
198-
top: safeOffset < 0 ? 0 : safeOffset,
199-
),
182+
Consumer<HFDDetailState>(
183+
builder: (context, state, child) {
184+
final safeOffset =
185+
state.getSafe(Dimensions.coverImageHeight);
186+
return Padding(
187+
padding: EdgeInsets.only(
188+
top: safeOffset < 0 ? 0 : safeOffset,
189+
),
190+
);
191+
},
200192
),
201193
HFDDetailScreenHeader(),
202194
],
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter/foundation.dart';
3+
import 'package:provider/provider.dart';
4+
5+
class HFDDetailState extends ChangeNotifier {
6+
static HFDDetailState state(BuildContext context, [listen = false]) =>
7+
Provider.of<HFDDetailState>(context, listen: listen);
8+
9+
double _offset = 0.0;
10+
11+
double get offset => this._offset;
12+
setOffset(double x) {
13+
this._offset = x;
14+
notifyListeners();
15+
}
16+
17+
getSafe(double max) {
18+
double safeOffset = this.offset;
19+
if (safeOffset > max) {
20+
safeOffset = max;
21+
}
22+
return safeOffset;
23+
}
24+
}

lib/MiniApps/HealtyFoodDelivery/Screens/HomeScreen/HFDHomeScreen.dart

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:ui';
22
import 'package:flutter/material.dart';
33
import 'package:flutter_icons/flutter_icons.dart';
4+
import 'package:provider/provider.dart';
45

56
import 'package:flutter_uis/configs/AppDimensions.dart';
67
import 'package:flutter_uis/configs/App.dart';
@@ -18,27 +19,19 @@ import 'messages/keys.dart';
1819
import 'data.dart' as data;
1920
import 'Dimensions.dart';
2021
import 'TestKeys.dart';
22+
import 'Provider.dart';
2123

22-
class HFDHomeScreen extends StatefulWidget {
23-
HFDHomeScreen({Key key}) : super(key: key);
24-
25-
@override
26-
_HFDHomeScreenState createState() => _HFDHomeScreenState();
27-
}
28-
29-
class _HFDHomeScreenState extends State<HFDHomeScreen> {
30-
int activeBottomNav = 0;
31-
24+
class HFDHomeScreen extends StatelessWidget {
3225
@override
33-
initState() {
34-
super.initState();
35-
}
36-
37-
@override
38-
dispose() {
39-
super.dispose();
26+
Widget build(BuildContext context) {
27+
return ChangeNotifierProvider<HFDHomeState>(
28+
create: (_) => HFDHomeState(),
29+
child: _Body(),
30+
);
4031
}
32+
}
4133

34+
class _Body extends StatelessWidget {
4235
@override
4336
Widget build(BuildContext context) {
4437
Dimensions.init(context);
@@ -50,38 +43,40 @@ class _HFDHomeScreenState extends State<HFDHomeScreen> {
5043
primaryColor: theme.primary,
5144
),
5245
fontFamily: 'Nunito',
53-
bottomNavigationBar: BottomNavigationBar(
54-
type: BottomNavigationBarType.fixed,
55-
currentIndex: this.activeBottomNav,
56-
selectedItemColor: theme.primary,
57-
unselectedItemColor: Colors.black,
58-
onTap: (index) {
59-
setState(() {
60-
this.activeBottomNav = index;
61-
});
62-
},
63-
items: data.bottomNavList
64-
.map(
65-
(item) => BottomNavigationBarItem(
66-
icon: Icon(item),
67-
title: Padding(
68-
padding: EdgeInsets.all(0),
69-
child: Container(
70-
width: 30,
71-
height: 4,
72-
transform: Matrix4.identity()..translate(0.0, 4.0),
73-
decoration: BoxDecoration(
74-
color: (data.bottomNavList[this.activeBottomNav] == item
75-
? theme.primary
76-
: Colors.transparent),
77-
borderRadius: BorderRadius.circular(4.0),
46+
bottomNavigationBar: Selector<HFDHomeState, int>(
47+
selector: (_, state) => state.activeTab,
48+
builder: (context, activeTab, child) {
49+
return BottomNavigationBar(
50+
type: BottomNavigationBarType.fixed,
51+
currentIndex: activeTab,
52+
selectedItemColor: theme.primary,
53+
unselectedItemColor: Colors.black,
54+
onTap: (index) {
55+
HFDHomeState.state(context).setActiveTab(index);
56+
},
57+
items: data.bottomNavList.map(
58+
(item) {
59+
return BottomNavigationBarItem(
60+
icon: Icon(item),
61+
title: Padding(
62+
padding: EdgeInsets.all(0),
63+
child: Container(
64+
width: 30,
65+
height: 4,
66+
transform: Matrix4.identity()..translate(0.0, 4.0),
67+
decoration: BoxDecoration(
68+
color: (data.bottomNavList[activeTab] == item
69+
? theme.primary
70+
: Colors.transparent),
71+
borderRadius: BorderRadius.circular(4.0),
72+
),
73+
),
7874
),
79-
),
80-
),
81-
),
82-
)
83-
.toList(),
84-
),
75+
);
76+
},
77+
).toList(),
78+
);
79+
}),
8580
textStyle: Theme.of(context)
8681
.textTheme
8782
.bodyText1
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter/foundation.dart';
3+
import 'package:provider/provider.dart';
4+
5+
class HFDHomeState extends ChangeNotifier {
6+
static HFDHomeState state(BuildContext context, [listen = false]) =>
7+
Provider.of<HFDHomeState>(context, listen: listen);
8+
9+
int _activeTab = 0;
10+
int get activeTab => this._activeTab;
11+
setActiveTab(int tab) {
12+
if (tab == this._activeTab) {
13+
return;
14+
}
15+
this._activeTab = tab;
16+
notifyListeners();
17+
}
18+
}

0 commit comments

Comments
 (0)