Skip to content

Instantly share code, notes, and snippets.

@ddikman
Last active Oct 19, 2021
Embed
What would you like to do?
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