diameter/lib/components/progress_indicator.dart

57 lines
1.6 KiB
Dart
Raw Normal View History

2021-10-22 23:08:09 +00:00
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<ViewWithProgressIndicator> {
@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
2021-10-22 23:08:09 +00:00
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;
}
}
}
}