Home > Back-end >  Flutter returns Main.dart screen instead of closing the Application
Flutter returns Main.dart screen instead of closing the Application

Time:10-16

I am new in flutter and I got an error that is- Basically ,I have two Screens one is login screen that is my main.dart file and second is home screen .So I just tried to implement "double tap back button to exit" on my home screen .For that I am used "WillPopScope" but when I click on back button flutter Throws me on my login screen instead of closing the application.

here my code -

home screen -

eturn Scaffold( bottomNavigationBar: bottomnav(), body: WillPopScope( onWillPop: () async { final difference = DateTime.now().difference(timeBackPressed); final isExitWarning = difference >= Duration(seconds: 2); timeBackPressed = DateTime.now();

      if (isExitWarning) {
        final message = "Press Back Again To Exit";
        Fluttertoast.showToast(
            msg: message,
            fontSize: 18,
            backgroundColor: Colors.white,
            textColor: Colors.black.withOpacity(0.95));
        return false;
      } else {
        Fluttertoast.cancel();
        return true;
      }
    },
    child: SingleChildScrollView(
        reverse: false,
        scrollDirection: Axis.vertical,
        child: Container(
            width: double.infinity,
            //height: MediaQuery.of(context).size.height,
            child: Column(children: <Widget>[
              Padding(
                padding: EdgeInsets.only(
                    top: 30, bottom: 12, left: 20, right: 0),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text("Hello! Sudeep",
                            style: TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.w800,
                                color: Color(0xfffef9e7),
                                fontFamily: 'Lato')),
                        Text("Arya Nagar,Bhind",
                            style: TextStyle(
                                fontSize: 12,
                                fontWeight: FontWeight.w600,
                                fontFamily: 'Lato',
                                color: Color(0xff808b96)))
                      ]),
                ),
              ),
              Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Container(
                      width: MediaQuery.of(context).size.width / 1.45,
                      height: 50,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(8.0),
                        color: Color(0xff17202a),
                      ),
                      child: TextField(
                        style: GoogleFonts.inter(
                          color: Color(0xff808b96),
                          fontSize: 19,
                          //fontFamily: 'Lato',
                        ),
                        cursorColor: Color(0xff808b96),
                        decoration: InputDecoration(
                            filled: true,
                            fillColor: Color(0xff212121),
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(11.0),
                              borderSide: BorderSide(
                                  style: BorderStyle.none, width: 0),
                            ),
                            hintText: "Search Here",
                            hintStyle: TextStyle(
                                fontSize: 18,
                                color: Color(0xff808b96),
                                fontWeight: FontWeight.w400),
                            prefixIcon: Icon(Icons.search,
                                color: Color(0xff808b96), size: 30)),
                      ),
                    ),
                    Container(
                        width: MediaQuery.of(context).size.width / 5.9,
                        height: 56.0,
                        padding: EdgeInsets.only(
                            top: 1.6, bottom: 0, right: 12.0, left: 5.6),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(15.0),
                          color: Color(0xff212121),
                        ),
                        child: IconButton(
                            onPressed: () {},
                            icon: Icon(Icons.qr_code,
                                color: Color(0xff808b96), size: 39.0))),
                  ]),
              SizedBox(height: 30.0),
              Container(
                width: 364,
                height: 200,
                child: ListView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: itemimg.length,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: EdgeInsets.only(right: 22, left: 6),
                        child: Card(
                            clipBehavior: Clip.antiAlias,
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(22)),
                            child: itemimg[index]),
                      );
                    }),
              ),
              SizedBox(height: 29),
              Visibility(
                  child: Column(children: [
                Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: EdgeInsets.only(
                            left: 29, right: 10, bottom: 14),
                        child: Icon(Icons.thumb_up_alt_outlined,
                            color: Colors.blue),
                      ),
                      Text("Top Picks for u",
                          style: GoogleFonts.inter(
                            fontSize: 20,
                            fontWeight: FontWeight.w800,
                            color: Color(0xff808b96),
                          )),
                    ]),
                Padding(
                  padding: EdgeInsets.only(left: 20),
                  child: Container(
                    height: 200,
                    child: ListView.separated(
                        clipBehavior: Clip.antiAlias,
                        shrinkWrap: true,
                        scrollDirection: Axis.horizontal,
                        separatorBuilder: (context, _) =>
                            SizedBox(width: 10),
                        itemCount: reslst.length,
                        itemBuilder: (context, index) {
                          return Card(
                              color: Colors.transparent,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(18.0),
                              ),
                              child: Container(
                                  width: 180.0,
                                  decoration: BoxDecoration(
                                    borderRadius:
                                        BorderRadius.circular(18.0),
                                  ),
                                  child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.start,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        InkWell(
                                            onTap: () {
                                              Navigator.push(
                                                  context,
                                                  MaterialPageRoute(
                                                    builder: (context) =>
                                                        menu(),
                                                  ));
                                            },
                                            child: ClipRRect(
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        18.0),
                                                child: reslst[index])),
                                        Padding(
                                          padding: EdgeInsets.only(
                                              left: 10, top: 7),
                                          child: Text(txtlst[index],
                                              style: GoogleFonts.inter(
                                                  fontSize: 17,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff263238))),
                                        ),
                                        Row(children: [
                                          Padding(
                                            padding: EdgeInsets.only(
                                                top: 5,
                                                bottom: 5,
                                                left: 10,
                                                right: 3.5),
                                            child: Icon(Icons.grade,
                                                size: 15,
                                                color: Colors.green),
                                          ),
                                          Text("5.0",
                                              style: GoogleFonts.inter(
                                                  fontSize: 14,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff424242))),
                                        ]),
                                        Row(children: [
                                          Padding(
                                            padding: EdgeInsets.only(
                                                top: 5,
                                                bottom: 5,
                                                left: 10,
                                                right: 3.5),
                                            child: Icon(Icons.timer,
                                                size: 15,
                                                color: Colors.orange),
                                          ),
                                          Text("45 mins",
                                              style: GoogleFonts.inter(
                                                  fontSize: 14,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff424242))),
                                        ]),
                                      ])));
                        }),
                  ),
                )
              ])),
              SizedBox(height: 25),
              Visibility(
                  child: Column(children: [
                Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: EdgeInsets.only(
                            left: 29, right: 10, bottom: 14),
                        child: Icon(Icons.timer, color: Colors.orange),
                      ),
                      Text("Less Waiting Time",
                          style: GoogleFonts.inter(
                            fontSize: 20,
                            fontWeight: FontWeight.w800,
                            color: Color(0xff808b96),
                          )),
                    ]),
                Padding(
                  padding: EdgeInsets.only(left: 20),
                  child: Container(
                    height: 200,
                    child: ListView.separated(
                        clipBehavior: Clip.antiAlias,
                        shrinkWrap: true,
                        scrollDirection: Axis.horizontal,
                        separatorBuilder: (context, _) =>
                            SizedBox(width: 10),
                        itemCount: reslst.length,
                        itemBuilder: (context, index) {
                          return Card(
                              color: Colors.transparent,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(18.0),
                              ),
                              child: Container(
                                  width: 180.0,
                                  decoration: BoxDecoration(
                                    borderRadius:
                                        BorderRadius.circular(18.0),
                                  ),
                                  child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.start,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        InkWell(
                                            onTap: () {
                                              debugPrint("clicked");
                                            },
                                            child: ClipRRect(
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        18.0),
                                                child: reslst[index])),
                                        Padding(
                                          padding: EdgeInsets.only(
                                              left: 10, top: 7),
                                          child: Text(txtlst[index],
                                              style: GoogleFonts.inter(
                                                  fontSize: 17,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff263238))),
                                        ),
                                        Row(children: [
                                          Padding(
                                            padding: EdgeInsets.only(
                                                top: 5,
                                                bottom: 5,
                                                left: 10,
                                                right: 3.5),
                                            child: Icon(Icons.grade,
                                                size: 15,
                                                color: Colors.green),
                                          ),
                                          Text("5.0",
                                              style: GoogleFonts.inter(
                                                  fontSize: 14,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff424242))),
                                        ]),
                                        Row(children: [
                                          Padding(
                                            padding: EdgeInsets.only(
                                                top: 5,
                                                bottom: 5,
                                                left: 10,
                                                right: 3.5),
                                            child: Icon(Icons.timer,
                                                size: 15,
                                                color: Colors.orange),
                                          ),
                                          Text("45 mins",
                                              style: GoogleFonts.inter(
                                                  fontSize: 14,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff424242))),
                                        ]),
                                      ])));
                        }),
                  ),
                )
              ])),
              SizedBox(height: 25),
              Visibility(
                  child: Column(children: [
                Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Padding(
                        padding: EdgeInsets.only(
                            left: 29, right: 10, bottom: 14),
                        child:
                            Icon(Icons.grade_outlined, color: Colors.green),
                      ),
                      Text("Top Rated",
                          style: GoogleFonts.inter(
                            fontSize: 20,
                            fontWeight: FontWeight.w800,
                            color: Color(0xff808b96),
                          )),
                    ]),
                Padding(
                  padding: EdgeInsets.only(left: 20),
                  child: Container(
                    height: 200,
                    child: ListView.separated(
                        clipBehavior: Clip.antiAlias,
                        shrinkWrap: true,
                        scrollDirection: Axis.horizontal,
                        separatorBuilder: (context, _) =>
                            SizedBox(width: 10),
                        itemCount: reslst.length,
                        itemBuilder: (context, index) {
                          return Card(
                              color: Colors.transparent,
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(18.0),
                              ),
                              child: Container(
                                  width: 180.0,
                                  decoration: BoxDecoration(
                                    borderRadius:
                                        BorderRadius.circular(18.0),
                                  ),
                                  child: Column(
                                      mainAxisAlignment:
                                          MainAxisAlignment.start,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        InkWell(
                                            onTap: () {
                                              debugPrint("clicked");
                                            },
                                            child: ClipRRect(
                                                borderRadius:
                                                    BorderRadius.circular(
                                                        18.0),
                                                child: reslst[index])),
                                        Padding(
                                          padding: EdgeInsets.only(
                                              left: 10, top: 7),
                                          child: Text(txtlst[index],
                                              style: GoogleFonts.inter(
                                                  fontSize: 17,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff263238))),
                                        ),
                                        Row(children: [
                                          Padding(
                                            padding: EdgeInsets.only(
                                                top: 5,
                                                bottom: 5,
                                                left: 10,
                                                right: 3.5),
                                            child: Icon(Icons.grade,
                                                size: 15,
                                                color: Colors.green),
                                          ),
                                          Text("5.0",
                                              style: GoogleFonts.inter(
                                                  fontSize: 14,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff424242))),
                                        ]),
                                        Row(children: [
                                          Padding(
                                            padding: EdgeInsets.only(
                                                top: 5,
                                                bottom: 5,
                                                left: 10,
                                                right: 3.5),
                                            child: Icon(Icons.timer,
                                                size: 15,
                                                color: Colors.orange),
                                          ),
                                          Text("45 mins",
                                              style: GoogleFonts.inter(
                                                  fontSize: 14,
                                                  fontWeight:
                                                      FontWeight.w600,
                                                  color:
                                                      Color(0xff424242))),
                                        ]),
                                      ])));
                        }),
                  ),
                )
              ])),  ));
            

CodePudding user response:

Use

   Navigator.pushReplacement(context, MaterialPageRoute(builder: 
(context)=>HomeScreen()));

to Route from LoginScreen to HomeScreen, It disposes the previous route after routing.

CodePudding user response:

As I understand you have the Login screen and Home screen, you want to close the application when the user is on the Home screen, not return them to the Login screen.

You can solve it by calling the function below when double-clicking.

SystemNavigator.pop();

An example with AlertDialog:

onWillPop: () {
  return showDialog(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Watch out!', style: TextStyle(color: Colors.black)),
        content: Text('Do you really want to leave?', style: TextStyle(color: Colors.black)),
        actions: <Widget>[
          TextButton(
            child: Text('Yes!'),
            onPressed: () {SystemNavigator.pop();}
          ),
          TextButton(
            child: Text('No!'),
            onPressed: () {Navigator.of(context).pop();}
          )
        ]
      );
    }
  );
}
  • Related