gpt4 book ai didi

flutter - Flutter-如何在按下“后退”按钮时重定向到特定事件?

转载 作者:行者123 更新时间:2023-12-03 04:10:59 27 4
gpt4 key购买 nike

我想在按下返回按钮时将我当前的 flutter 页面重定向到主页,将重定向。当我按返回按钮时,主页/ Activity 应开始。我在主页中有多种选择,在当前页面中,如果用户不想选择它们,我可以显示眼睛,可以按返回首页。

这是我当前页面/ Activity 的代码



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;
}

CHECK OUT THIS BLOG FOR MORE DETAIL

关于flutter - Flutter-如何在按下“后退”按钮时重定向到特定事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62383859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com