Last active
October 23, 2020 13:40
-
-
Save DanielCardonaRojas/cca43ee572b567bb712cd7d0e5d50ae0 to your computer and use it in GitHub Desktop.
Flutter DialogController #flutter
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 '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); | |
} | |
} |
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 '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