Last active
September 2, 2021 23:07
-
-
Save DanielCardonaRojas/76e631707d2f2f62e7ed55db4e2f499f to your computer and use it in GitHub Desktop.
Dialog Utilities for Flutter pages
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
import 'dart:async'; | |
import 'package:flutter/material.dart'; | |
typedef DialogShowHandler<T> = Future<T> Function(); | |
class DialogController { | |
late DialogShowHandler _showDialog; | |
late Function _closeDialog; | |
Completer? _dialogCompleter; | |
/// Registers a callback function. Typically to show the dialog | |
void prepareDialog( | |
{required DialogShowHandler show, required Function close}) { | |
if (_dialogCompleter != null && !_dialogCompleter!.isCompleted) { | |
_closeDialog(); | |
} | |
_showDialog = show; | |
_closeDialog = close; | |
} | |
/// Calls the dialog listener and returns a Future that will wait for dialogComplete. | |
Future<T?> show<T>() { | |
final completer = Completer<T>(); | |
_dialogCompleter = completer; | |
_showDialog().then((value) { | |
if (completer.isCompleted) return; | |
completer.complete(value); | |
}); | |
return completer.future; | |
} | |
/// Completes the _dialogCompleter to resume the Future's execution call | |
void close<T>([T? value]) { | |
if (_dialogCompleter?.isCompleted ?? true) return; | |
_dialogCompleter?.complete(value); | |
_closeDialog(); | |
_dialogCompleter = null; | |
} | |
} |
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
import 'package:dialog_manager/dialog_controller.dart'; | |
import 'package:flutter/material.dart'; | |
class StyledDialogs<S> { | |
Map<String, DialogShowHandler> _styleBuilders = {}; | |
DialogController _dialogController = DialogController(); | |
void registerDialogOf( | |
{required S style, required DialogShowHandler builder}) { | |
final styleIdentifier = style.toString(); | |
_styleBuilders[styleIdentifier] = builder; | |
} | |
Future<T?> showDialogWithStyle<T>( | |
S style, { | |
required Function closingFunction, | |
}) { | |
final styleIdentifier = style.toString(); | |
final builder = _styleBuilders[styleIdentifier]; | |
if (builder == null) return Future.value(null); | |
_dialogController.prepareDialog( | |
show: builder, | |
close: closingFunction, | |
); | |
return _dialogController.show(); | |
} | |
void closeVisibleDialog<T>([T? value]) { | |
_dialogController.close(value); | |
} | |
} | |
mixin StyledDialogsMixin<S, W extends StatefulWidget> on State<W> { | |
var _styledDialogs = StyledDialogs<S>(); | |
void registerDialogOf( | |
{required S style, required DialogShowHandler builder}) => | |
_styledDialogs.registerDialogOf(style: style, builder: builder); | |
Future<T?> showDialogWithStyle<T>(S style, {Function? closingFunction}) => | |
_styledDialogs.showDialogWithStyle( | |
style, | |
closingFunction: closingFunction ?? () => Navigator.of(context).pop(), | |
); | |
void closeVisibleDialog<T>([T? value]) => | |
_styledDialogs.closeVisibleDialog(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment