dth-pingpong-mobileapp/lib/main.dart

66 lines
1.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:logger/logger.dart';
import 'pages/login.dart';
import 'pages/home.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Ping Pong Tracker',
theme: ThemeData.dark(useMaterial3: true).copyWith(
colorScheme: ColorScheme.dark(
primary: Colors.blue.shade900,
),
),
home: EntryPoint(),
);
}
}
class EntryPoint extends StatelessWidget {
final Logger _logger = Logger();
Future<Widget> _getInitialPage() async {
_logger.d("Fetching token from SharedPreferences");
final prefs = await SharedPreferences.getInstance();
final token = prefs.getString('token');
_logger.d("Token value from SharedPreferences: $token");
if (token == null || token.isEmpty) {
_logger.d("No token found. Returning LoginPage.");
return LoginPage();
} else {
_logger.d("Token found. Returning HomePage.");
return HomePage();
}
}
@override
Widget build(BuildContext context) {
_logger.d("Building EntryPoint UI with FutureBuilder");
return FutureBuilder<Widget>(
future: _getInitialPage(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
_logger.d("Waiting for initial page resolution.");
return Scaffold(
body: Center(child: CircularProgressIndicator()),
);
} else if (snapshot.hasError) {
_logger.e("Error while resolving initial page: ${snapshot.error}");
return LoginPage(); // Fallback to LoginPage on error
} else {
_logger.d("Navigation target resolved: ${snapshot.data.runtimeType}");
return snapshot.data!;
}
},
);
}
}