当前位置:网站首页>Flutter 返回按钮的监听
Flutter 返回按钮的监听
2022-07-04 20:35:00 【海阔天空6688】
物理按钮和返回按钮的监听
Flutter 返回按钮的监听是通过 WillPopScope来实现的
class BackDemoState extends State<BackDemoWidget> {
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onWillPop,
child: Scaffold(
appBar: AppBar(
title: const Text("返回键监听"),
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: (){
SmartDialog.showToast("返回");
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
},
),
),
));
}
Future<bool> _onWillPop() {
SmartDialog.showToast("返回");
if (Navigator.canPop(context)) {
Navigator.pop(context);
} else {
SystemNavigator.pop();
}
return Future.value(false);
}
}
WebView的返回监听
@override
Widget build(BuildContext context) {
return FutureBuilder<WebViewController>(
future: _controller.future,
builder: (context, snapshot) {
return WillPopScope(
onWillPop: () async {
if (snapshot.hasData) {
final bool canGoBack = await snapshot.data!.canGoBack();
if (canGoBack) {
// 网页可以返回时,优先返回上一页
await snapshot.data!.goBack();
return Future.value(false);
}
}
return Future.value(true);
},
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Stack(
children: [
WebView(
initialUrl: "https://www.baidu.com",
javascriptMode: JavascriptMode.unrestricted,
allowsInlineMediaPlayback: true,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
onProgress: (int progress) {
debugPrint('WebView is loading (progress : $progress%)');
setState(() {
_progressValue = progress;
});
},
),
if (_progressValue != 100) LinearProgressIndicator(
value: _progressValue / 100,
backgroundColor: Colors.transparent,
minHeight: 2,
) else Gaps.empty,
],
),
),
);
}
);
}
边栏推荐
- 软件开发过中的采购
- Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)
- Use of redis publish subscription
- Jerry's ad series MIDI function description [chapter]
- SolidWorks工程图添加材料明细表的操作
- Billions of citizens' information has been leaked! Is there any "rescue" for data security on the public cloud?
- 创客思维在高等教育中的启迪作用
- JS卡牌样式倒计时天数
- UTF encoding and character set in golang
- Foxit pdf editor v10.1.8 green version
猜你喜欢

D3.js+Three.js数据可视化3d地球js特效

Introduction to pressure measurement of JMeter

Day24:文件系统

Render function and virtual DOM

Configuration of DNS server of Huawei ENSP simulator

杰理之AD 系列 MIDI 功能说明【篇】

maya灯建模

Arcgis 10.2.2 | arcgis license server无法启动的解决办法

NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??

TCP三次握手,四次挥手,你真的了解吗?
随机推荐
Huawei ENSP simulator realizes communication security (switch)
Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)
colResizable.js自动调整表格宽度插件
PS vertical English and digital text how to change direction (vertical display)
Jerry's ad series MIDI function description [chapter]
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
解读创客教育中的各类智能化组织发展
What are the functional modules of RFID warehouse management system solution
华为模拟器ensp常用命令
maya灯建模
Huawei ENSP simulator configures ACL access control list
Huawei simulator ENSP common commands
redis事务
Redis:Redis配置文件相关配置、Redis的持久化
redis03——Redis的网络配置与心跳机制
Kubeadm初始化报错:[ERROR CRI]: container runtime is not running
创客思维在高等教育中的启迪作用
Explication détaillée du mécanisme de distribution des événements d'entrée multimodes
HWiNFO硬件检测工具v7.26绿色版
五子棋 上班摸鱼工具 可局域网/人机