Skip to content

Instantly share code, notes, and snippets.

@shihaohong
Created June 12, 2019 16:27
Show Gist options
  • Save shihaohong/5a1ac94c33d4237e96320d05d0889530 to your computer and use it in GitHub Desktop.
Save shihaohong/5a1ac94c33d4237e96320d05d0889530 to your computer and use it in GitHub Desktop.
Tracking whether dialog is open
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Tracking Dialog'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool isDialogShown = false;
Future<void> _neverSatisfied() async {
setState(() { isDialogShown = true; });
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Rewind and remember'),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('You will never be satisfied.'),
Text('You\’re like me. I’m never satisfied.'),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Regret'),
onPressed: () {
setState(() { isDialogShown = false; });
Navigator.of(context).pop();
},
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
print(isDialogShown);
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
floatingActionButton: FloatingActionButton(
onPressed: () {
_neverSatisfied();
},
child: Icon(Icons.add_alert),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment