Back Button of phone is not working in flutter

0

In my Flutter Application App closing when i click on back button. I used onWillpopscope() but it is not working to me. I share my code below. But when i create a sample project it is working. Can Any one explain me why it is not working in existing application. Can anyone suggest me how to achieve this.

class HomePage extends StatefulWidget {
  HomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  State<StatefulWidget> createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
  Future<bool> _onBackPressed() {
    return showDialog(
          context: context,
          builder: (context) => new AlertDialog(
                title: new Text('Are you sure?'),
                content: new Text('Do you want to exit an App'),
                actions: <Widget>[
                  new GestureDetector(
                    onTap: () => Navigator.of(context).pop(false),
                    child: roundedButton("No", const Color(0xFF167F67),
                        const Color(0xFFFFFFFF)),
                  ),
                  new GestureDetector(
                    onTap: () => Navigator.of(context).pop(true),
                    child: roundedButton(" Yes ", const Color(0xFF167F67),
                        const Color(0xFFFFFFFF)),
                  ),
                ],
              ),
        ) ??
        false;
  }

  Widget roundedButton(String buttonLabel, Color bgColor, Color textColor) {
    var loginBtn = new Container(
      padding: EdgeInsets.all(5.0),
      alignment: FractionalOffset.center,
      decoration: new BoxDecoration(
        color: bgColor,
        borderRadius: new BorderRadius.all(const Radius.circular(10.0)),
        boxShadow: <BoxShadow>[
          BoxShadow(
            color: const Color(0xFF696969),
            offset: Offset(1.0, 6.0),
            blurRadius: 0.001,
          ),
        ],
      ),
      child: Text(
        buttonLabel,
        style: new TextStyle(
            color: textColor, fontSize: 20.0, fontWeight: FontWeight.bold),
      ),
    );
    return loginBtn;
  }

  @override
  Widget build(BuildContext context) {
    return new 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"),
        ),
      ),
    );
  }
}

In Pubspec :

version: 1.0.0+1
environment:
  sdk: ">=2.0.0-dev.68.0 <3.0.0"
flutter
asked on Stack Overflow Jun 8, 2019 by kartheeki j • edited Jun 12, 2019 by kartheeki j

1 Answer

0

Change like this because child is deprecated, use builder

Future<bool> _onWillPop() {
return showDialog(
  context: context,
  builder: (context) => new AlertDialog(
    title: new Text('Are you sure?'),
    content: new Text('Unsaved data will be lost.'),
    actions: <Widget>[
      new FlatButton(
        onPressed: () => Navigator.of(context).pop(false),
        child: new Text('No'),
      ),
      new FlatButton(
        onPressed: () => Navigator.of(context).pop(true),
        child: new Text('Yes'),
      ),
    ],
  ),
) ?? false;
}
answered on Stack Overflow Jun 12, 2019 by Manoj Perumarath

User contributions licensed under CC BY-SA 3.0