Zenith/lib/main.dart

73 lines
1.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zenith/chats.dart';
import 'package:zenith/login.dart';
import 'package:provider/provider.dart';
import 'package:zenith/zenith_client_provider.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => ZenithClientProvider(),
child: MaterialApp(
title: 'Zenith',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(),
),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
bool savedCreds = false;
void getCreds() async {
final prefs = await SharedPreferences.getInstance();
final homeserver = prefs.getString("homeserver");
final username = prefs.getString("username");
final password = prefs.getString("password");
if (homeserver == null || username == null || password == null) return;
setState(() {
savedCreds = true;
});
if (!mounted) return;
final provider = Provider.of<ZenithClientProvider>(context, listen: false);
await provider.initialize(homeserver, username, password);
}
@override
void initState() {
super.initState();
getCreds();
}
@override
Widget build(BuildContext context) {
if (!savedCreds) {
return const LoginPage();
} else {
return const ChatsPage();
}
}
}