import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; class ViewWithProgressIndicator extends StatefulWidget { final AsyncSnapshot snapshot; final Widget child; final double progressIndicatorSize; final EdgeInsets padding; const ViewWithProgressIndicator( {Key? key, required this.snapshot, required this.child, this.progressIndicatorSize = 100, this.padding = const EdgeInsets.all(0)}) : super(key: key); @override _ViewWithProgressIndicatorState createState() => _ViewWithProgressIndicatorState(); } class _ViewWithProgressIndicatorState extends State { @override Widget build(BuildContext context) { switch (widget.snapshot.connectionState) { case ConnectionState.none: case ConnectionState.waiting: return Container( alignment: Alignment.center, padding: widget.padding, child: Center( child: SizedBox( width: widget.progressIndicatorSize, height: widget.progressIndicatorSize, // TODO: only show if loading takes longer than 30ms child: const CircularProgressIndicator(), ), ), ); default: if (widget.snapshot.hasError) { return Center( child: Text(widget.snapshot.error.toString()), ); } if (!widget.snapshot.hasData) { return const Center( child: Text("No data"), ); } else { return widget.child; } } } }