Flutter:每次应用程序显示时运行一个函数并强制同时加载小部件

abr*_*rev 3 android dart flutter

我为我工作的公司制作了一个非常简单的应用程序。该应用程序在打开时只是在浏览器上打开一个页面(它的工作原理就像网站的快捷方式,看起来很愚蠢,但它适合非常简单的人)。

但我有两个问题。

1 - 我将打开页面的函数放在 initState 上。但假设用户已打开应用程序一次,然后再次返回应用程序。该函数将不会运行,因为该应用程序是之前构建的。有没有办法在每次应用程序显示时运行该函数?

2 - 为了解决上述问题,我创建了一个按钮,用户单击该按钮并调用该函数,这样就可以了。但我在按钮上方也有公司徽标,当我打开应用程序时,按钮会在徽标之前加载,并且用户抱怨了这一点(图像大小不是问题,它只有 20kb)。有没有办法让按钮(和所有内容)在图像加载后立即加载?在加载所有内容之前放置加载指示器。

代码:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

**//the function:**
_launchURL() async {
  const url =
      'https://dev.testsite/pages/envio/document.php';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Error';
  }

}

class _MyAppState extends State<MyApp> {
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) => _launchURL());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          color: Colors.blueGrey[100],
          child: Column(
            children: <Widget>[
              SafeArea(
                child: Padding(
                  padding: EdgeInsets.only(top: 5.0),
                  child: Image.asset(
                    'assets/logo.png', **//the image**
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(5.0, 40.0, 5.0, 30.0),
                child: Text(
                  "Click the button if the \npage didn't load",
                  style: TextStyle(
                    fontSize: 18,
                  ),
                  textAlign: TextAlign.center,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 30.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      width: 250,
                      height: 150,
                      child: RaisedButton(
                        elevation: 20.0,
                        shape: StadiumBorder(
                            //borderRadius: new BorderRadius.circular(20.0),
                            side: BorderSide(
                                color: Color.fromRGBO(194, 39, 56, 1.0))),
                        color: Color.fromRGBO(194, 39, 56, 1.0),
                        onPressed: _launchURL,
                        child: Icon(Icons.arrow_right,
                            color: Color.fromRGBO(20, 21, 46, 1.0), size: 120),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}


Run Code Online (Sandbox Code Playgroud)

Jag*_*ngh 6

您可以使用WidgetsBindingObserver。它只是观察AppLifecycleState. _launchURL当应用程序返回时,您可以调用方法AppLifecycleState.resume

class _MyAppState extends State<MyApp>  with WidgetsBindingObserver {
  void initState() {
    super.initState();
   WidgetsBinding.instance.addObserver(this);
   }

  @override
   void dispose() {
     WidgetsBinding.instance.removeObserver(this);
     super.dispose();
   }

//// override this function
 @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if(state == AppLifecycleState.resumed)
 /// when user opens app again you can launch url if not already launched.
      _launchURL();
  }


  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Container(
          color: Colors.blueGrey[100],
          child: Column(
            children: <Widget>[
              SafeArea(
                child: Padding(
                  padding: EdgeInsets.only(top: 5.0),
                  child: Image.asset(
                    'assets/logo.png', **//the image**
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(5.0, 40.0, 5.0, 30.0),
                child: Text(
                  "Click the button if the \npage didn't load",
                  style: TextStyle(
                    fontSize: 18,
                  ),
                  textAlign: TextAlign.center,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(top: 30.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    SizedBox(
                      width: 250,
                      height: 150,
                      child: RaisedButton(
                        elevation: 20.0,
                        shape: StadiumBorder(
                            //borderRadius: new BorderRadius.circular(20.0),
                            side: BorderSide(
                                color: Color.fromRGBO(194, 39, 56, 1.0))),
                        color: Color.fromRGBO(194, 39, 56, 1.0),
                        onPressed: _launchURL,
                        child: Icon(Icons.arrow_right,
                            color: Color.fromRGBO(20, 21, 46, 1.0), size: 120),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息:- WidgetBindingObserver