需要在flutter中实现Native splash screen 2秒

问题描述 投票:0回答:5

我用过flutter插件

flutter_native_splash: ^2.2.2

我需要使用自定义图像准确实现启动画面 2 秒。我尝试了插件描述中的所有示例,但效果不佳。

flutter_native_splash:

  background_image: "assets/images/splash.png"
android ios flutter flutter-layout flutter-dependencies
5个回答
0
投票

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


0
投票

您可以在 main 方法中尝试此代码:

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

0
投票

我没有使用 flutter splash screen 而是试试这个 在脚手架中添加图像

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),
                  ()=> );
        });
} 

闪屏后您想导航到的屏幕


0
投票

在主飞镖中加载所有内容后,您可以为启动画面设置计时器

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);
}

0
投票

您可以保留启动画面,以便在您将其删除之前显示它。

在启动应用程序之前,保留启动画面

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 App 定义中,在构建方法中设置一个计时器,等待 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(),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.