Skip to content
Snippets Groups Projects

Mobile app initial implementation.

Merged Igor Markin requested to merge 3-android-mobile-app-web-view into master
4 files
+ 3
3
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 202
0
import 'package:flutter/material.dart';
import 'package:flutter_web_browser/flutter_web_browser.dart';
import 'package:flutter_app_auth_wrapper/flutter_app_auth_wrapper.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:developer';
import 'dart:convert';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
import '../../config.dart';
class Home extends StatefulWidget {
Home({@required this.mode, @required this.invokerURL, @required this.setMode, @required this.host});
final String mode;
final String invokerURL;
final String host;
final void Function(String) setMode;
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String _host = Config.appFlavor == Flavor.DEVELOPMENT ? Config.HOSTS[0] : Config.DEFAULT_APP_HOST;
bool _hidden = true;
@override
initState() {
super.initState();
if (widget.host != null) {
setState(() {
_host = widget.host;
});
}
showMode(widget.mode);
FlutterAppAuthWrapper.eventStream().listen((data) {
var token = json.decode(data.toString())["access_token"];
_showAlert(token);
widget.setMode("");
}, onError: (error) {
log("Err $error");
widget.setMode("");
});
Timer(
Duration(seconds: 3),
() {
setState(() {
_hidden = false;
});
}
);
}
Future<void> _showAlert(token) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Authorization success'),
actions: <Widget>[
FlatButton(
child: Text('Go back'),
onPressed: () async {
Navigator.of(context).pop();
log("Open ${widget.invokerURL}");
try {
await launch("${widget.invokerURL}?token=$token&host=$_host");
} catch (e) {
log("Error launching url ${widget.invokerURL}");
}
},
),
],
);
},
);
}
@override
void didUpdateWidget(Home oldWidget) {
// this method IS called when parent widget passes new "props"
// unlike React, this method IS called _before_ the build
// unlike React, this method ISN'T called after setState()
if (widget.host != oldWidget.host && widget.host != null) {
setState(() {
_host = widget.host;
});
}
if (widget.mode != oldWidget.mode) {
showMode(widget.mode);
}
super.didUpdateWidget(oldWidget);
}
showMode(mode) {
if (mode == "app") {
openVereign();
} else if (mode == "oauth") {
startOAuth();
}
}
openVereign() {
FlutterWebBrowser.openWebPage(url: _host, androidToolbarColor: Color(0xFFd51d32));
}
startOAuth() {
var params = Config.getOAuthParams(host: _host);
FlutterAppAuthWrapper.startAuth(
AuthConfig(
clientId: params["clientId"],
clientSecret: params["clientSecret"],
redirectUrl: params["redirectUrl"],
state: "login",
prompt: "consent",
endpoint: AuthEndpoint(
auth: params["authEndpoint"], token: params["tokenEndpoint"]),
scopes: [
"user_account_status",
"user_territory",
"user_profile"
],
),
);
}
@override
Widget build(BuildContext context) {
if (_hidden) {
return Scaffold();
}
var children = <Widget>[
_urlButton(context, "Open Dashboard", openVereign),
_urlButton(context, "Authorize with Vereign", startOAuth)
];
if (Config.appFlavor == Flavor.DEVELOPMENT) {
children.add(
wrapInContainer(
DropdownButton<String>(
value: _host,
onChanged: (String newValue) async {
setState(() {
_host = newValue;
});
final prefs = await SharedPreferences.getInstance();
prefs.setString("host", newValue);
},
items: Config.HOSTS
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
)
)
);
}
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: children
))));
}
Widget wrapInContainer(Widget widget) {
return Container(
padding: EdgeInsets.all(10.0),
child: widget,
);
}
Widget _urlButton(BuildContext context, String title, listener) {
return wrapInContainer(
FlatButton(
color: Theme.of(context).primaryColor,
textColor: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0),
child: Text(title),
onPressed: listener
)
);
}
}
\ No newline at end of file
Loading