当前位置:网站首页>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,
],
),
),
);
}
);
}
边栏推荐
- TCP三次握手,四次挥手,你真的了解吗?
- 杰理之AD 系列 MIDI 功能说明【篇】
- 巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
- 面试官:说说XSS攻击是什么?
- Delphi SOAP WebService 服务器端多个 SoapDataModule 实现相同的接口方法,接口继承
- Maidong Internet won the bid of Beijing life insurance
- A quick start to fastdfs takes you three minutes to upload and download files to the ECS
- 学习突围3 - 关于精力
- 类方法和类变量的使用
- Daily question-leetcode556-next larger element iii-string-double pointer-next_ permutation
猜你喜欢
随机推荐
Can be displayed in CAD but not displayed in print
CAD中能显示打印不显示
HWiNFO硬件检测工具v7.26绿色版
maya灯建模
Why does invariant mode improve performance
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
Detailed explanation of multi-mode input event distribution mechanism
numpy vstack 和 column_stack
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Gobang go to work fishing tools can be LAN / man-machine
Y56. Chapter III kubernetes from entry to proficiency -- business image version upgrade and rollback (29)
华为ensp模拟器 配置ACL访问控制列表
[buuctf.reverse] 151_ [FlareOn6]DnsChess
测试用例 (TC)
杰理之AD 系列 MIDI 功能说明【篇】
【微信小程序】协同工作与发布
Liu Jincheng won the 2022 China e-commerce industry innovation Figure Award
奋斗正当时,城链科技战略峰会广州站圆满召开
解析互联网时代的创客教育技术
杰理之增加进关机前把触摸模块关闭流程【篇】