用SystemChrome

引入services.dart

1
import 'package:flutter/services.dart';

修改整個app

main() 函數中加入

1
2
3
4
5
6
7
8
9
10
11
void main() {

SystemChrome.setPreferredOrientations([
//你要的方向
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);

runApp(...);

}

修改單一頁面

在該頁面的 initState() 函數中加入

1
2
3
4
5
6
7
8
9
10
11
12
@override
void initState() {

super.initState();

SystemChrome.setPreferredOrientations([
//你要的方向
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);

}

在該頁面的 dispose() 函數中加入

1
2
3
4
5
6
7
8
9
10
11
12
@override
void dispose() {

SystemChrome.setPreferredOrientations([
//代表遺棄時方向改為直立
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);

super.dispose();

}