Closed xh307 closed 1 month ago
child
param, builder
is only meant for advanced usage, like theme variables with libraryi moved MaterialApp Widget to child label,initialRoute also build twice
code below:
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(750, 1624),
child: MaterialApp(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate
],
supportedLocales: const [
Locale('zh'),
],
onGenerateRoute: (settings){
String? pageName = settings.name;
final Function pageBuilder = LunarPages().pageMap['$pageName'] as Function;
if(settings.arguments != null){
return CupertinoPageRoute(
builder: (context) =>
pageBuilder(context, arguments: settings.arguments));
}else{
return CupertinoPageRoute(
builder: (context) => pageBuilder(context),
settings: settings
);
}
},
initialRoute: 'loading',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
// home: child,
builder: (context, child){
//不跟随系统字体
return MediaQuery(data: MediaQuery.of(context).copyWith(textScaler: const TextScaler.linear(1.0)),
child: easyLoading(context, child));
},
),
);
}
}
leak to build twice, how to fix it, thx
@xh307 Which builder called twice ? I can't run your code to test. Please provide a minimal reproduction code so I can debug (fresh project with only flutter_screenutil as dependency). This will help also to ensure that this bug is raised from this package.
main code:
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(750, 1624),
child: MaterialApp(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate
],
supportedLocales: const [
Locale('zh'),
],
onGenerateRoute: (settings){
String? pageName = settings.name;
final Function pageBuilder = LunarPages().pageMap['$pageName'] as Function;
if(settings.arguments != null){
return CupertinoPageRoute(
builder: (context) =>
pageBuilder(context, arguments: settings.arguments));
}else{
return CupertinoPageRoute(
builder: (context) => pageBuilder(context),
settings: settings
);
}
},
initialRoute: 'loading',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
// home: child,
builder: (context, child){
//不跟随系统字体
return MediaQuery(data: MediaQuery.of(context).copyWith(textScaler: const TextScaler.linear(1.0)),
child: easyLoading(context, child));
},
),
);
}
}
LunarLoadingView.dart (loading route)
class LunarLoadingView extends StatelessWidget {
const LunarLoadingView({super.key});
@override
Widget build(BuildContext context) {
// todo execute build twice
return Container();
}
}
@Mounir-Bouaiche LunarLoadingView named loading is initialRoute, if i used ScreenUtilInit, The build method of LunarLoadingView will be executed twice
@xh307 Is your code uploaded somewhere ? Github? Can you share with me. I need to run the project so I can debug.
@xh307 However, for any code you want to be sure it run once, you sould use initState of StatefulWidget. This ensure your code run once even if widget get replaced by this library, flutter widget, or any other library.
1:flutter version:3.19.6 2:flutter_screenutil version: 5.9.0 3:使用方式是全局的方式(ScreenUtilInit):看上面图片 4:现在的问题,每次启动应用都有build两次,导致会生成两个page的问题,请问有什么方式可以解决吗