ocarina2/lib/views/login_view.dart

103 lines
3.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ocarina/api/login_manager.dart';
import 'package:ocarina/api/subsonic/subsonic.dart';
import 'package:ocarina/views/home_view.dart';
import 'package:responsive_sizer/responsive_sizer.dart';
class LoginView extends StatefulWidget {
const LoginView({super.key});
@override
State<LoginView> createState() => _LoginViewState();
}
class _LoginViewState extends State<LoginView> {
final _userController = TextEditingController();
final _passController = TextEditingController();
final _urlController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
width: 95.w,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Ocarina",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(
height: 10,
),
TextField(
controller: _userController,
decoration: const InputDecoration(hintText: "Username"),
),
const SizedBox(
height: 5,
),
TextField(
controller: _passController,
obscureText: true,
decoration: const InputDecoration(hintText: "Password"),
),
const SizedBox(
height: 5,
),
TextField(
controller: _urlController,
decoration: const InputDecoration(hintText: "Subsonic URL"),
inputFormatters: [
FilteringTextInputFormatter.allow(
RegExp(r"[a-z:\/\.0-9\_]+", multiLine: true),
),
],
),
const SizedBox(
height: 5,
),
TextButton(
onPressed: () async {
if (_urlController.text.isEmpty ||
_passController.text.isEmpty ||
_userController.text.isEmpty) return;
final baseUrl = _urlController.text; // TODO: more formatting
if (baseUrl.endsWith("/")) {
baseUrl.substring(0, baseUrl.length - 1);
}
SubsonicApiService()
..baseUrl = baseUrl
..user = _userController.text
..password = _passController.text;
final r = await SubsonicApiService().verifyCredentials();
if (!r) return;
await LoginManager.saveDetails(
_userController.text,
_passController.text,
baseUrl,
);
if (!context.mounted) return;
await Navigator.pushReplacement(
context,
MaterialPageRoute<void>(
builder: (c) => const HomeView(),
),
);
},
child: const Text("Log in"),
),
],
),
),
),
);
}
}