Flutter 如何仅在加载颤振数据时显示启动屏幕

发布于 2025-01-09 13:26:22 字数 1442 浏览 1 评论 0原文

当应用程序的启动屏幕显示时,它需要从 FTP 服务器下载文件并处理数据。实现了 flutter 的启动屏幕

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
      future: Future.delayed(Duration(seconds: 3)),
      builder: (BuildContext context, AsyncSnapshot snapshot){
        if(snapshot.connectionState == ConnectionState.waiting)
          return SplashUI();    ///Splash Screen
        else
          return MainUI();       ///Main Screen
      },
    );
  }
}

现在,延迟 3 秒,启动屏幕显示 3 秒,在此期间从 FTP 下载文件并处理数据。我想保留启动屏幕直到数据处理完成而不是指定的时间。

启动画面


Widget _splashUI(Size size){
    return SafeArea(
      child: Center(
        child: Container(
          width: size.width * 0.5,
          height: size.height * 0.1,
          child: Image(
            fit: BoxFit.fill,
            image: AssetImage('assets/images/elf_logo.png'),
          ),
        ),
      ),
    );
  }

 Widget build(BuildContext context) {

 getFtpFile();
 dataProgress();

 return Platform.isAndroid ?
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: _splashUI(_size),
      ),
    ) :
    CupertinoApp(
      debugShowCheckedModeBanner: false,
      home: CupertinoPageScaffold(
        child: _splashUI(_size),
      ),
    );
 }

我想知道如何在处理数据时保留启动画面,而不是延迟处理启动画面。谢谢。

While the app's splash screen is displayed, it needs to download files from the FTP server and process data. Implemented splash screen for flutter

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
      future: Future.delayed(Duration(seconds: 3)),
      builder: (BuildContext context, AsyncSnapshot snapshot){
        if(snapshot.connectionState == ConnectionState.waiting)
          return SplashUI();    ///Splash Screen
        else
          return MainUI();       ///Main Screen
      },
    );
  }
}

Now, with a delay of 3 seconds, the startup screen is displayed for 3 seconds, during which time the file is downloaded from FTP and data is processed. I want to keep the splash screen until the completion of data processing rather than the specified time.

Splash Screen


Widget _splashUI(Size size){
    return SafeArea(
      child: Center(
        child: Container(
          width: size.width * 0.5,
          height: size.height * 0.1,
          child: Image(
            fit: BoxFit.fill,
            image: AssetImage('assets/images/elf_logo.png'),
          ),
        ),
      ),
    );
  }

 Widget build(BuildContext context) {

 getFtpFile();
 dataProgress();

 return Platform.isAndroid ?
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: _splashUI(_size),
      ),
    ) :
    CupertinoApp(
      debugShowCheckedModeBanner: false,
      home: CupertinoPageScaffold(
        child: _splashUI(_size),
      ),
    );
 }

I want to know how to keep SplashScreen while processing data rather than handling SplashScreen with delayed. thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晨说晚安 2025-01-16 13:26:22

flutter_native_splash 正是您所要求的。
runApp() 之前调用 FlutterNativeSplash.preserve() 以将启动画面保留在屏幕上,然后调用 FlutterNativeSplash.remove();您的下载完成:

import 'package:flutter_native_splash/flutter_native_splash.dart';

void main() {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  runApp(const MyApp());
}

// when your download is completed, remove the splash screen:
    FlutterNativeSplash.remove();

全面披露:我维护这个包。

The package flutter_native_splash does exactly what you are asking for.
Make a call to FlutterNativeSplash.preserve() before your runApp() to keep the splash on screen, then FlutterNativeSplash.remove(); when your download completes:

import 'package:flutter_native_splash/flutter_native_splash.dart';

void main() {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  runApp(const MyApp());
}

// when your download is completed, remove the splash screen:
    FlutterNativeSplash.remove();

Full disclosure: I maintain this package.

離殇 2025-01-16 13:26:22

处理数据时保留SplashScreen,而不是处理
SplashScreen 有延迟

为什么不改变延迟呢?

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
      future: _processingData(),
      builder: (BuildContext context, AsyncSnapshot snapshot){
        if(snapshot.connectionState == ConnectionState.waiting)
          return SplashUI();    ///Splash Screen
        else
          return MainUI();       ///Main Screen
      },
    );
  }

  Future<List> _processingData() {
    return Future.wait[
      _getFtpFile(),
      _dataProgress(),
    ];
  }
}

to keep SplashScreen while processing data rather than handling
SplashScreen with delayed.

Why not change the delayed?

class Home extends StatelessWidget {
  const Home({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
      future: _processingData(),
      builder: (BuildContext context, AsyncSnapshot snapshot){
        if(snapshot.connectionState == ConnectionState.waiting)
          return SplashUI();    ///Splash Screen
        else
          return MainUI();       ///Main Screen
      },
    );
  }

  Future<List> _processingData() {
    return Future.wait[
      _getFtpFile(),
      _dataProgress(),
    ];
  }
}
So要识趣 2025-01-16 13:26:22

你可以像其他人过去做过的那样做;您应该使您的方法 getFTPFiledataProgress 返回一个 Future,然后使用 Future.wait 等待这两个 Future强>,如这个答案
https://stackoverflow.com/a/54465973/871364

Future.wait([
   getFTPFile(),
   dataProgress(),     
], () {
  // once all Futures have completed, navigate to another page here
});

You could do like other people have done in the past; you should make both of your methods getFTPFile and dataProgress return a Future, then you wait for both Futures using Future.wait, as in this answer
https://stackoverflow.com/a/54465973/871364

Future.wait([
   getFTPFile(),
   dataProgress(),     
], () {
  // once all Futures have completed, navigate to another page here
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文