[英]How to remove Android back buttons on MaterialApp on Flutter?
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
OrientationSingleton.left = true;
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
.then((_) {
runApp(new MyApp());
});
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
這是我的應用程序。 我找到了一些關於如何刪除它的教程: flutter remove back button on appbar但它適用於 AppBar。 我嘗試讓我的應用程序在AppBar
上運行,但我得到了
MediaQuery.of() called with a context that does not contain a MediaQuery.
因為我在我的應用程序中依賴MediaQuery.of()
。
那么,如何為MaterialApp
移除 Flutter 上的 Android 返回、主頁和方形按鈕?
如錯誤消息中所述, SystemChrome
需要context
- 調用它的地方可能是Widget
的initState()
方法:
class MyApp extends StatefulWidget {
const MyApp({ Key key }) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
OrientationSingleton.left = true;
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]);
}
@override
Widget build(BuildContext context) {
return MaterialApp(); // your MaterialApp class
}
}
要隱藏Android 上的系統底部欄,一種選擇是調用setEnabledSystemUIOverlays()
function 並使用一個空列表:
SystemChrome.setEnabledSystemUIOverlays([]);
但是,並非所有 Android 器件都全局支持此 function。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.