void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return RootWidget( child: MaterialApp( debugShowCheckedModeBanner: false, title: ‘V2EX app‘, theme: ThemeData( primarySwatch: Colors.grey, ), initialRoute: ‘/‘, routes: { ‘/‘: (context) => HomePage(), ‘/nodes‘: (context) => NodePage(), ‘/test‘: (context) => TestApp(), // ‘/web‘: (context) => WebTest(), }, ), bloc: UserBloc(), ); } }
class RootWidget extends InheritedWidget { RootWidget({this.bloc, this.child}) : super(child: child); final UserBloc bloc; final Widget child; static RootWidget of(BuildContext context) => (context).inheritFromWidgetOfExactType(RootWidget); @override bool updateShouldNotify(InheritedWidget oldWidget) { return true; } }
原文:https://www.cnblogs.com/pythonClub/p/10695598.html