Last active
November 16, 2020 12:17
-
-
Save malinkaphann/d9407badd1ca30435993548d14cacec7 to your computer and use it in GitHub Desktop.
Alert Manager
This file contains 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
/* | |
* author: malinkaphann@gmail.com | |
* create an alert widget | |
* */ | |
import 'package:flutter/material.dart'; | |
final Color darkBlue = Color.fromARGB(255, 18, 32, 47); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: MyAlert(), | |
), | |
), | |
); | |
} | |
} | |
class MyWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Text('Hello, World!', style: Theme.of(context).textTheme.headline4); | |
} | |
} | |
/* | |
* SEVERITY | |
* - INFO: show an alert in normal case | |
* - ERROR: show an alert when error or crash | |
* | |
* @TODO: move this enum into utility class | |
*/ | |
enum SEVERITY { INFO, ERROR } | |
/* | |
* MyAlert is a stateful widget | |
* 1- type: INFO or ERROR | |
* 2- title | |
* 3- message | |
*/ | |
class MyAlert extends StatefulWidget { | |
@override | |
_MyAlertState createState() => _MyAlertState(); | |
} | |
/* | |
* _MyAlertState is a state for MyAlert | |
*/ | |
class _MyAlertState extends State<MyAlert> { | |
SEVERITY type; | |
String title; | |
String message; | |
@override | |
Widget build(BuildContext context) { | |
return RaisedButton( | |
onPressed: () { | |
this._showMyDialog(); | |
}, | |
child: const Text('show alert box', style: TextStyle(fontSize: 20)), | |
); | |
} | |
/* | |
* to show alert box | |
*/ | |
Future<void> _showMyDialog() async { | |
return showDialog<void>( | |
context: context, | |
barrierDismissible: false, // user must tap button! | |
builder: (BuildContext context) { | |
return AlertDialog( | |
title: Text('here is title'), | |
content: SingleChildScrollView( | |
child: ListBody( | |
children: <Widget>[ | |
Text('message is here') | |
], | |
), | |
), | |
actions: <Widget>[ | |
TextButton( | |
child: Text('button is here'), | |
onPressed: () { | |
Navigator.of(context).pop(); | |
}, | |
), | |
], | |
); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment