Skip to content

Instantly share code, notes, and snippets.

@mono0926
Created April 18, 2019 09:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0926/49d43958933964ea01887f632a504ee1 to your computer and use it in GitHub Desktop.
Save mono0926/49d43958933964ea01887f632a504ee1 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
class HUD {
BuildContext hudContext;
void show(BuildContext context) {
showGeneralDialog(
context: context,
pageBuilder: (hudContext, _a, _sa) {
this.hudContext = hudContext;
return const Center(child: CircularProgressIndicator());
},
barrierDismissible: false,
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
barrierColor: Colors.black26,
transitionDuration: const Duration(milliseconds: 150),
transitionBuilder: (_c, animation, _sa, child) => FadeTransition(
opacity: CurvedAnimation(
parent: animation,
curve: Curves.easeOut,
),
child: child,
),
);
}
void dismiss() {
if (hudContext == null) {
assert(false, 'hudContext should not be null.');
return;
}
Navigator.of(hudContext)?.pop();
}
}
@mono0926
Copy link
Author

Usage

final hud = HUD()..show(context);
Future.delayed(Duration(seconds: 2)).then((_) {
  // HUDが閉じる
  hud.dismiss();
  Future.delayed(Duration(seconds: 2)).then((_) {
    // 何も起きない
    hud.dismiss();
  });
});

@mono0926
Copy link
Author

2019-04-18_18-26-21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment