Skip to content

Instantly share code, notes, and snippets.

@GursheeshSingh
Created August 7, 2020 20:44
Show Gist options
  • Save GursheeshSingh/b94df59b1434dcebce9178eac38102c3 to your computer and use it in GitHub Desktop.
Save GursheeshSingh/b94df59b1434dcebce9178eac38102c3 to your computer and use it in GitHub Desktop.
class SecondScreen extends StatefulWidget {
@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
final TextEditingController _controller = TextEditingController();
String savedText = "";
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
if (_controller.text == savedText) {
return true;
}
final result = await showDialog(
context: context,
child: AlertDialog(
title: Text("Are you sure?"),
content: Text("All unsaved changes would be lost"),
actions: <Widget>[
FlatButton(
child: Text('No'),
onPressed: () {
Navigator.pop(context, false);
},
),
FlatButton(
child: Text('Yes', style: TextStyle(color: Colors.red)),
onPressed: () {
Navigator.pop(context, true);
},
),
],
),
);
return result;
},
child: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(),
body: Container(
margin: EdgeInsets.symmetric(horizontal: 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("Saved text: $savedText"),
SizedBox(height: 16),
TextField(controller: _controller),
SizedBox(height: 16),
RaisedButton(
child: Text("Save"),
onPressed: () {
setState(() {
savedText = _controller.text;
});
},
),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment