ugly login page and dbhelper
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'pages/login_page.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
runApp(const Application());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
class Application extends StatelessWidget {
|
||||
const Application({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -15,6 +17,7 @@ class MyApp extends StatelessWidget {
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: const LoginPage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
34
lib/pages/login_page.dart
Normal file
34
lib/pages/login_page.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../services/db_helper.dart';
|
||||
|
||||
class LoginPage extends StatelessWidget {
|
||||
const LoginPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
TextEditingController username = TextEditingController();
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: username,
|
||||
decoration: const InputDecoration(
|
||||
label: Text('Username'),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => DbHelper.login(username.text),
|
||||
child: const Text('Login'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => DbHelper.logout(),
|
||||
child: const Text('Logout'),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
27
lib/services/db_helper.dart
Normal file
27
lib/services/db_helper.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class DbHelper {
|
||||
static Future<SharedPreferences> get _prefs async =>
|
||||
SharedPreferences.getInstance();
|
||||
|
||||
static void login(String username) async {
|
||||
final prefs = await _prefs;
|
||||
final String? savedUsername = prefs.getString('username');
|
||||
|
||||
if (savedUsername == null) {
|
||||
await prefs.setString('username', username);
|
||||
// TODO create new account
|
||||
} else if (savedUsername == username) {
|
||||
// TODO log into database
|
||||
} else {
|
||||
// TODO wrong username
|
||||
}
|
||||
}
|
||||
|
||||
static void logout() async {
|
||||
final prefs = await _prefs;
|
||||
bool success = await prefs.remove('username');
|
||||
|
||||
//TODO log out on database
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user