I made a VERY SIMPLE app for the company I work in. The app just opens a page on the browser when it opens (it works just like a shortcut for the site, it seems foolish but it is for very simple people).
But I'm having 2 problems.
1 - I put the function to open the page on the initState. But let's say the user has opened the app once, and return to the app again. the function will not run because the app was built before. Is there a way to run the function every time the app shows?
2 - To solve the problem above I created a button that the user clicks and calls the function and that's ok. But I also have the company logo above the button, and when I open the app the button loads before the logo, and a user complained about this (the image size it's not the problem, its only 20kb). Is there a way to make the button (and everything) loads just after the image has loaded? To put a loading indicator before everything is loaded.
The code:
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),
),
),
],
),
),
],
),
),
),
);
}
}
You can use WidgetsBindingObserver
. It simply observes for the AppLifecycleState
.
You can call _launchURL
method when the application returns to 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),
),
),
],
),
),
],
),
),
),
);
}
}
For more information :- WidgetBindingObserver
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With