116 lines
3.3 KiB
Dart
116 lines
3.3 KiB
Dart
import 'package:appwrite/appwrite.dart';
|
|
import 'package:ltx_flutter/appwrite/appwrite.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_LoginPageState createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final emailTextController = TextEditingController();
|
|
final passwordTextController = TextEditingController();
|
|
bool loading = false;
|
|
|
|
signIn() async {
|
|
showDialog(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (BuildContext context) {
|
|
return Dialog(
|
|
backgroundColor: Colors.transparent,
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
|
children: const [
|
|
CircularProgressIndicator(),
|
|
]),
|
|
);
|
|
});
|
|
|
|
try {
|
|
final AuthAPI appwrite = context.read<AuthAPI>();
|
|
await appwrite.createEmailSession(
|
|
email: emailTextController.text,
|
|
password: passwordTextController.text,
|
|
);
|
|
Navigator.pop(context);
|
|
} on AppwriteException catch (e) {
|
|
Navigator.pop(context);
|
|
showAlert(title: 'Login failed', text: e.message.toString());
|
|
}
|
|
}
|
|
|
|
showAlert({required String title, required String text}) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) {
|
|
return AlertDialog(
|
|
title: Text(title),
|
|
content: Text(text),
|
|
actions: [
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
},
|
|
child: const Text('Ok'))
|
|
],
|
|
);
|
|
});
|
|
}
|
|
|
|
signInWithProvider(String provider) {
|
|
try {
|
|
context.read<AuthAPI>().signInWithProvider(provider: provider);
|
|
} on AppwriteException catch (e) {
|
|
showAlert(title: 'Login failed', text: e.message.toString());
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('LTX Android'),
|
|
),
|
|
body: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(32.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
TextField(
|
|
controller: emailTextController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: passwordTextController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Password',
|
|
border: OutlineInputBorder(),
|
|
),
|
|
obscureText: true,
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton.icon(
|
|
onPressed: () {
|
|
signIn();
|
|
},
|
|
icon: const Icon(Icons.login),
|
|
label: const Text("Sign in"),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|