Skip to content

Instantly share code, notes, and snippets.

@ddikman
Last active October 19, 2021 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddikman/f7c7bd4da6fb16af4b8d9e39f31b683d to your computer and use it in GitHub Desktop.
Save ddikman/f7c7bd4da6fb16af4b8d9e39f31b683d to your computer and use it in GitHub Desktop.
Example of floating loader overlay
import 'package:flutter/material.dart';
void main() => runApp(ExampleApp());
class ExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Floating overlay example',
debugShowCheckedModeBanner: false,
home: ExampleMainWidget(),
);
}
}
class ExampleMainWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final overlay = LoadingOverlay.of(context);
return Scaffold(
body: Center(
child: TextButton(
child: const Text('Press me!'),
onPressed: () async {
await overlay
.during(Future.delayed(const Duration(seconds: 2)));
})));
}
}
class LoadingOverlay {
BuildContext _context;
void hide() {
Navigator.of(_context).pop();
}
void show() {
showDialog(
context: _context,
barrierDismissible: false,
builder: (ctx) => _FullScreenLoader());
}
Future<T> during<T>(Future<T> future) {
show();
return future.whenComplete(() => hide());
}
LoadingOverlay._create(this._context);
factory LoadingOverlay.of(BuildContext context) {
return LoadingOverlay._create(context);
}
}
class _FullScreenLoader extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(color: Color.fromRGBO(0, 0, 0, 0.5)),
child: const Center(child: CircularProgressIndicator()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment