diff --git a/lib/main.dart b/lib/main.dart index 4a9d2af..f89ffb7 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -17,15 +17,43 @@ void main() { ); } -class MyApp extends StatefulWidget { +class MyApp extends StatelessWidget { const MyApp({super.key}); @override - MyAppState createState() => MyAppState(); + Widget build(BuildContext context) { + return MaterialApp( + title: 'Pogdark', + theme: _buildTheme(), + home: const HomeScreen(), + ); + } + + ThemeData _buildTheme() { + return ThemeData( + primarySwatch: Colors.blue, + visualDensity: VisualDensity.adaptivePlatformDensity, + elevatedButtonTheme: ElevatedButtonThemeData( + style: ElevatedButton.styleFrom( + padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12), + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(12), + ), + ), + ), + ); + } } -class MyAppState extends State { - Future? _prefsReady; +class HomeScreen extends StatefulWidget { + const HomeScreen({super.key}); + + @override + HomeScreenState createState() => HomeScreenState(); +} + +class HomeScreenState extends State { + late Future _prefsReady; bool isProfileActive = false; bool showProfileInitially = false; @@ -33,16 +61,11 @@ class MyAppState extends State { void initState() { super.initState(); - // Retrieve SharedPreferencesProvider instance outside async function final prefsProvider = Provider.of(context, listen: false); - _prefsReady = prefsProvider.ready; - - _prefsReady!.then((_) { - // Ensure the widget is still mounted before updating state + _prefsReady = prefsProvider.ready.then((_) { if (mounted) { setState(() { - // Check if the username is not set, then show ProfileScreen initially showProfileInitially = prefsProvider.getUserName().isEmpty; }); } @@ -50,73 +73,62 @@ class MyAppState extends State { } void toggleProfileScreen() { - setState(() { - isProfileActive = !isProfileActive; - }); + setState(() => isProfileActive = !isProfileActive); } void closeInitialProfileScreen() { - setState(() { - showProfileInitially = false; - }); + setState(() => showProfileInitially = false); } @override Widget build(BuildContext context) { - return MaterialApp( - title: 'Pogdark', - theme: ThemeData( - primarySwatch: Colors.blue, - visualDensity: VisualDensity.adaptivePlatformDensity, - elevatedButtonTheme: ElevatedButtonThemeData( - style: ElevatedButton.styleFrom( - padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12), - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(12), - ), + return FutureBuilder( + future: _prefsReady, + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + return Stack( + children: [ + StatusPage(toggleProfile: toggleProfileScreen), + if (showProfileInitially) + ProfileOverlay( + isEditing: false, + onClose: closeInitialProfileScreen, + ), + if (isProfileActive && !showProfileInitially) + ProfileOverlay( + isEditing: true, + onClose: toggleProfileScreen, + ), + ], + ); + } else { + return const Center(child: CircularProgressIndicator()); + } + }, + ); + } +} + +class ProfileOverlay extends StatelessWidget { + final bool isEditing; + final VoidCallback onClose; + + const ProfileOverlay( + {super.key, required this.isEditing, required this.onClose}); + + @override + Widget build(BuildContext context) { + return BackdropFilter( + filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), + child: Container( + color: Colors.black.withOpacity(0.3), + child: Center( + child: ProfileScreen( + isEditing: isEditing, + onClose: onClose, ), ), ), - home: FutureBuilder( - future: _prefsReady, - builder: (context, snapshot) { - if (snapshot.connectionState == ConnectionState.done) { - return Stack( - children: [ - StatusPage(toggleProfile: toggleProfileScreen), - if (showProfileInitially) - BackdropFilter( - filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), - child: Container( - color: Colors.black.withOpacity(0.3), - child: Center( - child: ProfileScreen( - isEditing: false, - onClose: closeInitialProfileScreen, - ), - ), - ), - ), - if (isProfileActive && !showProfileInitially) - BackdropFilter( - filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5), - child: Container( - color: Colors.black.withOpacity(0.3), - child: Center( - child: ProfileScreen( - isEditing: true, - onClose: toggleProfileScreen, - ), - ), - ), - ), - ], - ); - } else { - return const Center(child: CircularProgressIndicator()); - } - }, - ), ); } } diff --git a/lib/status_page.dart b/lib/status_page.dart index ff0715d..5b74df4 100644 --- a/lib/status_page.dart +++ b/lib/status_page.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'dart:convert'; - import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; @@ -24,7 +23,7 @@ class StatusPageState extends State { late final Stream broadcastStream; late StreamController controller; List> messages = []; - final Map _imageCache = {}; // Cache for decoded images + final Map _imageCache = {}; @override void initState() { @@ -81,12 +80,12 @@ class StatusPageState extends State { await prefsProvider.setCurrentStatus(''); } else { messages.add(message); - _cacheImage(incomingId, image); // Cache image on message receive + _cacheImage(incomingId, image); } } void _cacheImage(String id, String? base64Image) { - if (base64Image != null && !_imageCache.containsKey(id)) { + if (base64Image != null) { _imageCache[id] = Image.memory(base64Decode(base64Image)).image; } }