2019-05-08 19:54:14 +01:00
import ' dart:async ' ;
import ' package:flutter/material.dart ' ;
import ' package:flutter/services.dart ' ;
import ' package:local_spend/common/apifunctions/request_login_api.dart ' ;
import ' package:local_spend/common/functions/show_dialog_single_button.dart ' ;
import ' package:local_spend/common/platform/platform_scaffold.dart ' ;
import ' package:shared_preferences/shared_preferences.dart ' ;
import ' package:url_launcher/url_launcher.dart ' ;
2019-07-09 11:52:25 +01:00
import ' package:local_spend/common/widgets/labeled_checkbox.dart ' ;
2019-08-20 16:01:56 +01:00
import ' package:local_spend/common/widgets/animatedGradientButton.dart ' ;
2019-05-08 19:54:14 +01:00
2019-08-21 14:53:52 +01:00
const url = " https://flutter.io/ " ;
2019-05-08 19:54:14 +01:00
class LoginPage extends StatefulWidget {
@ override
State < StatefulWidget > createState ( ) {
return new LoginPageState ( ) ;
}
}
class LoginPageState extends State < LoginPage > {
2019-08-20 13:54:45 +01:00
bool _isLoggingIn = false ;
2019-08-21 14:53:52 +01:00
final TextEditingController _emailController =
2019-08-27 11:00:57 +01:00
TextEditingController ( ) ;
2019-08-21 14:53:52 +01:00
final TextEditingController _passwordController =
2019-08-27 11:00:57 +01:00
TextEditingController ( ) ;
bool _saveLoginDetails = true ;
2019-07-05 22:56:15 +01:00
2019-08-21 14:53:52 +01:00
FocusNode focusNode ; // added so focus can move automatically
2019-05-08 19:54:14 +01:00
Future launchURL ( String url ) async {
if ( await canLaunch ( url ) ) {
await launch ( url , forceSafariVC: true , forceWebView: true ) ;
} else {
showDialogSingleButton (
context ,
" Unable to reach your website. " ,
2019-08-21 14:53:52 +01:00
" Currently unable to reach the website $ url . Please try again at a later time. " ,
2019-05-08 19:54:14 +01:00
" OK " ) ;
}
}
@ override
void initState ( ) {
super . initState ( ) ;
_saveCurrentRoute ( " /LoginPage " ) ;
2019-07-03 12:41:51 +01:00
focusNode = FocusNode ( ) ;
2019-07-16 12:09:10 +01:00
_fillLoginDetails ( ) ;
2019-07-03 12:41:51 +01:00
}
@ override
void dispose ( ) {
focusNode . dispose ( ) ; //disposes focus node when form disposed
super . dispose ( ) ;
2019-05-08 19:54:14 +01:00
}
2019-08-21 14:53:52 +01:00
void _fillLoginDetails ( ) async {
2019-07-16 12:09:10 +01:00
SharedPreferences preferences = await SharedPreferences . getInstance ( ) ;
var username = await preferences . get ( ' username ' ) ;
var password = await preferences . get ( ' password ' ) ;
_emailController . text = await username ;
_passwordController . text = await password ;
}
2019-08-21 14:53:52 +01:00
void _saveCurrentRoute ( String lastRoute ) async {
2019-05-08 19:54:14 +01:00
SharedPreferences preferences = await SharedPreferences . getInstance ( ) ;
await preferences . setString ( ' LastPageRoute ' , lastRoute ) ;
}
2019-08-21 14:53:52 +01:00
void login ( String username , String password ) async {
2019-08-20 13:54:45 +01:00
_isLoggingIn = true ;
2019-08-21 14:53:52 +01:00
await SystemChannels . textInput . invokeMethod ( ' TextInput.hide ' ) ;
2019-07-16 12:09:10 +01:00
SharedPreferences preferences = await SharedPreferences . getInstance ( ) ;
if ( _saveLoginDetails ) {
await preferences . setString ( ' username ' , username ) ;
await preferences . setString ( ' password ' , password ) ;
} else {
2019-08-27 11:00:57 +01:00
await preferences . setString ( ' username ' , " " ) ; // this does work...
await preferences . setString ( ' password ' , " " ) ; // ...but this doesn't seem to have any effect..?
2019-07-16 12:09:10 +01:00
}
2019-08-21 14:53:52 +01:00
await requestLoginAPI ( context , username , password ) . then ( ( value ) {
2019-08-20 13:54:45 +01:00
_isLoggingIn = false ;
} ) ;
2019-07-05 15:34:39 +01:00
}
2019-05-08 19:54:14 +01:00
@ override
Widget build ( BuildContext context ) {
return WillPopScope (
onWillPop: ( ) {
if ( Navigator . canPop ( context ) ) {
Navigator . of ( context ) . pushNamedAndRemoveUntil (
' /HomePage ' , ( Route < dynamic > route ) = > false ) ;
} else {
Navigator . of ( context ) . pushReplacementNamed ( ' /HomePage ' ) ;
}
2019-08-21 14:53:52 +01:00
return null ;
2019-05-08 19:54:14 +01:00
} ,
child: PlatformScaffold (
2019-08-20 16:01:56 +01:00
body: Stack (
children: [
2019-08-21 14:53:52 +01:00
AnimatedBackground ( [ Colors . lightBlue [ 50 ] , Colors . lightBlue [ 50 ] ] ,
Colors . white , Alignment . topRight , Alignment . bottomLeft , 3 ) ,
2019-08-20 16:01:56 +01:00
Container (
2019-08-21 14:53:52 +01:00
margin: EdgeInsets . fromLTRB ( 60 , 30 , 60 , 0 ) ,
2019-08-20 16:01:56 +01:00
child: Column (
children: < Widget > [
Expanded (
child: AnimatedContainer (
duration: Duration ( seconds: 2 ) ,
2019-08-21 14:53:52 +01:00
margin: EdgeInsets . fromLTRB ( 15 , 0 , 15 , 0 ) ,
2019-08-20 16:01:56 +01:00
decoration: BoxDecoration (
image: DecorationImage (
2019-08-21 14:53:52 +01:00
image:
AssetImage ( ' assets/images/launch_image.png ' ) ) ,
2019-05-08 19:54:14 +01:00
) ,
) ,
) ,
2019-08-20 16:01:56 +01:00
Padding (
padding: EdgeInsets . fromLTRB ( 0.0 , 0.0 , 0.0 , 0.0 ) ,
child: TextField (
autocorrect: false ,
textAlign: TextAlign . center ,
controller: _emailController ,
decoration: InputDecoration (
hintText: " EMAIL " ,
hintStyle: TextStyle ( fontSize: 15 ) ,
) ,
style: TextStyle (
fontSize: 18.0 ,
color: Colors . grey [ 800 ] ,
fontWeight: FontWeight . bold ,
) ,
onSubmitted: ( _ ) {
FocusScope . of ( context ) . requestFocus ( focusNode ) ;
} ,
2019-05-08 19:54:14 +01:00
) ,
) ,
2019-08-20 16:01:56 +01:00
Padding (
padding: EdgeInsets . fromLTRB ( 0.0 , 15.0 , 0.0 , 0.0 ) ,
child: TextField (
autocorrect: false ,
textAlign: TextAlign . center ,
controller: _passwordController ,
focusNode: focusNode ,
decoration: InputDecoration (
hintText: ' PASSWORD ' ,
hintStyle: TextStyle ( fontSize: 15 ) ,
) ,
obscureText: true ,
style: TextStyle (
fontSize: 18.0 ,
color: Colors . grey [ 800 ] ,
fontWeight: FontWeight . bold ,
2019-08-20 13:54:45 +01:00
) ,
2019-08-20 16:01:56 +01:00
onSubmitted: ( _ ) {
2019-08-21 14:53:52 +01:00
login ( _emailController . text , _passwordController . text ) ;
2019-08-20 16:01:56 +01:00
} ,
2019-07-16 11:28:36 +01:00
) ,
2019-08-20 16:01:56 +01:00
) ,
2019-08-21 14:53:52 +01:00
Container (
margin: EdgeInsets . fromLTRB ( 0.0 , 40.0 , 0.0 , 30.0 ) ,
width: 100 ,
height: 50 ,
child: Opacity (
opacity: _isLoggingIn ? 0.5 : 1 ,
child: ClipRRect (
borderRadius: BorderRadius . circular ( 2 ) ,
child: Stack (
children: [
AnimatedBackground (
[ Colors . blue , Colors . lightBlue [ 300 ] ] ,
Colors . lightBlue ,
Alignment . bottomRight ,
Alignment . topLeft ,
3 ) ,
Material (
type: MaterialType . transparency ,
child: InkWell (
onTap: _isLoggingIn
? null
: ( ) = > login ( _emailController . text ,
_passwordController . text ) ,
child: new Center (
child: new Text (
' GO ' ,
style: new TextStyle (
fontSize: 18 , color: Colors . white ) ,
) ,
) ,
2019-08-20 16:01:56 +01:00
) ,
) ,
2019-08-21 14:53:52 +01:00
] ,
) ,
2019-08-12 15:18:01 +01:00
) ,
) ,
2019-05-08 19:54:14 +01:00
) ,
2019-08-20 16:01:56 +01:00
Padding (
padding: EdgeInsets . fromLTRB ( 0 , 10 , 0 , 50 ) ,
child: LabeledCheckbox (
2019-08-21 14:53:52 +01:00
label: " SAVE LOGIN " ,
textStyle: TextStyle (
fontSize: 18 ,
color: Colors . black54 ,
fontWeight: FontWeight . bold ) ,
padding: const EdgeInsets . fromLTRB ( 0 , 0 , 0 , 0 ) ,
value: _saveLoginDetails ,
2019-08-20 16:01:56 +01:00
onChanged: ( bool newValue ) {
setState ( ( ) {
_saveLoginDetails = newValue ;
} ) ;
} ,
) ,
2019-07-09 11:52:25 +01:00
) ,
2019-08-20 16:01:56 +01:00
] ,
) ,
2019-05-08 19:54:14 +01:00
) ,
2019-08-20 16:01:56 +01:00
] ,
2019-05-08 19:54:14 +01:00
) ,
) ,
) ;
}
}