Updated 29 October 2024
In this blog we will learn about how to use Adaptive Theme In Flutter.
Adaptive themes in Flutter allow your app to automatically change its appearance based on user preferences or device settings, like light and dark modes.
Adaptive Theme in Flutter let your app change its look based on user preferences, like switching between light and dark modes.
This makes the app more comfortable to use in different lighting conditions.
This feature is particularly useful for supporting light and dark themes, which help users view content comfortably in various lighting conditions.
We are going to implement Adaptive Theme In Flutter so we need to follow below steps to implement this topic.
First of all we need to create a new flutter project and add the following dependency in pubspec.yaml file.
1 |
adaptive_theme: ^3.6.0 |
Add latest version of these dependency in your flutter project.
In this step you need to create Wrap MaterialApp with AdaptiveTheme widget.
1 2 3 4 5 6 7 8 9 10 11 12 |
AdaptiveTheme( light: ThemeData.light(useMaterial3: true), dark: ThemeData.dark(useMaterial3: true), initial: AdaptiveThemeMode.light, builder: (theme, darkTheme) => MaterialApp( debugShowCheckedModeBanner: false, title: 'Adaptive Theme Demo', theme: theme, darkTheme: darkTheme, home: const MyHomePage(title: 'Adaptive Theme Demo'), ), ); |
With the help of above code we will implement AdaptiveTheme.
This widget manages light and dark themes.
Both themes are defined using ThemeData.light(useMaterial3: true)
and ThemeData.dark(useMaterial3: true)
, which enables Material 3 design features.
The app starts with the light theme (AdaptiveThemeMode.light
).
The builder function creates MaterialApp.
In this step you need to create a StatefulWidget and create a button on which you can change app theme mode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Center( child: Text("Implement Adaptive Theme"), ), floatingActionButton: FloatingActionButton( onPressed: (){ AdaptiveTheme.of(context).toggleThemeMode(); }, child: Text('Change Theme'), ), ); } } |
Above mention code is helpful to change App Theme (light to dark & dark to light).
Adaptive themes in Flutter allow your app to automatically change its appearance based on user preferences or device settings, like light and dark modes.
In this blog we have discussed about How To Implement Adaptive Theme In Flutter.
I hope this blog is helpful to UnderStand this topic, you can visit here for more knowledge about Adaptive Theme.
Please visit the Link for more additional information about theme.
You can also check other blogs from here for more knowledge.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.