OpenFlutter / flutter_screenutil

Flutter screen adaptation, font adaptation, get screen information
https://pub.dartlang.org/packages/flutter_screenutil
Apache License 2.0
3.83k stars 485 forks source link

关于我使用ScreenUtilInit这种方式初始化,builder会调用两次,导致跳转页面会多生成一个 #563

Closed xh307 closed 1 month ago

xh307 commented 2 months ago
iShot_2024-05-02_22 52 42

1:flutter version:3.19.6 2:flutter_screenutil version: 5.9.0 3:使用方式是全局的方式(ScreenUtilInit):看上面图片 4:现在的问题,每次启动应用都有build两次,导致会生成两个page的问题,请问有什么方式可以解决吗

Mounir-Bouaiche commented 1 month ago
xh307 commented 1 month ago

i moved MaterialApp Widget to child label,initialRoute also build twice

xh307 commented 1 month ago

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

Mounir-Bouaiche commented 1 month ago

@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.

xh307 commented 1 month ago

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

Mounir-Bouaiche commented 1 month ago

@xh307 Is your code uploaded somewhere ? Github? Can you share with me. I need to run the project so I can debug.

Mounir-Bouaiche commented 1 month ago

@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.