flutter 要求在MaterialApp里的builder写多个东西例如:插件等

多个的时候可以这么写 以同时使用flutter_easyloading 跟 bot_toast这两个全局类的插件为例子

final botToastBuilder = BotToastInit();
final easyload = EasyLoading.init();
 
MaterialApp(
   title: 'xxxx',
   builder: (context, child) {
    child = easyload(context, child);
    child = botToastBuilder(context, child);
    return child;
   }
)

  如果是同时写一个方法,比如点击空白处键盘消失

class MyApp extends StatelessWidget {

  final easyload = EasyLoading.init();

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'app名',
      theme: AppTheme.light,
      darkTheme: AppTheme.dark,
      themeMode: ThemeMode.system,
      debugShowCheckedModeBanner: false,
      initialRoute: AppRoutes.Splash,
      getPages: AppPages.routes,
      builder: (context, child){
        child = easyload(context, child);
        child = Scaffold(
            // Global GestureDetector that will dismiss the keyboard
            body: GestureDetector(
            onTap: () => hideKeyboard(context),
        child: child,
        ));
        return child;
      },
      unknownRoute: AppPages.unknownRoute,
      enableLog: true,
      logWriterCallback: Logger.write,
      initialBinding: LayoutBinding(),
    );
  }
}

void hideKeyboard(BuildContext context) {
  FocusScopeNode currentFocus = FocusScope.of(context);
  if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
    FocusManager.instance.primaryFocus.unfocus();
  }
}

  

特此记录

上一篇:什么是OOA,OOP


下一篇:flutter 主要weight学习(materialapp scaffold container text)