MaterialPageRoute
Navigator 需要包在 MaterialApp() 裡面,才可以使用。
導到某一個頁面語法:
Navigator.push(context,
MaterialPageRoute(builder: (context) => DetailPage()));
回到上一頁:
Navigator.pop(context);


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
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MainPage());
}
class MainPage extends StatelessWidget {
const MainPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
// Navigator 需要包在 MaterialApp() 裡面,才可以使用。
return MaterialApp(
home: ListPage(),
);
}
}
class ListPage extends StatefulWidget {
ListPage({Key? key}) : super(key: key);
@override
_ListPageState createState() => _ListPageState();
}
class _ListPageState extends State<ListPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List Page'),
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => DetailPage()));
},
child: Container(
color: Colors.blue,
margin: EdgeInsets.only(top: 10),
height: 80,
alignment: Alignment.center,
child: Text('item $index',
style: TextStyle(color: Colors.white, fontSize: 20)),
),
);
},
),
);
}
}
class DetailPage extends StatefulWidget {
DetailPage({Key? key}) : super(key: key);
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Detail Page'),
),
body: Center(
child: TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Back'),
),
),
);
}
}
傳遞參數
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
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MainPage());
}
class MainPage extends StatelessWidget {
const MainPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: ListPage(),
);
}
}
class ListPage extends StatefulWidget {
ListPage({Key? key}) : super(key: key);
@override
_ListPageState createState() => _ListPageState();
}
class _ListPageState extends State<ListPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('List Page'),
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage(id: index)));
},
child: Container(
color: Colors.blue,
margin: EdgeInsets.only(top: 10),
height: 80,
alignment: Alignment.center,
child: Text('item $index',
style: TextStyle(color: Colors.white, fontSize: 20)),
),
);
},
),
);
}
}
class DetailPage extends StatefulWidget {
final int? id;
const DetailPage({Key? key, this.id}) : super(key: key);
@override
_DetailPageState createState() => _DetailPageState();
}
class _DetailPageState extends State<DetailPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Detail Page'),
),
body: Center(
child: TextButton(
onPressed: () {
Navigator.pop(context);
},
child: Text('Back to List Page, id: ${widget.id}'),
),
),
);
}
}