Skip to content

Instantly share code, notes, and snippets.

@lukepighetti
Last active March 8, 2020 01:31
Show Gist options
  • Select an option

  • Save lukepighetti/bebfe371f33c5480c1e02ca80e90953b to your computer and use it in GitHub Desktop.

Select an option

Save lukepighetti/bebfe371f33c5480c1e02ca80e90953b to your computer and use it in GitHub Desktop.
A declarative™ way to handle loading notifications with Widgets™
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:material_theme/loading_notification/loading_image.dart';
import 'package:material_theme/loading_notification/loading_notification_listener.dart';
import 'package:material_theme/widgets/layout/default_scaffold.dart';
import 'loading_future_builder.dart';
class LoadingNotificationsRoute extends StatefulWidget {
@override
_LoadingNotificationsRouteState createState() =>
_LoadingNotificationsRouteState();
}
class _LoadingNotificationsRouteState extends State<LoadingNotificationsRoute> {
final _groupOne = {
"1000": Duration(milliseconds: 1000),
"1250": Duration(milliseconds: 1250),
"1500": Duration(milliseconds: 1500),
"1750": Duration(milliseconds: 1750),
};
bool _isLoading = true;
void _handleLoaded() {
print("Loaded!");
setState(() {
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return DefaultScaffold(
"/loading-notifications",
child: LoadingNotificationListener(
onLoaded: _handleLoaded,
child: Column(
children: [
Text(_isLoading ? "All still loading..." : "All done!"),
SizedBox(height: 16),
/// Group One
LoadingNotificationListener(
key: Key("Group 1"),
child: Column(
children: <Widget>[
Text("FutureBuilders"),
..._groupOne.entries.map<Widget>((e) {
return LoadingFutureBuilder<String>(
key: Key(e.key),
future:
Future<String>.delayed(e.value).then((_) => e.key),
initialData: "Loading...",
builder: (context, snap) {
return Text(snap.data ?? "Wat");
},
);
})
],
),
),
SizedBox(height: 32),
/// Images
LoadingNotificationListener(
key: Key("Images"),
child: Column(
children: <Widget>[
Text("Images"),
LoadingImage(
key: Key("Image 1"),
image: NetworkImage(
"https://upload.wikimedia.org/wikipedia/commons/4/4e/Pleiades_large.jpg",
),
),
LoadingImage(
key: Key("Image 2"),
image: NetworkImage(
"https://upload.wikimedia.org/wikipedia/commons/3/3d/LARGE_elevation.jpg",
),
),
],
),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment