Flutter-按下后退按钮时如何重定向到特定活动?

问题描述 投票:0回答:2

我想重定向我当前的浮动页面到主页按下后退按钮时。当我按返回按钮时,主页/活动应开始。我在主页中有多个选择,在当前页面中,如果用户不想选择它们,我会显示眼睛,他们可以按返回首页。

这是我当前页面/活动的代码



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),



       ),


     );
   }


}


所以我在这里可以使用什么来实现后按功能?

flutter dart
2个回答
0
投票

您可以使用WillPopScope实现这一目标。

  @override
  Widget build(BuildContext context) {
    return new WillPopScope(
      onWillPop: ()  {
       // your logic here
   },
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text("Home Page"),
        ),
        body: new Center(
          child: new Text("Home Page"),
        ),
      ),
    );
  }

0
投票

您可以在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

© www.soinside.com 2019 - 2024. All rights reserved.