I’m working on a Flutter project with a custom app structure that does not use MaterialApp
directly for initializing the app. Instead, app configurations are managed through a custom AppConfig
class. I’m trying to integrate a darkTheme
configuration similar to how it’s traditionally implemented within MaterialApp
.
Here’s a breakdown of my project structure:
-
AppConfig Class:
I have anAppConfig
class defined inconfig/app_config.dart
to manage app configurations. Currently, it includes properties likeappName
and other custom configurations.class AppConfig { final String appName; AppConfig({ required this.appName, }); }
-
Main Entry Point:
My main entry point (main.dart
) initializes the app based on configurations provided byAppConfig
.import 'package:flutter/material.dart'; import 'config/app_config.dart'; import 'config/app_init.dart'; import 'restart_widget.dart'; Future<void> main() async { AppConfig devAppConfig = AppConfig( appName: 'MedCare', ); Widget app = await initializeApp(devAppConfig); runApp(RestartWidget(child: app)); }
-
Desired Implementation of darkTheme:
I want to extend theAppConfig
class to include adarkTheme
property and apply this theme configuration globally within my app. Ideally, I’d like to implement thisdarkTheme
configuration in a way that mirrors how it’s typically done withinMaterialApp
:MaterialApp( theme: ThemeData.light(), darkTheme: ThemeData.dark(), themeMode: ThemeMode.system, // or ThemeMode.light/dark home: MyHomePage(), );
-
Integration Challenges:
How can I modify my custom app initialization process to accommodate thedarkTheme
configuration similar to the traditional approach used withinMaterialApp
? Specifically, I’m looking for guidance on how to set up and apply thedarkTheme
globally within my app structure without relying onMaterialApp
directly.
I’ve attempted to extend the AppConfig
class to include a darkTheme
property similar to the ThemeData.dark()
approach used in MaterialApp
. However, I’m unsure how to integrate this darkTheme
configuration effectively within my custom app initialization process (main.dart
structure).
I would greatly appreciate any advice, code examples, or best practices on how to implement and apply the darkTheme
configuration in a custom Flutter app structure without using MaterialApp
directly.
danchoichoufee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.