flutter - Flutter-按下后退按钮时如何重定向到特定活动?
问题描述
当按下后退按钮时,我想将当前的颤动页面重定向到主页。当我按下返回按钮时,主页/活动应该开始。我在主页中有多个选择,在当前页面中,如果用户不想选择,我会显示眼睛,他们可以按返回返回主页。
这是我当前页面/活动的代码
import '../DataFetching/face_part.dart';
import 'package:flutter/material.dart';
class EyesFetch extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_DataFetching createState() => _DataFetching();
}
class _DataFetching extends State<MyHomePage>{
Future<List<facial>> list = facial.alleyes();
List<facial> alleyes=new List<facial>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("fetch"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new FutureBuilder<List<facial>>(
future: list, // a previously-obtained Future<String> or null
builder: (BuildContext context, AsyncSnapshot<List<facial>> snapshot) {
List<Widget> children;
if (snapshot.hasData) {
var result=snapshot.data;
for(var item in result)
{
alleyes.add(new facial(criminal_id: item.criminal_id,
part_size: item.part_size,
link: item.link));
}
children = <Widget>[
new Padding(
padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 0.0),
child: getHomePageBody(context))
];
} else if (snapshot.hasError) {
children = <Widget>[
Icon(
Icons.error_outline,
color: Colors.red,
size: 60,
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Text('Error: ${snapshot.error}'),
)
];
} else {
children = <Widget>[
SizedBox(
child: CircularProgressIndicator(),
width: 60,
height: 60,
),
];
}
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
),
);
},
),
]// This trailing comma makes auto-formatting nicer for build methods.
)));
}
getHomePageBody(BuildContext context) {
final _screenSize = MediaQuery.of(context).size;
return Container(
height: _screenSize.height * 0.85,
child:
new GridView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: alleyes.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount (crossAxisCount: 3),
itemBuilder: (context,index){
return _getItemUI(context,index);
}));
}
Widget _getItemUI(BuildContext context, int index) {
return Container(
child: Card(
child:
new Image.network(alleyes[index].link, fit: BoxFit.cover),
),
);
}
}
那么我可以在这里使用什么来实现背压功能?
解决方案
您可以在WillPopScope小部件的帮助下处理 Flutter 中的后按事件。你会找到onWillPop
方法
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _onBackPressed,
child: new Scaffold(
appBar: new AppBar(
title: new Text(
"On Back pressed",
style: new TextStyle(color: Colors.white),
),
),
body: new Center(
child: new Text("Home Page"),
),
),
);
}
实现 _onBackPressed 方法,使用您想要在后按时执行的操作:
Future<bool> _onBackPressed() {
return ANYTHING YOU WANT TO DO ??
false;
}
推荐阅读
- c# - 我的问题是关于比较列表
带字典 > 使用 C# linq - angularjs - highstock 股票图表标志未存储在系列数据中&&点击弹出窗口未出现&& 2个标志呈现
- python - Numpy 导入失败
- python - 试图在两个日期列之间进行差异
- amazon-s3 - 从源访问 XMLHttpRequest 已被 CORS 策略阻止:请求中不存在“Access-Control-Allow-Origin”标头
- azure - 如何确保消息将在队列中等待 10 分钟
- java - 如何通过 mongoDb 中的 spring 数据进行 serverStatus() 查询
- c# - SilverLight 5 和 VS2019 - 缺少程序集参考?
- excel - 如果满足条件,则显示下拉列表
- firebase-cloud-messaging - FirebaseMessagingException:调用 FCM 后端服务时出错