How can we change icon colors when switching to dark mode in Flutter?
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.indigo,
iconTheme: const IconThemeData(color: Colors.black),
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.indigo,
iconTheme: const IconThemeData(color: Colors.white),
),
themeMode: ThemeMode.system,
home: const ExampleDeleteScreen(),
);
}
}
I applied these changes but the icon color does not change when I switch to dark mode. I would appreciate your help.
2
Answers
You need to define the color of the Icon to follow the ThemeData like this:
Please try this code