InheritedWidget
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
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; } }

更多精彩