shlink-manager/lib/views/login_view.dart

150 lines
5.1 KiB
Dart
Raw Normal View History

2023-06-26 23:40:05 +02:00
import 'package:flutter/material.dart';
2024-01-27 23:07:06 +01:00
import 'package:shlink_app/API/server_manager.dart';
2023-06-26 23:40:05 +02:00
import 'package:shlink_app/main.dart';
2024-01-27 23:07:06 +01:00
import '../globals.dart' as globals;
2023-06-26 23:40:05 +02:00
class LoginView extends StatefulWidget {
2024-01-28 00:32:09 +01:00
const LoginView({super.key});
2023-06-26 23:40:05 +02:00
@override
State<LoginView> createState() => _LoginViewState();
}
class _LoginViewState extends State<LoginView> {
2024-01-27 23:07:06 +01:00
late TextEditingController _serverUrlController;
late TextEditingController _apiKeyController;
2023-06-26 23:40:05 +02:00
bool _isLoggingIn = false;
String _errorMessage = "";
@override
void initState() {
// TODO: implement initState
super.initState();
2024-01-27 23:07:06 +01:00
_serverUrlController = TextEditingController();
_apiKeyController = TextEditingController();
2023-06-26 23:40:05 +02:00
}
void _connect() async {
setState(() {
_isLoggingIn = true;
_errorMessage = "";
});
2024-01-28 00:32:09 +01:00
final connectResult = await globals.serverManager
.initAndConnect(_serverUrlController.text, _apiKeyController.text);
2023-06-26 23:40:05 +02:00
connectResult.fold((l) {
Navigator.of(context).pushReplacement(
2024-01-28 00:32:09 +01:00
MaterialPageRoute(builder: (context) => const InitialPage()));
2023-06-26 23:40:05 +02:00
setState(() {
_isLoggingIn = false;
});
}, (r) {
if (r is ApiFailure) {
setState(() {
_errorMessage = r.detail;
_isLoggingIn = false;
});
2024-01-28 00:32:09 +01:00
} else if (r is RequestFailure) {
2023-06-26 23:40:05 +02:00
setState(() {
_errorMessage = r.description;
_isLoggingIn = false;
});
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
2024-01-28 00:32:09 +01:00
extendBody: true,
body: CustomScrollView(
slivers: [
const SliverAppBar.medium(
title: Text("Add server",
style: TextStyle(fontWeight: FontWeight.bold))),
SliverFillRemaining(
child: Padding(
2023-06-26 23:40:05 +02:00
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2024-01-28 00:32:09 +01:00
const Padding(
padding: EdgeInsets.only(bottom: 8),
child: Text(
"Server URL",
style: TextStyle(fontWeight: FontWeight.bold),
)),
2023-07-09 23:15:15 +02:00
Row(
children: [
2024-01-27 23:07:06 +01:00
const Icon(Icons.dns_outlined),
const SizedBox(width: 8),
2024-01-28 00:32:09 +01:00
Expanded(
child: TextField(
2024-01-27 23:07:06 +01:00
controller: _serverUrlController,
2023-07-09 23:15:15 +02:00
keyboardType: TextInputType.url,
decoration: const InputDecoration(
border: OutlineInputBorder(),
2024-01-28 00:32:09 +01:00
labelText: "https://shlink.example.com"),
2023-07-09 23:15:15 +02:00
))
],
2023-06-26 23:40:05 +02:00
),
const Padding(
padding: EdgeInsets.only(top: 8, bottom: 8),
2024-01-28 00:32:09 +01:00
child: Text("API Key",
style: TextStyle(fontWeight: FontWeight.bold)),
2023-06-26 23:40:05 +02:00
),
2023-07-09 23:15:15 +02:00
Row(
children: [
2024-01-27 23:07:06 +01:00
const Icon(Icons.key),
const SizedBox(width: 8),
2024-01-28 00:32:09 +01:00
Expanded(
child: TextField(
2024-01-27 23:07:06 +01:00
controller: _apiKeyController,
2023-07-09 23:15:15 +02:00
keyboardType: TextInputType.text,
obscureText: true,
decoration: const InputDecoration(
2024-01-28 00:32:09 +01:00
border: OutlineInputBorder(), labelText: "..."),
2023-07-09 23:15:15 +02:00
))
],
2023-06-26 23:40:05 +02:00
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FilledButton.tonal(
2024-01-28 00:32:09 +01:00
onPressed: () => {_connect()},
child: _isLoggingIn
? Container(
width: 34,
height: 34,
padding: const EdgeInsets.all(4),
child: const CircularProgressIndicator(),
)
: const Text("Connect",
style: TextStyle(fontSize: 20)),
2023-06-26 23:40:05 +02:00
)
],
),
),
Padding(
padding: const EdgeInsets.only(top: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2024-01-28 00:32:09 +01:00
Flexible(
child: Text(_errorMessage,
style: const TextStyle(color: Colors.red),
textAlign: TextAlign.center))
2023-06-26 23:40:05 +02:00
],
),
)
],
),
2024-01-28 00:32:09 +01:00
))
],
));
2023-06-26 23:40:05 +02:00
}
}