showWeuiToast function

HideCallback showWeuiToast({
  1. required BuildContext context,
  2. required Widget message,
  3. required Widget icon,
  4. bool stopEvent = false,
  5. Alignment? alignment,
  6. bool backButtonClose = false,
})

Implementation

HideCallback showWeuiToast(
    {required BuildContext context,
    required Widget message,
    required Widget icon,
    bool stopEvent = false,
    Alignment? alignment,
    bool backButtonClose = false}) {

  var config = WeuiToastConfig.of(context);
  alignment = alignment ?? config.toastAlignment;

  Completer<VoidCallback> result = Completer<VoidCallback>();
  var backButtonName = 'CoolUI_WeuiToast$backButtonIndex';
  BackButtonInterceptor.add((stopDefaultButtonEvent, routeInfo){
    if(backButtonClose){
      result.future.then((hide){
        hide();
      });
    }
    return true;
  }, zIndex: backButtonIndex, name: backButtonName);
  backButtonIndex++;

  OverlayEntry? overlay = OverlayEntry(
    maintainState: true,
      builder: (_) => WillPopScope(
        onWillPop: () async {
          var hide = await result.future;
          hide();
          return false;
        },
        child: WeuiToastWidget(
            alignment: alignment!,
            icon: icon,
            message: message,
            stopEvent: stopEvent,
          ),
      ));
  result.complete((){
    if(overlay == null){
      return;
    }
    overlay!.remove();
    overlay = null;
    BackButtonInterceptor.removeByName(backButtonName);
  });
  Overlay.of(context)!.insert(overlay!);


  return () async {
    var hide = await result.future;
    hide();
  };
}