-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13791f4
commit 7171404
Showing
6 changed files
with
64 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
/// ThemeCubit manages the light/dark theme state | ||
class ThemeCubit extends Cubit<bool> { | ||
ThemeCubit() : super(false); // false means light, true means dark theme | ||
|
||
void toggleTheme() => emit(!state); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import 'package:bloc_example/cubit/theme_cubit.dart'; | ||
import 'package:disco/disco.dart'; | ||
|
||
final themeProvider = Provider((_) => ThemeCubit()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:bloc_example/ui/widgets/themed_button.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class ThemeSwitcherPage extends StatelessWidget { | ||
const ThemeSwitcherPage({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(title: const Text('Cubit Theme Example')), | ||
body: const Center( | ||
child: ThemedButton(), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import 'package:bloc_example/di/providers.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
class ThemedButton extends StatelessWidget { | ||
const ThemedButton({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final themeCubit = themeProvider.of(context); | ||
return BlocBuilder( | ||
bloc: themeCubit, | ||
builder: (context, bool isDarkMode) => ElevatedButton( | ||
onPressed: themeCubit.toggleTheme, | ||
style: ElevatedButton.styleFrom( | ||
backgroundColor: isDarkMode ? Colors.grey[800] : Colors.lightBlue, | ||
foregroundColor: isDarkMode ? Colors.white : Colors.black, | ||
padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 16), | ||
shape: RoundedRectangleBorder( | ||
borderRadius: BorderRadius.circular(12), | ||
), | ||
), | ||
child: Text( | ||
isDarkMode ? 'Switch to Light Mode' : 'Switch to Dark Mode', | ||
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters