You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

42 lines
1.5 KiB

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:info_tren/models.dart';
import 'package:info_tren/utils/default_ui_design.dart';
class FutureDisplay<T> extends StatelessWidget {
final UiDesign? uiDesign;
final Future<T> future;
final Widget Function<T>(BuildContext context, T data) builder;
final Widget Function(BuildContext context, Object error, StackTrace? st)? errorBuilder;
const FutureDisplay({Key? key, required this.future, required this.builder, this.errorBuilder, this.uiDesign}): super(key: key);
@override
Widget build(BuildContext context) {
final uiDesign = this.uiDesign ?? defaultUiDesign;
return FutureBuilder(
future: future,
builder: (context, snapshot) {
if (snapshot.hasData) return builder(context, snapshot.data);
if (snapshot.hasError) return (errorBuilder != null ? errorBuilder!(context, snapshot.error!, snapshot.stackTrace) : throw snapshot.error!);
if (snapshot.connectionState == ConnectionState.done) return Container();
Widget loadingWidget;
switch (uiDesign) {
case UiDesign.MATERIAL:
loadingWidget = const CircularProgressIndicator();
break;
case UiDesign.CUPERTINO:
loadingWidget = const CupertinoActivityIndicator();
break;
default:
throw UnmatchedUiDesignException(uiDesign);
}
return Center(
child: loadingWidget,
);
},
);
}
}