0

I want to redirect my current flutter page to home page when back button is pressed. When i press back button the home page/activity should start. I have multiple selections in home page and in current page i am displaying eyes if user don't want to select they can press back to go to homepage.

here is code for my current page/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),



       ),


     );
   }


}


so what could i use here so that the back pressing functionality can be achieved?

2 Answers 2

5

You can handle a back pressed event in the Flutter with help of WillPopScope widget. and you will find onWillPop method

@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"),
      ),
    ),
  );
}

Implement _onBackPressed Method with what you want to do on back press:

Future<bool> _onBackPressed() {
  return ANYTHING YOU WANT TO DO ??
      false;
}

CHECK OUT THIS BLOG FOR MORE DETAIL

Sign up to request clarification or add additional context in comments.

Comments

0

You can use WillPopScope to achieve this.

  @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"),
        ),
      ),
    );
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.