11import 'package:flutter/material.dart' ;
22import 'package:http/http.dart' as http;
3- import 'package:flutter_secure_storage/flutter_secure_storage.dart' ;
3+ import 'package:ir_net/data/shared_preferences.dart' ;
4+
5+ class KerioLoginView extends StatefulWidget {
6+ const KerioLoginView ({super .key});
47
5- class KerioLoginPage extends StatefulWidget {
68 @override
7- _KerioLoginPageState createState () => _KerioLoginPageState ();
9+ State < KerioLoginView > createState () => _KerioLoginViewState ();
810}
911
10- class _KerioLoginPageState extends State <KerioLoginPage > {
12+ class _KerioLoginViewState extends State <KerioLoginView > {
1113 final TextEditingController _ipController = TextEditingController ();
1214 final TextEditingController _usernameController = TextEditingController ();
1315 final TextEditingController _passwordController = TextEditingController ();
14- final _storage = FlutterSecureStorage ();
1516
1617 @override
1718 void initState () {
@@ -20,19 +21,21 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
2021 }
2122
2223 Future <void > _attemptAutoLogin () async {
23- final ip = await _storage.read (key: 'kerio_ip' );
24- final username = await _storage.read (key: 'kerio_username' );
25- final password = await _storage.read (key: 'kerio_password' );
24+ final ip = await AppSharedPreferences .kerioIP;
25+ final username = await AppSharedPreferences .kerioUsername;
26+ final password = await AppSharedPreferences .kerioPassword;
27+ final enabled = await AppSharedPreferences .kerioAutoLogin;
2628
27- if (ip != null && username != null && password != null ) {
29+ _ipController.text = ip ?? '' ;
30+ if (ip != null && username != null && password != null && enabled == true ) {
2831 _ipController.text = ip;
2932 _usernameController.text = username;
3033 _passwordController.text = password;
31- _login ();
34+ _login (true );
3235 }
3336 }
3437
35- void _login () async {
38+ void _login (bool auto ) async {
3639 final ip = _ipController.text;
3740 final username = _usernameController.text;
3841 final password = _passwordController.text;
@@ -43,9 +46,9 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
4346 }
4447
4548 // Save credentials for auto-login
46- await _storage. write (key : 'kerio_ip' , value : ip);
47- await _storage. write (key : 'kerio_username' , value : username);
48- await _storage. write (key : 'kerio_password' , value : password);
49+ await AppSharedPreferences . setKerioIP ( ip);
50+ await AppSharedPreferences . setKerioUsername ( username);
51+ await AppSharedPreferences . setKerioPassword ( password);
4952
5053 final url = 'http://$ip /internal/dologin.php' ;
5154 final response = await http.post (
@@ -56,11 +59,11 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
5659 },
5760 );
5861
59- if (response.statusCode == 200 ) {
60- _showMessage ('Login successful' );
61- } else {
62- _showMessage ('Login failed' );
62+ if (auto) {
63+ return ;
6364 }
65+
66+ _showMessage ('Login request sent' ); // todo: handle failure case
6467 }
6568
6669 void _showMessage (String message) {
@@ -71,7 +74,7 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
7174 actions: [
7275 TextButton (
7376 onPressed: () => Navigator .of (context).pop (),
74- child: Text ('OK' ),
77+ child: const Text ('OK' ),
7578 ),
7679 ],
7780 ),
@@ -80,37 +83,124 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
8083
8184 @override
8285 Widget build (BuildContext context) {
83- return Scaffold (
84- appBar: AppBar (
85- title: Text ('Kerio Login' ),
86+ return SizedBox (
87+ width: 400 ,
88+ child: Column (
89+ crossAxisAlignment: CrossAxisAlignment .center,
90+ children: [
91+ ipInput (),
92+ const SizedBox (height: 4 ),
93+ Row (
94+ children: [
95+ Expanded (
96+ child: username (),
97+ ),
98+ const SizedBox (width: 4 ),
99+ Expanded (
100+ child: password (),
101+ ),
102+ ],
103+ ),
104+ const SizedBox (height: 8 ),
105+ loginRow ()
106+ ],
86107 ),
87- body: Padding (
88- padding: const EdgeInsets .all (16.0 ),
89- child: Column (
90- crossAxisAlignment: CrossAxisAlignment .stretch,
91- children: [
92- TextField (
93- controller: _ipController,
94- decoration: InputDecoration (labelText: 'Kerio Login Page IP' ),
95- keyboardType: TextInputType .url,
96- ),
97- TextField (
98- controller: _usernameController,
99- decoration: InputDecoration (labelText: 'Username' ),
100- ),
101- TextField (
102- controller: _passwordController,
103- decoration: InputDecoration (labelText: 'Password' ),
104- obscureText: true ,
105- ),
106- SizedBox (height: 20 ),
107- ElevatedButton (
108- onPressed: _login,
109- child: Text ('Login' ),
110- ),
111- ],
108+ );
109+ }
110+
111+ Widget ipInput () {
112+ return TextField (
113+ controller: _ipController,
114+ decoration: InputDecoration (
115+ focusedBorder: const OutlineInputBorder (
116+ borderSide: BorderSide (color: Colors .green),
117+ ),
118+ enabledBorder: const OutlineInputBorder (
119+ borderSide: BorderSide (color: Colors .blue),
120+ ),
121+ hintText: 'Kerio login page IP' ,
122+ hintStyle: const TextStyle (color: Colors .black38),
123+ suffixIcon: IconButton (
124+ onPressed: null ,
125+ icon: Image .asset ('assets/kerio.png' , width: 24 , height: 24 ),
126+ ),
127+ ),
128+ keyboardType: TextInputType .url,
129+ );
130+ }
131+
132+ Widget username () {
133+ return TextField (
134+ controller: _usernameController,
135+ decoration: const InputDecoration (
136+ focusedBorder: OutlineInputBorder (
137+ borderSide: BorderSide (color: Colors .green),
138+ ),
139+ enabledBorder: OutlineInputBorder (
140+ borderSide: BorderSide (color: Colors .blue),
112141 ),
142+ hintText: 'Username' ,
143+ hintStyle: TextStyle (color: Colors .black38),
113144 ),
114145 );
115146 }
147+
148+ Widget password () {
149+ return TextField (
150+ controller: _passwordController,
151+ decoration: const InputDecoration (
152+ focusedBorder: OutlineInputBorder (
153+ borderSide: BorderSide (color: Colors .green),
154+ ),
155+ enabledBorder: OutlineInputBorder (
156+ borderSide: BorderSide (color: Colors .blue),
157+ ),
158+ hintText: 'Password' ,
159+ hintStyle: TextStyle (color: Colors .black38),
160+ ),
161+ obscureText: true ,
162+ );
163+ }
164+
165+ Widget loginRow () {
166+ return Row (
167+ children: [
168+ Expanded (
169+ flex: 2 ,
170+ child: ElevatedButton (
171+ onPressed: () => _login (false ),
172+ style: ElevatedButton .styleFrom (
173+ shape: const RoundedRectangleBorder (
174+ borderRadius: BorderRadius .all (Radius .circular (8 )),
175+ ),
176+ padding: const EdgeInsets .symmetric (vertical: 16 , horizontal: 24 ),
177+ backgroundColor: Colors .blue,
178+ ),
179+ child: const Text ('Login' , style: TextStyle (color: Colors .white)),
180+ ),
181+ ),
182+ const SizedBox (width: 10 ),
183+ Expanded (
184+ child: autoLoginOption (),
185+ )
186+ ],
187+ );
188+ }
189+
190+ Widget autoLoginOption () {
191+ return FutureBuilder <bool >(
192+ future: AppSharedPreferences .kerioAutoLogin,
193+ builder: (context, snapshot) {
194+ final value = snapshot.data ?? false ;
195+ return CheckboxListTile (
196+ title: const Text ('Auto?' ),
197+ value: value,
198+ onChanged: (enabled) async {
199+ await AppSharedPreferences .setKerioAutoLogin (enabled ?? false );
200+ setState (() {});
201+ },
202+ );
203+ },
204+ );
205+ }
116206}
0 commit comments