Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xh307 opened this issue May 2, 2024 · 7 comments

Comments

@xh307
Copy link

xh307 commented May 2, 2024

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
Copy link
Collaborator

  • Use child param, builder is only meant for advanced usage, like theme variables with library

@xh307
Copy link
Author

xh307 commented May 29, 2024

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

@xh307
Copy link
Author

xh307 commented May 29, 2024

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
Copy link
Collaborator

@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
Copy link
Author

xh307 commented May 30, 2024

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
Copy link
Collaborator

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

@Mounir-Bouaiche
Copy link
Collaborator

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

@xh307 xh307 closed this as completed Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants