145 lines
4.5 KiB
Dart
145 lines
4.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:animated_background/animated_background.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:tuuli_app/api/api_client.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
State<StatefulWidget> createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> with TickerProviderStateMixin {
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
final apiClient = Get.find<ApiClient>();
|
|
var submitted = false;
|
|
|
|
final loginController = TextEditingController();
|
|
final passwordController = TextEditingController();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final screenSize = MediaQuery.of(context).size;
|
|
final formWidth = screenSize.width <= 600 ? screenSize.width : 300.0;
|
|
return Stack(
|
|
children: [
|
|
AnimatedBackground(
|
|
behaviour: RandomParticleBehaviour(),
|
|
vsync: this,
|
|
child: const SizedBox.square(
|
|
dimension: 0,
|
|
),
|
|
),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
LimitedBox(
|
|
maxWidth: formWidth,
|
|
child: Container(
|
|
color: Colors.black.withAlpha(100),
|
|
padding: const EdgeInsets.all(16),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
TextFormField(
|
|
controller: loginController,
|
|
enabled: !submitted,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Login',
|
|
hintText: 'Enter your login',
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Please enter your Login';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
TextFormField(
|
|
controller: passwordController,
|
|
obscureText: true,
|
|
enabled: !submitted,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Password',
|
|
hintText: 'Enter your password',
|
|
),
|
|
validator: (value) {
|
|
if (value == null || value.isEmpty) {
|
|
return 'Please enter your password';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: submitted ? null : _submit,
|
|
child: const Text('Login'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Future<void> _submit() async {
|
|
if (!_formKey.currentState!.validate()) {
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
submitted = true;
|
|
});
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Trying to login...'),
|
|
),
|
|
);
|
|
|
|
final response = await apiClient.login(
|
|
loginController.text.trim(),
|
|
passwordController.text.trim(),
|
|
);
|
|
response.unfold((data) {
|
|
ScaffoldMessenger.of(context).hideCurrentSnackBar();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Login successful'),
|
|
),
|
|
);
|
|
GetStorage()
|
|
.write("accessToken", data.accessToken)
|
|
.then((value) => GetStorage().save());
|
|
Timer(1.seconds, () {
|
|
ScaffoldMessenger.of(context).hideCurrentSnackBar();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
Get.offAllNamed("/home");
|
|
});
|
|
});
|
|
}, (error) {
|
|
ScaffoldMessenger.of(context).hideCurrentSnackBar();
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(error.toString()),
|
|
),
|
|
);
|
|
setState(() {
|
|
submitted = false;
|
|
});
|
|
});
|
|
}
|
|
}
|