【问题标题】:Calling a method in Statelesswidget for splash screen?调用 Statelesswidget 中的方法以获取启动画面?
【发布时间】:2020-06-03 13:11:21
【问题描述】:

我正在构建一个用于学习的天气应用程序,并且在应用程序开始时我想获取用户位置。闪屏是无状态的小部件,不做任何事情。当位置可用时,我想推送到另一个屏幕。

我可以做到,但这是正确的做法吗(请查看下面的代码)? 让我知道任何其他解决方案。

在从 build 方法返回小部件之前,我调用了 getLocation 方法。这是理想的做法吗?因为这个屏幕状态不会更新。

class LaunchScreen extends StatelessWidget {
  static const String id = 'launch_screen';

  void getLocation(BuildContext context) async {
    LocationData location = await LocationService().getCurrentLocation();
    if (location != null) {
      // Go to home
      print(location.latitude);
      print(location.longitude);
    } else {
      // Error fetching location
    }
  }

  @override
  Widget build(BuildContext context) {
    getLocation(context);
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(
            child: Container(),
          ),
          Center(
            child: Image(
              image: AssetImage('images/launch_screen/app_logo.png'),
            ),
          ),
          SizedBox(
            height: 8,
          ),
          Center(
            child: Shimmer.fromColors(
              baseColor: Colors.white.withOpacity(0.6),
              highlightColor: Colors.white,
              child: Text(
                'Forecasts',
                style: GoogleFonts.montserratAlternates(
                    textStyle:
                        TextStyle(fontSize: 60, fontWeight: FontWeight.w300),
                    color: Colors.white.withOpacity(0.6)),
              ),
            ),
          ),
          Expanded(
            child: Container(),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                'Made with ❤️ ',
                style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.w300,
                    color: Colors.white.withOpacity(0.5)),
              ),
              Text(
                'Parth Adroja',
                style: TextStyle(
                    fontSize: 12,
                    fontWeight: FontWeight.w500,
                    color: Colors.white.withOpacity(0.5)),
              ),
            ],
          ),
          SizedBox(
            height: 20,
          )
        ],
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以使用FutureBuilder 来执行此操作。

    喜欢:

    Widget build(BuildContext context) {
        return FutureBuilder(
          initialData: null,
          future: getLocation(),
          builder: (context, snap){
            if(snap.hasData){
              return FullDataWidget(snap.data);
            }else{
              return SplashScreen();
            }
          },
        );
      }
    

    【讨论】:

    • FutureBuilder 很高兴知道。
    • 当我收到想要导航到不同屏幕而不是显示 FullDataWidget 的位置时怎么办?
    • FullDataWidget 这是我使用的通用名称。您可以在“返回”之前调用任何方法,包括“Navigator.push”,但是当状态管理开始变得复杂时,最好使用 StateFullWidget。
    • 我问的原因是因为构建器需要返回一个小部件对吗?现在在此之前我推送它然后返回任何小部件没有任何意义,或者如果我可以返回 SplashScreen。但这没有意义。
    • 我认为在显示 Splash 或 FullDataWidget(或您的 Home)时最好使用顶部 Widget 来管理。如果使用 SplashScreen 转到另一个页面,则需要使用 Navigator.pushReplacement 从 NAvigator 的 Stack 中排除 Splash,开始非常复杂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 2017-03-10
    相关资源
    最近更新 更多