Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flutter DialogController #flutter
import 'dart:async';
import 'package:flutter/material.dart' as material;
import 'package:flutter/material.dart' show BuildContext, Widget, Navigator;
class DialogController {
final BuildContext context;
final Widget Function(BuildContext) builder;
Future<dynamic> value;
BuildContext _innerContext;
bool _isShowing = false;
DialogController({this.context, this.builder});
void close() {
if (!_isShowing) return;
Navigator.of(_innerContext).pop();
_isShowing = false;
}
Future awaitDismissal() async {
final result = await value;
return result;
}
void showDialog() {
_isShowing = true;
value = material.showDialog(
context: context,
builder: (ctx) {
_innerContext = ctx;
final widget = builder(ctx);
return widget;
},
);
value.whenComplete(() => _isShowing = false);
}
}
import 'dart:async';
import 'package:meta/meta.dart';
import 'dialog_controller.dart';
class DialogService {
static final DialogService _sharedInstance = DialogService._internal();
Map<String, DialogController> dialogs = {};
factory DialogService() {
return _sharedInstance;
}
DialogService._internal();
void registerDialogController(
{@required DialogController controller, @required String key}) {
dialogs[key] = controller;
}
DialogController controller(String key) {
return dialogs[key];
}
void showDialog(String key) {
final controller = dialogs[key];
return controller.showDialog();
}
Future awaitDismissal(String key) {
final controller = dialogs[key];
return controller.awaitDismissal();
}
void close(String key) {
final controller = dialogs[key];
controller.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.