質問編集履歴

1

該当ソースコードを全文記載しました。

2020/08/29 02:31

投稿

Inete6Q
Inete6Q

スコア18

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  KboyさんのYouTube動画を見て、写経していました。
8
8
 
9
+ Navigate with named routesを使おうとしています。
10
+
9
11
 
10
12
 
11
13
  ### 発生している問題・エラーメッセージ
@@ -46,6 +48,22 @@
46
48
 
47
49
  ```dart
48
50
 
51
+
52
+
53
+ //以下、main.dart
54
+
55
+ import 'package:flutter/material.dart';
56
+
57
+
58
+
59
+ void main() {
60
+
61
+ runApp(MyApp());
62
+
63
+ }
64
+
65
+
66
+
49
67
  class MyApp extends StatelessWidget {
50
68
 
51
69
  // This widget is the root of your application.
@@ -68,7 +86,193 @@
68
86
 
69
87
  ),
70
88
 
89
+ home: MyHomePage(title: 'Flutter Demo Home Page'),
90
+
91
+ );
92
+
93
+ }
94
+
95
+ }
96
+
97
+
98
+
99
+ class MyHomePage extends StatefulWidget {
100
+
101
+ MyHomePage({Key key, this.title}) : super(key: key);
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+ final String title;
110
+
111
+
112
+
113
+ @override
114
+
115
+ _MyHomePageState createState() => _MyHomePageState();
116
+
117
+ }
118
+
119
+
120
+
121
+ class _MyHomePageState extends State<MyHomePage> {
122
+
123
+ int _counter = 0;
124
+
125
+
126
+
127
+ void _incrementCounter() {
128
+
129
+ setState(() {
130
+
131
+
132
+
133
+ _counter++;
134
+
135
+ });
136
+
137
+ }
138
+
139
+
140
+
141
+ @override
142
+
143
+ Widget build(BuildContext context) {
144
+
145
+
146
+
147
+ return Scaffold(
148
+
149
+ appBar: AppBar(
150
+
151
+
152
+
153
+ title: Text(widget.title),
154
+
155
+ ),
156
+
157
+ body: Center(
158
+
159
+
160
+
161
+ child: RaisedButton(
162
+
163
+ child: Text('次へ'),
164
+
165
+ onPressed: (){
166
+
167
+ Navigator.pushNamed(context, '/next');
168
+
169
+ },
170
+
171
+ ),
172
+
173
+ ),
174
+
175
+ floatingActionButton: FloatingActionButton(
176
+
177
+ onPressed: _incrementCounter,
178
+
179
+ tooltip: 'Increment',
180
+
181
+ child: Icon(Icons.add),
182
+
183
+ ), // This trailing comma makes auto-formatting nicer for build methods.
184
+
185
+ );
186
+
187
+ }
188
+
189
+ }
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ //以下、next_page.dart
198
+
199
+ import 'package:flutter/material.dart';
200
+
201
+
202
+
203
+ class NextPage extends StatelessWidget {
204
+
205
+ @override
206
+
207
+ Widget build(BuildContext context) {
208
+
209
+ // TODO: implement build
210
+
211
+ return Scaffold(
212
+
213
+ appBar: AppBar(
214
+
215
+
216
+
217
+ title: Text('次の画面'),
218
+
219
+ ),
220
+
221
+ body: Container(
222
+
223
+ height: double.infinity,
224
+
225
+ color: Colors.red,
226
+
227
+ child: Center(
228
+
229
+ child: RaisedButton(
230
+
231
+ child: Text('戻る'),
232
+
233
+ onPressed: () {
234
+
235
+ Navigator.pop(context);
236
+
237
+ },
238
+
239
+ ),
240
+
241
+ ),
242
+
243
+ ),
244
+
245
+ );
246
+
247
+ }
248
+
249
+
250
+
251
+ }
252
+
253
+ ```
254
+
255
+
256
+
257
+ ### 試したこと
258
+
259
+
260
+
261
+ ```
262
+
263
+ home: MyHomePage(title: 'Flutter Demo Home Page'),
264
+
265
+ ```
266
+
267
+ の記述を削除後、initialRoute以下を追加した際にエラーが起きました。
268
+
269
+ (その前まではエラーなく動きました。)
270
+
271
+ ```dart
272
+
273
+ //以下を追加
274
+
71
- initialRoute: '/',
275
+ initialRoute: '/',
72
276
 
73
277
  routes: {
74
278
 
@@ -82,52 +286,6 @@
82
286
 
83
287
  },
84
288
 
85
-
86
-
87
- );
88
-
89
-
90
-
91
- }
92
-
93
- }
94
-
95
- ```
96
-
97
-
98
-
99
- ### 試したこと
100
-
101
-
102
-
103
- ```
104
-
105
- home: MyHomePage(title: 'Flutter Demo Home Page'),
106
-
107
- ```
108
-
109
- の記述を削除後、initialRoute以下を追加した際にエラーが起きました。
110
-
111
- (その前まではエラーなく動きました。)
112
-
113
- ```dart
114
-
115
- //以下を追加
116
-
117
- initialRoute: '/',
118
-
119
- routes: {
120
-
121
-
122
-
123
- '/': (context) => MyHomePage(),
124
-
125
-
126
-
127
- '/next': (context) => NextPage(),
128
-
129
- },
130
-
131
289
  ```
132
290
 
133
291