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 extends StatelessWidget { final UiDesign? uiDesign; final Future future; final Widget Function(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, ); }, ); } }