I'm new to flutter, I'm building an app using bloc for state management. I'm trying to toggle theme mode using IconButton on the appBar but the theme won't change when I press the button. how can I fix that ? I'm creating this app by watching a tutorial and I did everything like it except for the theme manager part as he had the theme data in the main.dart file I figured it'd be better to separate it.
AppCubit
class AppCubit extends Cubit<AppStates> {
AppCubit() : super(NewsInitialState());
static AppCubit get(context) => BlocProvider.of(context);
bool isDark = false;
void changeAppMode() {
isDark = !isDark;
emit(AppChangeModeState());
}
}
AppStates
class AppChangeModeState extends AppStates {}
ThemeManager
class MyThemes {
static final darkTheme = ThemeData(
primarySwatch: Colors.deepOrange,
scaffoldBackgroundColor: HexColor('01000E'),
appBarTheme: AppBarTheme(
titleSpacing: 20,
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: HexColor('01000E'),
statusBarIconBrightness: Brightness.light),
backgroundColor: HexColor('01000E'),
elevation: 0,
titleTextStyle: const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
iconTheme: const IconThemeData(
color: Colors.white,
)),
bottomNavigationBarTheme: BottomNavigationBarThemeData(
type: BottomNavigationBarType.fixed,
selectedItemColor: Colors.deepOrange,
unselectedItemColor: Colors.grey,
elevation: 20,
backgroundColor: HexColor('01000E'),
),
textTheme: const TextTheme(
bodyText1: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: Colors.white,
)));
static final lightTheme = ThemeData(
primarySwatch: Colors.deepOrange,
scaffoldBackgroundColor: Colors.white,
appBarTheme: const AppBarTheme(
titleSpacing: 20,
systemOverlayStyle: SystemUiOverlayStyle(
statusBarColor: Colors.white,
statusBarIconBrightness: Brightness.dark),
backgroundColor: Colors.white,
elevation: 0,
titleTextStyle: TextStyle(
color: Colors.black,
fontSize: 20,
fontWeight: FontWeight.bold,
),
iconTheme: IconThemeData(
color: Colors.black,
),
),
textTheme: const TextTheme(
bodyText1: TextStyle(
fontSize: 18,
fontWeight: FontWeight.w600,
color: Colors.black,
),
),
bottomNavigationBarTheme: const BottomNavigationBarThemeData(
type: BottomNavigationBarType.fixed,
selectedItemColor: Colors.deepOrange,
elevation: 20,
backgroundColor: Colors.white,
));
}
HomeLayout
class HomeLayout extends StatefulWidget {
const HomeLayout({Key? key}) : super(key: key);
@override
State<HomeLayout> createState() => _HomeLayoutState();
}
class _HomeLayoutState extends State<HomeLayout> {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => AppCubit()
..getBusiness()
..getSports()
..getScience(),
child: BlocConsumer<AppCubit, AppStates>(
listener: (context, state) {},
builder: (context, state) {
var cubit = AppCubit.get(context);
return Scaffold(
appBar: AppBar(
title: const Text(
'News App',
),
actions: [
IconButton(
onPressed: () {},
icon: const Icon(Icons.search),
),
IconButton(
onPressed: () {
AppCubit.get(context).changeAppMode();
},
icon: const Icon(Icons.dark_mode),
),
],
),
body: cubit.screens[cubit.currentIndex],
bottomNavigationBar: BottomNavigationBar(
currentIndex: cubit.currentIndex,
onTap: (index) {
cubit.changeBottomNavBar(index);
},
items: cubit.bottomItems,
),
);
},
),
);
}
main.dart
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (BuildContext context) => AppCubit(),
child: BlocConsumer<AppCubit, AppStates>(
listener: (context, state) {},
builder: (context, state) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: MyThemes.lightTheme,
darkTheme: MyThemes.darkTheme,
themeMode:
AppCubit.get(context).isDark ? ThemeMode.dark : ThemeMode.light,
home: const HomeLayout(),
);
},
),
);
}
}