Skip to content

Instantly share code, notes, and snippets.

@ravimittal16
Last active April 27, 2020 18:31
Show Gist options
  • Save ravimittal16/4e742d2778c1ab47bc510303d92c8a83 to your computer and use it in GitHub Desktop.
Save ravimittal16/4e742d2778c1ab47bc510303d92c8a83 to your computer and use it in GitHub Desktop.
Simple Flutter Dialog Wrapper
//======================================
// This will be used to show the dialog
// =====================================
import 'dart:async';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
class DialogService {
Completer<DialogResponse> _dialogCompleter;
BuildContext context;
void registerContext(BuildContext context) {
this.context = context;
}
//======================================
// Show the confirmation box
// =====================================
Future<DialogResponse<bool>> showConfirmation(DialogRequest request) {
_dialogCompleter = Completer<DialogResponse<bool>>();
if (context != null) {
//======================================
// dialog actions
// =====================================
List<Widget> actions = <Widget>[
FlatButton(
child: Text(request.okButtonTitle),
onPressed: () {
Navigator.of(context).pop();
dialogComplete<bool>(new DialogResponse<bool>(
response: true, additionalNote: "Okay button clicked"));
},
),
FlatButton(
child: Text(request.cancelButtonTitle),
onPressed: () {
Navigator.of(context).pop();
dialogComplete<bool>(new DialogResponse<bool>(
response: false, additionalNote: "Cancel button clicked"));
},
),
];
//======================================
// dialog content
// =====================================
Widget _content = SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(request.description),
],
),
);
//======================================
// dialog title
// =====================================
Widget _title = Text(request.title);
showDialog<bool>(
context: this.context,
barrierDismissible: false,
builder: (BuildContext context) {
return Platform.isIOS
? CupertinoAlertDialog(
title: _title, content: _content, actions: actions)
: AlertDialog(
title: _title, content: _content, actions: actions);
});
}
return _dialogCompleter.future;
}
void dialogComplete<TKey>(DialogResponse<TKey> response) {
_dialogCompleter.complete(response);
_dialogCompleter = null;
}
deregisterContext() {
this.context = null;
}
}
class DialogRequest {
final String title;
final String description;
final String okButtonTitle;
final String cancelButtonTitle;
DialogRequest({
this.cancelButtonTitle = "Cancel",
this.title,
this.description,
this.okButtonTitle = "Okay",
});
}
class DialogResponse<T> {
final String additionalNote;
final T response;
DialogResponse({
this.additionalNote,
this.response,
});
}
class NewWidget extends StatefulWidget {
}
class _NewWidget extends State<NewWidget> {
DialogService dialogService = locator.get<DialogService>();
@override
void dispose() {
dialogService.deregisterContext();
}
@override
Widget build(BuildContext context) {
dialogService.registerContext(context);
return WillPopScope(
onWillPop: () {
return dialogService
.showConfirmation(new DialogRequest(
title: "Confirmation", description: "Are you sure?"))
.then((response) {
return Future.value(response.response);
});
},
child:Text("Hello World"),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment