Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
Mobile App
Manage
Activity
Members
Labels
Plan
Issues
3
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Code
Mobile App
Merge requests
!1
Mobile app initial implementation.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Mobile app initial implementation.
3-android-mobile-app-web-view
into
master
Overview
0
Commits
37
Pipelines
0
Changes
86
Merged
Igor Markin
requested to merge
3-android-mobile-app-web-view
into
master
5 years ago
Overview
0
Commits
37
Pipelines
0
Changes
4
Expand
Closes
#3 (closed)
0
0
Merge request reports
Viewing commit
942cdc93
Show latest version
4 files
+
3
−
3
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
942cdc93
Rearrange directories.
· 942cdc93
igorwork
authored
5 years ago
lib/src/screens/home.dart
0 → 100644
+
202
−
0
Options
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