android 需要在 Flutter 中实现本地启动画面2秒

v6ylcynt  于 2023-04-04  发布在  Android
关注(0)|答案(5)|浏览(170)

我用过flutter插件flutter_native_splash: ^2.2.2
我需要实现自定义图像精确启动画面2秒。我试过从插件描述的所有例子,但没有完美的工作。

flutter_native_splash:

  background_image: "assets/images/splash.png"
4nkexdtk

4nkexdtk1#

Flutter原生启动画面不可能显示精确的时间,因为启动画面是在Flutter框架加载时显示的。根据设备的处理速度,这可能需要或多或少的时间。可以想象,在旧设备上,即使您没有添加额外的延迟,它也可能持续超过两秒。

kmb7vmvb

kmb7vmvb2#

你可以在main方法中试试这个代码:

void main() async{
  await Future.delayed(const Duration(seconds: 2))
      .then((value) => FlutterNativeSplash.remove());
  runApp(const MyApp());
}
7nbnzgx9

7nbnzgx93#

我没有使用flutter启动画面,而是尝试在scaffold中添加图像

Scaffold(
        backgroundColor: Colors.white,
        body: Center(
          child: FadeInImage(
            image: AssetImage("assets/images/splash.png"),//some animation
            height: 400,
            fadeInDuration: const Duration(seconds: 1),
            placeholder: MemoryImage(kTransparentImage),//transparent_image: ^2.0.0 add this to pub.yaml file
          ),
        )
    );

//从init调用此函数

void userData() async{
        setState(() {
          Timer(
              Duration(seconds: 2),
                  ()=> );
        });
}

闪屏后要导航到的屏幕

vmjh9lq9

vmjh9lq94#

在你的主镖中载入所有东西后,你可以设置一个启动画面的计时器

void main() async {
  final widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  if (defaultTargetPlatform == TargetPlatform.iOS) {
    await SystemChrome.setEnabledSystemUIMode(
      SystemUiMode.immersiveSticky,
      overlays: [
        SystemUiOverlay.bottom,
        SystemUiOverlay.top,
      ],
    );
  }
  AppTheme.setTransparentStatusBar(
    darkIcons: false,
  );
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  await Hive.initFlutter();
  await Hive.openBox(Constants.prefs);
  await bootstrap(
    () => const App(),
  );

  /// Here after loading everything we are giving a 4 seconds delay.
  await Future.delayed(const Duration(seconds: 4), FlutterNativeSplash.remove);
}
u1ehiz5o

u1ehiz5o5#

您可以保留启动画面,以便在您将其删除之前一直显示它。
启动应用程序之前,请保留启动画面

void main() {
  // This code enables the Splash Screen to be active unless manually removed
  var widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

  runApp(const MyApp());
}

在Material应用程序定义中,在构建方法中设置一个计时器,在删除启动画面之前等待2秒

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {

    // This will keep you SplashScreen up for exactly 2 seconds
    Timer(Duration(seconds: 2), () {FlutterNativeSplash.remove(); });
    return MaterialApp(
      title: 'Coffee Rewards',
      
      home: const MyHomePage(),
    );
  }
}

相关问题