如何导航到之前内置于 flutter 中的屏幕?

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

我在我的主页上调用了一系列API,并在从服务器接收数据后创建了一系列小部件。 我想缩短这些小部件的加载时间,所以我希望在显示启动画面时完全构建我的主页,以便当导航操作发生时,观众将面对一个准备好的页面。

测试了以下代码,但显然,除非在 build 方法中使用 HomeScreen 小部件,否则构建操作和调用 api 不会在主页上发生:

import 'package:flutter/material.dart';
import 'package:imam/config/routes/routes.dart';
import 'package:imam/core/utils/extensions.dart';
import 'package:imam/features/home/screens/home_screen.dart';

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  State<SplashScreen> createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  late HomeScreen homeScreen;

  @override
  void initState() {
    homeScreen =  HomeScreen();
    Future.delayed(const Duration(seconds: 3), () {
      context.pushNamed(Routes.homeScreen);
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return const Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Splash Screen',
              style: TextStyle(
                fontWeight: FontWeight.bold,
                fontSize: 24,
              ),
            ),
            SizedBox(height: 20),
            CircularProgressIndicator(),
          ],
        ),
      ),
    );
  }
}

请注意,我不想等待主页的加载数据,我只想在显示启动页面的那 3 秒内同时创建主页的所有组件。

我不想使用 Stack 小部件作为解决此问题的技巧,因为页面应该通过导航来移动。
谢谢你的好意。

flutter dart navigation isolation
1个回答
0
投票

尝试在您的

FutureBuilder
,
 中使用 
SplashScreen

class SplashScreen extends StatefulWidget {
  const SplashScreen({super.key});

  @override
  SplashScreenState createState() => SplashScreenState();
}

class SplashScreenState extends State<SplashScreen> {
  late Future<void> _initializing;

  @override
  void initState() {
    super.initState();
    _initializing = _initializingApp();
  }

  Future<void> _initializingApp() async {
    // Perform expensive operations, such as data fetching or widget pre-building
    await Future.delayed(const Duration(seconds: 3));
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initializing,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return const HomeScreen();
        }else if () {
          //handle errors here
        } else {
          return const Scaffold(
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    'Splash Screen',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 24,
                    ),
                  ),
                  SizedBox(height: 20),
                  CircularProgressIndicator(),
                ],
              ),
            ),
          );
        }
      },
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.