Last active
October 28, 2020 03:24
-
-
Save tranductam2802/5ad70b881ba582c93764e5d094d270b8 to your computer and use it in GitHub Desktop.
Demo the basic action with dialog. (cancelOnTouchOutside, dismiss listener, call GUI function after dismiss)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Tutorial by Tran Duc Tam', | |
home: ProcessPage(), | |
); | |
} | |
} | |
class ProcessPage extends StatefulWidget { | |
const ProcessPage(); | |
@override | |
_ProcessPageState createState() => _ProcessPageState(); | |
} | |
class _ProcessPageState extends State<ProcessPage> { | |
final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>(); | |
DateTime startTime; | |
Future performStart(BuildContext context) async { | |
startTime = DateTime.now(); | |
Future.value(start(context)).then((value) { | |
Navigator.pop(context, value); | |
}); | |
return await showDialog( | |
context: context, | |
builder: loadingDialogBuilder, | |
barrierDismissible: false, | |
); | |
} | |
Future<bool> start(BuildContext context) async { | |
// TODO: Do something | |
await Future.delayed(Duration(seconds: 2)); | |
return true; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
key: scaffoldKey, | |
appBar: AppBar(title: Text('')), | |
body: RaisedButton( | |
child: Text('LOAD DATA'), | |
onPressed: () => performStart(context).then((returnVal) { | |
// On dismiss | |
if (startTime == null || returnVal == null) { | |
final snackBar = SnackBar(content: Text('Invalid action')); | |
scaffoldKey.currentState.showSnackBar(snackBar); | |
} else { | |
final Duration detal = DateTime.now().difference(startTime); | |
final snackBar = SnackBar( | |
content: Text('Process status "$returnVal" after: $detal'), | |
duration: Duration(seconds: 5), | |
); | |
scaffoldKey.currentState.showSnackBar(snackBar); | |
} | |
})), | |
); | |
} | |
Function(BuildContext) loadingDialogBuilder = | |
(BuildContext context) => AlertDialog( | |
content: Row( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
Container( | |
height: 20.0, | |
width: 20.0, | |
child: CircularProgressIndicator(strokeWidth: 2.0), | |
), | |
Padding(padding: EdgeInsets.all(20.0), child: Text('LOADING')), | |
], | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment