gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:35:16 28 4
gpt4 key购买 nike

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

但是我有两个问题。

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

2 - 为了解决上面的问题,我创建了一个按钮,用户可以单击该按钮并调用该函数,这样就可以了。但是我在按钮上方也有公司 Logo ,当我打开应用程序时,按钮先于 Logo 加载,并且用户对此提示(图像大小不是问题,它只有 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),
),
),
],
),
),
],
),
),
),
);
}
}


最佳答案

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

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

更多信息:- WidgetBindingObserver

关于android - Flutter:每次应用程序显示时运行一个函数并强制同时加载小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61481062/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com