-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdetails_feature.dart
150 lines (130 loc) · 4.19 KB
/
details_feature.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import 'package:dart_board_core/dart_board_core.dart';
import 'package:dart_board_locator/dart_board_locator.dart';
import 'package:flutter/material.dart';
import 'package:transparent_image/transparent_image.dart';
import 'repository_feature.dart';
class DetailsFeature extends DartBoardFeature {
@override
List<RouteDefinition> get routes => [
NamedRouteDefinition(
route: "/details",
builder: (context, settings) => MainDetailsScreen()),
NamedRouteDefinition(
route: "/details_by_id",
builder: (context, settings) {
dynamic args = settings.arguments;
return DetailsScreen(id: args["id"]);
})
];
@override
List<DartBoardDecoration> get appDecorations =>
[LocatorDecoration(() => MainDetailsState())];
@override
String get namespace => "DetailsFeature";
@override
List<DartBoardFeature> get dependencies =>
[RepositoryFeature(repository: MockRepository())];
}
// State for the "Main" route inside the tabs
class MainDetailsState extends ChangeNotifier {
int _id = 0;
set id(int id) {
_id = id;
notifyListeners();
}
int get id => _id;
}
// Messenger class to send messages to the State class
class MainDetailsMessenger {
static void setId(BuildContext context, int id) =>
locate<MainDetailsState>().id = id;
}
class MainDetailsScreen extends StatefulWidget {
@override
_MainDetailsScreenState createState() => _MainDetailsScreenState();
}
class _MainDetailsScreenState extends State<MainDetailsScreen> {
@override
Widget build(BuildContext context) => locateAndBuild<MainDetailsState>(
(ctx, state) => DetailsScreen(id: state.id));
}
/// Details for a particular ID
class DetailsScreen extends StatefulWidget {
final int id;
const DetailsScreen({Key? key, required this.id}) : super(key: key);
@override
_DetailsScreenState createState() => _DetailsScreenState();
}
class _DetailsScreenState extends State<DetailsScreen> {
late Future<LongRecord> details;
// Re-initialize the future
void initFuture() {
details = RepositoryMessenger.fetchDetails(context, widget.id);
}
@override
void initState() {
initFuture();
super.initState();
}
@override
void didUpdateWidget(covariant DetailsScreen oldWidget) {
super.didUpdateWidget(oldWidget);
setState(() {
initFuture();
});
}
@override
Widget build(BuildContext context) => FutureBuilder<LongRecord>(
future: details,
builder: (ctx, snapshot) {
if (snapshot.hasData && snapshot.data != null) {
final data = snapshot.data!;
var textTheme = Theme.of(context).textTheme;
return Stack(
children: [
FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
image: data.imageUrl,
fit: BoxFit.cover,
width: double.infinity,
height: double.infinity,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
width: 300,
child: Card(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
data.title,
style: textTheme.headline3,
),
Text(
data.price,
style: textTheme.headline4,
),
Container(
height: 48,
),
Text(
data.description,
style: textTheme.bodyText1,
),
],
),
),
)),
),
),
],
);
} else {
return LinearProgressIndicator();
}
});
}