回答編集履歴
5
追記
test
CHANGED
@@ -177,3 +177,125 @@
|
|
177
177
|
|
178
178
|
|
179
179
|
```
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
----
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
追加質問に対する回答 2020-3-6
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
FavoriteScreenでFavoriteListWidgetに渡すリストをお気に入りのコースリストにすればいいと思います。やり方は色々あると思いますが、Courseクラスにコースリストの情報があるので、Courseにお気に入りに登録されているIDを渡してCourseクラスでお気に入りのコースリストを生成するようにしてみました。
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
以下、変更したコードを載せておきます。
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
FavoriteScreenでお気に入りのコースを作る処理を呼び出す`course.favoriteList(favoriteList.ids)`
|
200
|
+
|
201
|
+
```
|
202
|
+
|
203
|
+
class FavoriteScreen extends StatelessWidget {
|
204
|
+
|
205
|
+
static const routeName = '/favorite-screen';
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
@override
|
210
|
+
|
211
|
+
Widget build(BuildContext context) {
|
212
|
+
|
213
|
+
return Scaffold(
|
214
|
+
|
215
|
+
appBar: AppBar(
|
216
|
+
|
217
|
+
title: Text('favorite'),
|
218
|
+
|
219
|
+
centerTitle: true,
|
220
|
+
|
221
|
+
flexibleSpace: Container(
|
222
|
+
|
223
|
+
decoration: BoxDecoration(
|
224
|
+
|
225
|
+
gradient: LinearGradient(
|
226
|
+
|
227
|
+
begin: Alignment.centerLeft,
|
228
|
+
|
229
|
+
end: Alignment.centerRight,
|
230
|
+
|
231
|
+
colors: <Color>[Colors.lightBlue, Colors.lightBlueAccent],
|
232
|
+
|
233
|
+
),
|
234
|
+
|
235
|
+
),
|
236
|
+
|
237
|
+
),
|
238
|
+
|
239
|
+
),
|
240
|
+
|
241
|
+
body: Material(
|
242
|
+
|
243
|
+
color: Colors.yellow,
|
244
|
+
|
245
|
+
child: Consumer2<Course, FavoriteList>(
|
246
|
+
|
247
|
+
builder: (context, course, favoriteList, child) => FavoriteListWidget(
|
248
|
+
|
249
|
+
list: course.favoriteList(favoriteList.ids),
|
250
|
+
|
251
|
+
),
|
252
|
+
|
253
|
+
),
|
254
|
+
|
255
|
+
),
|
256
|
+
|
257
|
+
);
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
```
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
FavoriteListにidsを返すプロパティを追加 (一部抜粋)
|
268
|
+
|
269
|
+
```
|
270
|
+
|
271
|
+
class FavoriteList with ChangeNotifier {
|
272
|
+
|
273
|
+
// 省略
|
274
|
+
|
275
|
+
List<String> get ids => _favoriteIds;
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
```
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
Courseでお気に入りのコースリストを作る (一部抜粋)
|
286
|
+
|
287
|
+
```
|
288
|
+
|
289
|
+
class Course with ChangeNotifier {
|
290
|
+
|
291
|
+
// 省略
|
292
|
+
|
293
|
+
UnmodifiableListView<Course> favoriteList(List<String> ids) =>
|
294
|
+
|
295
|
+
UnmodifiableListView(
|
296
|
+
|
297
|
+
_favorite.where((course) => ids.contains(course.id)));
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
```
|
4
誤字
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
やりたいことは
|
5
|
+
やりたいことはFlutterの[Cartサンプル](https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple)に似ている気がしました。
|
6
6
|
|
7
7
|
|
8
8
|
|
3
誤植
test
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
GitHubのコードを修正して実験したコードを載せておきます。
|
20
20
|
|
21
|
-
なお、以下のコードだと、SharedPreferencesのロードが非同期なので、一瞬だけお気に入りが消えた状態で
|
21
|
+
なお、以下のコードだと、SharedPreferencesのロードが非同期なので、一瞬だけお気に入りが消えた状態でリストが表示されてしまいます。必要ならロードが終わるまでリストを非表示にするなど対応した方がいいかもしれません。
|
22
22
|
|
23
23
|
|
24
24
|
|
2
補足
test
CHANGED
@@ -9,3 +9,171 @@
|
|
9
9
|
`Course`モデルとは別に、お気に入りのリストを管理する`FavoriteList`モデルをChangeNotifierを継承して作成する。起動直後に`ChangeNotifierProvider`で初期化して、そのときに`shared_preferences`から取得する。
|
10
10
|
|
11
11
|
後は必要な箇所で`Provider.of`や`Consumer`で`FavoriteList`にアクセスすればいい気がします。お気に入りを変更する場合は、`FavoriteList`の状態を変更しつつ、内部で`shared_preferences`の内容も更新する感じですかね
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
----
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
GitHubのコードを修正して実験したコードを載せておきます。
|
20
|
+
|
21
|
+
なお、以下のコードだと、SharedPreferencesのロードが非同期なので、一瞬だけお気に入りが消えた状態でコースリストが表示されてしまいます。必要ならロードが終わるまでリストを非表示にするなど対応した方がいいかもしれません。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
models/favolit_list.dart
|
26
|
+
|
27
|
+
```
|
28
|
+
|
29
|
+
import 'package:flutter/material.dart';
|
30
|
+
|
31
|
+
import 'package:shared_preferences/shared_preferences.dart';
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
class FavoriteList with ChangeNotifier {
|
36
|
+
|
37
|
+
List<String> _favoriteIds = [];
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
FavoriteList() {
|
42
|
+
|
43
|
+
load();
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
void load() async {
|
50
|
+
|
51
|
+
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
52
|
+
|
53
|
+
_favoriteIds = prefs.getStringList('my_favorite_list') ?? [];
|
54
|
+
|
55
|
+
notifyListeners();
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
void save() async {
|
62
|
+
|
63
|
+
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
64
|
+
|
65
|
+
prefs.setStringList('my_favorite_list', _favoriteIds);
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
bool isFavorite(String id) {
|
72
|
+
|
73
|
+
return _favoriteIds.contains(id);
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
void toggle(String id) {
|
80
|
+
|
81
|
+
if (_favoriteIds.contains(id)) {
|
82
|
+
|
83
|
+
_favoriteIds.remove(id);
|
84
|
+
|
85
|
+
} else {
|
86
|
+
|
87
|
+
_favoriteIds.add(id);
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
save();
|
92
|
+
|
93
|
+
notifyListeners();
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
main.dart (一部抜粋)
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
void main() => runApp(MultiProvider(
|
108
|
+
|
109
|
+
providers: [
|
110
|
+
|
111
|
+
ChangeNotifierProvider<FavoriteList>(
|
112
|
+
|
113
|
+
create: (_) => FavoriteList(),
|
114
|
+
|
115
|
+
),
|
116
|
+
|
117
|
+
ChangeNotifierProvider<TextVisibility>(
|
118
|
+
|
119
|
+
create: (_) => TextVisibility(),
|
120
|
+
|
121
|
+
),
|
122
|
+
|
123
|
+
ChangeNotifierProvider<Course>(
|
124
|
+
|
125
|
+
create: (_) => Course(),
|
126
|
+
|
127
|
+
),
|
128
|
+
|
129
|
+
],
|
130
|
+
|
131
|
+
child: MyApp(),
|
132
|
+
|
133
|
+
));
|
134
|
+
|
135
|
+
```
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
widgets/list_cards.dart (一部抜粋)
|
140
|
+
|
141
|
+
```
|
142
|
+
|
143
|
+
trailing: Material(
|
144
|
+
|
145
|
+
color: Colors.transparent,
|
146
|
+
|
147
|
+
child: Consumer<FavoriteList>(
|
148
|
+
|
149
|
+
builder: (context, favoriteList, _) => IconButton(
|
150
|
+
|
151
|
+
icon: Icon(favoriteList.isFavorite(name.id)
|
152
|
+
|
153
|
+
? Icons.favorite
|
154
|
+
|
155
|
+
: Icons.favorite_border),
|
156
|
+
|
157
|
+
iconSize: 40,
|
158
|
+
|
159
|
+
autofocus: true,
|
160
|
+
|
161
|
+
highlightColor: Colors.pinkAccent,
|
162
|
+
|
163
|
+
hoverColor: Colors.pink,
|
164
|
+
|
165
|
+
onPressed: () {
|
166
|
+
|
167
|
+
favoriteList.toggle(name.id);
|
168
|
+
|
169
|
+
},
|
170
|
+
|
171
|
+
),
|
172
|
+
|
173
|
+
),
|
174
|
+
|
175
|
+
),
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
```
|
1
補足
test
CHANGED
@@ -8,4 +8,4 @@
|
|
8
8
|
|
9
9
|
`Course`モデルとは別に、お気に入りのリストを管理する`FavoriteList`モデルをChangeNotifierを継承して作成する。起動直後に`ChangeNotifierProvider`で初期化して、そのときに`shared_preferences`から取得する。
|
10
10
|
|
11
|
-
後は必要な箇所で`Provider.of`や`Consumer`で`FavoriteList`にアクセスすればいい気がします。お気に入りを変更する場合は、`FavoriteList`の状態を変更しつつ、`shared_preferences`の内容も更新する感じですかね
|
11
|
+
後は必要な箇所で`Provider.of`や`Consumer`で`FavoriteList`にアクセスすればいい気がします。お気に入りを変更する場合は、`FavoriteList`の状態を変更しつつ、内部で`shared_preferences`の内容も更新する感じですかね
|