import 'package:flutter/material.dart'; import 'package:openmediacentermobile/login/login_context.dart'; import '../api/settings_api.dart'; // todo maybe instead of feature context a context for all settings? class FeatureContext extends InheritedWidget { const FeatureContext(this.tvShowEnabled, this.fullDeleteEnabled, {Key? key, required Widget child}) : super(key: key, child: child); final bool tvShowEnabled; final bool fullDeleteEnabled; static FeatureContext of(BuildContext context) { final FeatureContext? result = context.dependOnInheritedWidgetOfExactType(); assert(result != null, 'No LoginContext found in context'); return result!; } @override bool updateShouldNotify(FeatureContext old) { return tvShowEnabled != old.tvShowEnabled || fullDeleteEnabled != old.fullDeleteEnabled; } } class FeatureContainer extends StatefulWidget { const FeatureContainer({Key? key, required this.child}) : super(key: key); final Widget child; @override State createState() => _FeatureContainerState(); } class _FeatureContainerState extends State { bool? tvShowEnabled; bool? fullDeleteEnabled; bool fetcherror = false; @override void initState() { loadInitialData() .then((value) => setState( () { fullDeleteEnabled = value.fullDeleteEnabled; tvShowEnabled = value.tvShowEnabled; }, )) .catchError((err) => setState(() => fetcherror = true)); super.initState(); } @override Widget build(BuildContext context) { if (fetcherror) { final loginctx = LoginContext.of(context); return MaterialApp( home: Scaffold( body: Center( child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [ Text("Fetch error"), MaterialButton( onPressed: () => loginctx.onLoggin(false), child: Text("Logout"), ) ]), ), ), ); } else if (tvShowEnabled == null || fullDeleteEnabled == null) { return MaterialApp( home: Scaffold( body: Container( color: Colors.white, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [CircularProgressIndicator(), Text("loading features")], )), ), ), ); } else { return FeatureContext(tvShowEnabled!, fullDeleteEnabled!, child: widget.child); } } }