How to Add Light and Dark Theme Support in Your Flutter App with Adaptive Theme

Apr 15, 2024 | Programming

In today’s digital world, creating a visually appealing app isn’t just about layout and design; it’s also about ensuring your users can navigate your app comfortably. Incorporating light and dark themes allows your app to cater to different user preferences and ambient lighting conditions. Enter Adaptive Theme, a simple yet powerful package that lets you add light and dark theme support to your Flutter app.

Getting Started

To begin, you need to add the Adaptive Theme package to your Flutter project. This can be easily done by adding the following dependency in your pubspec.yaml file:

dependencies:
  adaptive_theme: latest_version

Initialization

Your next step is to wrap your MaterialApp in the AdaptiveTheme widget. Here’s how you can do this:

import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AdaptiveTheme(
      light: ThemeData.light(useMaterial3: true),
      dark: ThemeData.dark(useMaterial3: true),
      initial: AdaptiveThemeMode.light,
      builder: (theme, darkTheme) => MaterialApp(
        title: 'Adaptive Theme Demo',
        theme: theme,
        darkTheme: darkTheme,
        home: MyHomePage(),
      ),
    );
  }
}

Changing Theme Mode

After initialization, switching between themes is a breeze!

  • AdaptiveTheme.of(context).setDark(); – Set theme to dark.
  • AdaptiveTheme.of(context).setLight(); – Set theme to light.
  • AdaptiveTheme.of(context).setSystem(); – Set theme to system default.

Toggle Theme Mode

If you want an easy way for users to switch between themes, consider a toggle feature:

AdaptiveTheme.of(context).toggleThemeMode();

Changing Themes Entirely

If you wish to change the entire theme, including specific color schemes, you can do it like this:

AdaptiveTheme.of(context).setTheme(
  light: ThemeData(
    useMaterial3: true,
    brightness: Brightness.light,
    colorSchemeSeed: Colors.purple,
  ),
  dark: ThemeData(
    useMaterial3: true,
    brightness: Brightness.dark,
    colorSchemeSeed: Colors.purple,
  ),
);

Reset Theme

Do you want to bring back the default theme? With Adaptive Theme, it’s simple:

AdaptiveTheme.of(context).reset();

Handle App Start Theme

To ensure your app remembers the last selected theme, make your main() method asynchronous:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final savedThemeMode = await AdaptiveTheme.getThemeMode();
  runApp(MyApp(savedThemeMode: savedThemeMode));
}

Listen to Theme Changes

Adaptive Theme offers a way to listen for changes via ValueNotifier:

AdaptiveTheme.of(context).modeChangeNotifier.addListener(() {
  // React to theme changes
});

Using Floating Theme Button Overlay

For ease of testing your app under different themes, utilize a floating button that toggles theme modes:

AdaptiveTheme(
  light: ThemeData.light(),
  dark: ThemeData.dark(),
  debugShowFloatingThemeButton: true, // Add this line
  initial: AdaptiveThemeMode.light,
  builder: (theme, darkTheme) => MaterialApp(
    theme: theme,
    darkTheme: darkTheme,
    home: MyHomePage(),
  ),
);

Troubleshooting Ideas

Creating an app can sometimes be a “bumpy ride.” Here are some troubleshooting tips to simplify the process:

  • Check that you have imported the adaptive_theme package correctly.
  • Make sure your theme initialization is correctly wrapped around MaterialApp.
  • If you notice persistent theme issues, ensure shared_preferences is working as expected.
  • Don’t forget to call AdaptiveTheme.persist(); after clearing preferences to maintain theme settings.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations. With these simple steps and the adaptive theme package, your Flutter app will not only be more user-friendly but also visually appealing. Dive in, and give your users an elevated experience!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox