I have a url_launcher that opens up an Email app a an onTap listener and would like to show an error if there is no email app installed on the device ? How can this be achieved using the Flutter framework ?
The _launchcaller() throws an exception when an email app does not exist, but I would like the error message (custom message) to be displayed to the user so they know what is going to on ?
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Thank you"),
),
body: new ListView(children: <Widget>[
new Column(
children: <Widget>[
new Padding(
padding: new EdgeInsets.all(20.0),
child: new AnimatedBuilder(
animation: animationController,
child: new Container(
height: 150.0,
width: 150.0,
child: new Image.asset('assets/angry_face.png'),
),
builder: (BuildContext context, Widget _widget) {
return new Transform.rotate(
angle: animationController.value * 6.3,
child: _widget,
);
},
),
),
new Column(
children: <Widget>[
new Text(
'We are sorry for the inconvenience...',
style: new TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w700,
color: Colors.black87),
),
new Padding(
padding: new EdgeInsets.symmetric(
vertical: 5.0, horizontal: 0.0)),
new Text(
'Please contact us to resolve this issue!',
style: new TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.w200,
color: Colors.black87),
),
],
),
new Padding(
padding:
new EdgeInsets.symmetric(vertical: 25.0, horizontal: 0.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new GestureDetector(
onTap: _launchcaller,
child: new Icon(
Icons.contact_phone,
color: Colors.green,
size: 45.0,
),
),
new GestureDetector(
onTap: _launchcaller,
child: new Icon(
Icons.email,
color: Colors.blue,
size: 50.0,
),
),
],
),
),
],
),
]));
}
_launchcaller() async {
const url = 'mailto:[email protected]?subject=subject&body=body';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
}
Unless that isn't a choice, you can show a snackbar, like this.
You have a Scaffold, so add a key to it
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
Scaffold(
key: _scaffoldkey,
...
Then throw error like this:
_launchcaller() async {
const url = 'mailto:[email protected]?subject=subject&body=body';
if (await canLaunch(url)) {
await launch(url);
} else {
scaffoldkey.currentState.showSnackBar(
SnackBar(
content: new Text('Error: ${url}'),
duration: new Duration(seconds: 10),
);
);
throw 'Could not launch $url'; // <-- you might wanna ignore/comment this part out
// Or use a `print('Could not launch $url')` instead.
print('Could not launch $url')
}
}
Solution without scaffoldKey:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: Duration(seconds: 5),
content: Text('error message'),
)
)
This shows a white area at the bottom of the screen which stays for 5 seconds (specified duration) and then disappears again:
To add nice styling to the message I found this youtube video quite helpful.
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