OpenContacts/lib/main.dart

66 lines
2 KiB
Dart
Raw Normal View History

2023-04-29 13:18:46 -04:00
import 'package:contacts_plus/widgets/home_screen.dart';
import 'package:contacts_plus/widgets/login_screen.dart';
import 'package:flutter/material.dart';
import 'api_client.dart';
import 'models/authentication_data.dart';
void main() {
2023-04-30 07:39:09 -04:00
runApp(const ContactsPlus());
2023-04-29 13:18:46 -04:00
}
2023-04-30 07:39:09 -04:00
class ContactsPlus extends StatefulWidget {
const ContactsPlus({super.key});
@override
State<ContactsPlus> createState() => _ContactsPlusState();
}
class _ContactsPlusState extends State<ContactsPlus> {
2023-04-29 16:21:00 -04:00
final Typography _typography = Typography.material2021(platform: TargetPlatform.android);
2023-04-30 07:39:09 -04:00
AuthenticationData _authData = AuthenticationData.unauthenticated();
2023-04-29 13:18:46 -04:00
@override
Widget build(BuildContext context) {
2023-04-30 07:39:09 -04:00
return ClientHolder(
authenticationData: _authData,
child: MaterialApp(
title: 'Contacts+',
theme: ThemeData(
textTheme: _typography.white,
colorScheme: ColorScheme.fromSeed(seedColor: Colors.purple, brightness: Brightness.dark)
),
home: _authData.isAuthenticated ?
const HomeScreen() :
LoginScreen(
onLoginSuccessful: (AuthenticationData authData) {
if (authData.isAuthenticated) {
setState(() {
_authData = authData;
});
}
},
),
2023-04-29 13:18:46 -04:00
),
);
}
}
2023-04-30 07:39:09 -04:00
class ClientHolder extends InheritedWidget {
final ApiClient client;
2023-04-29 13:18:46 -04:00
2023-04-30 07:39:09 -04:00
ClientHolder({super.key, required AuthenticationData authenticationData, required super.child})
: client = ApiClient(authenticationData: authenticationData);
2023-04-29 13:18:46 -04:00
2023-04-30 07:39:09 -04:00
static ClientHolder? maybeOf(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<ClientHolder>();
}
2023-04-29 13:18:46 -04:00
2023-04-30 07:39:09 -04:00
static ClientHolder of(BuildContext context) {
final ClientHolder? result = maybeOf(context);
assert(result != null, 'No AuthenticatedClient found in context');
return result!;
2023-04-29 13:18:46 -04:00
}
2023-04-30 07:39:09 -04:00
@override
bool updateShouldNotify(covariant ClientHolder oldWidget) => oldWidget.client != client;
2023-04-29 13:18:46 -04:00
}