Flutter基础使用汇总

Flutter 监听返回按键

通过 WillPopScope 组件来注册监听回调

Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _requestPop,
child: Scaffold(
appBar: AppBar(
title: Text('测试代码'),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
print("退出${Navigator.canPop(context)}");
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
},
),
),
),
);
}

Future<bool> _requestPop() {
print("POP");
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
return Future.value(false);
}

Flutter 防止屏幕翻转

在 main.dart 入口中加入如下代码

void main() {
HttpOverrides.global = new MyHttpOverrides();
// runApp(MyApp());
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(new MyApp());
});
}

BMW WARNING

  • Bulletin

I am a bucolic migrant worker but I never walk backwards.

  • Material
  • Warrant

本文作者: Skyline(lty)
版权声明: 本博客所有文章除特别声明外, 均采用 CC BY - NC - SA 3.0 协议。 转载请注明出处!

Copyright © 2017 - 2024 鹧鸪天 All Rights Reserved.

skyline 保留所有权利