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
+ 87
30
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 70
25
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';
class Home extends StatefulWidget {
Home({@required this.mode});
Home({@required this.mode, @required this.invokerURL});
final String mode;
final String invokerURL;
@override
_HomeState createState() => _HomeState();
@@ -25,6 +28,38 @@ class _HomeState extends State<Home> {
initState() {
super.initState();
showMode(widget.mode);
FlutterAppAuthWrapper.eventStream().listen((data) {
var token = json.decode(data.toString())["access_token"];
_showAlert(token);
});
}
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('Try run ${widget.invokerURL}');
if (await canLaunch(widget.invokerURL)) {
log('Run ${widget.invokerURL}');
await launch(widget.invokerURL);
} else {
log('Could not launch ${widget.invokerURL}');
}
},
),
],
);
},
);
}
@override
@@ -41,27 +76,36 @@ class _HomeState extends State<Home> {
showMode(mode) {
log(mode);
if (mode == "app") {
FlutterWebBrowser.openWebPage(url: 'https://app.vereign.com', androidToolbarColor: Colors.deepPurple);
openVereign();
} else if (mode == "oauth") {
FlutterAppAuthWrapper.startAuth(
AuthConfig(
clientId: clientId,
clientSecret: clientSecret,
redirectUrl: redirectURL,
state: "login",
prompt: "consent",
endpoint: AuthEndpoint(
auth: authEndpoint, token: tokenEndpoint),
scopes: [
"user_account_status",
"user_territory",
"user_profile"
],
),
);
startOAuth();
}
}
openVereign() {
FlutterWebBrowser.openWebPage(url: 'https://app.vereign.com', androidToolbarColor: Colors.deepPurple);
}
startOAuth() {
FlutterAppAuthWrapper.startAuth(
AuthConfig(
clientId: clientId,
clientSecret: clientSecret,
redirectUrl: redirectURL,
state: "login",
prompt: "consent",
endpoint: AuthEndpoint(
auth: authEndpoint, token: tokenEndpoint),
scopes: [
"user_account_status",
"user_territory",
"user_profile"
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -70,20 +114,21 @@ class _HomeState extends State<Home> {
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: _links.map((link) => _urlButton(context, link)).toList(),
// children: _links.map((link) => _urlButton(context, link)).toList(),
children: <Widget>[
_urlButton(context, _links[0], "Open Vereign", openVereign),
_urlButton(context, _links[1], "Authorize with Vereign", startOAuth),
]
))));
}
Widget _urlButton(BuildContext context, String url) {
Widget _urlButton(BuildContext context, String url, String title, listener) {
return Container(
padding: EdgeInsets.all(20.0),
child: FlatButton(
color: Theme.of(context).primaryColor,
padding: const EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0),
child: Text(url),
onPressed: () => _handleURLButtonPress(context, url),
child: Text(title),
onPressed: listener
));
}
void _handleURLButtonPress(BuildContext context, String url) async {
FlutterWebBrowser.openWebPage(url: url, androidToolbarColor: Colors.deepPurple);
}
}
\ No newline at end of file
Loading