Example of floating loader overlay
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: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