diff --git a/examples/bloc/lib/cubit/theme_cubit.dart b/examples/bloc/lib/cubit/theme_cubit.dart new file mode 100644 index 0000000..93f2edf --- /dev/null +++ b/examples/bloc/lib/cubit/theme_cubit.dart @@ -0,0 +1,8 @@ +import 'package:flutter_bloc/flutter_bloc.dart'; + +/// ThemeCubit manages the light/dark theme state +class ThemeCubit extends Cubit { + ThemeCubit() : super(false); // false means light, true means dark theme + + void toggleTheme() => emit(!state); +} diff --git a/examples/bloc/lib/di/providers.dart b/examples/bloc/lib/di/providers.dart new file mode 100644 index 0000000..d0572c0 --- /dev/null +++ b/examples/bloc/lib/di/providers.dart @@ -0,0 +1,4 @@ +import 'package:bloc_example/cubit/theme_cubit.dart'; +import 'package:disco/disco.dart'; + +final themeProvider = Provider((_) => ThemeCubit()); diff --git a/examples/bloc/lib/main.dart b/examples/bloc/lib/main.dart index 732eaf8..f78ea98 100644 --- a/examples/bloc/lib/main.dart +++ b/examples/bloc/lib/main.dart @@ -1,3 +1,5 @@ +import 'package:bloc_example/di/providers.dart'; +import 'package:bloc_example/ui/screens/theme_switcher_page.dart'; import 'package:disco/disco.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -6,28 +8,18 @@ void main() { runApp(const MyApp()); } -// ThemeCubit manages the light/dark theme state -class ThemeCubit extends Cubit { - // false means Light theme, true means Dark theme - ThemeCubit() : super(false); - - static final themeProvider = Provider((_) => ThemeCubit()); - - void toggleTheme() => emit(!state); -} - class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return ProviderScope( - providers: [ThemeCubit.themeProvider], + providers: [themeProvider], child: Builder( builder: (context) => BlocBuilder( // NB: When injecting the cubit, the context has to be a descendant // of ProviderScope (and not what MyApp.build provides). - bloc: ThemeCubit.themeProvider.of(context), + bloc: themeProvider.of(context), builder: (context, bool isDarkMode) { return MaterialApp( theme: isDarkMode @@ -41,44 +33,3 @@ class MyApp extends StatelessWidget { ); } } - -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(), - ), - ); - } -} - -class ThemedButton extends StatelessWidget { - const ThemedButton({super.key}); - - @override - Widget build(BuildContext context) { - final themeCubit = 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), - ), - ), - ); - } -} diff --git a/examples/bloc/lib/ui/screens/theme_switcher_page.dart b/examples/bloc/lib/ui/screens/theme_switcher_page.dart new file mode 100644 index 0000000..b6809e5 --- /dev/null +++ b/examples/bloc/lib/ui/screens/theme_switcher_page.dart @@ -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(), + ), + ); + } +} diff --git a/examples/bloc/lib/ui/widgets/themed_button.dart b/examples/bloc/lib/ui/widgets/themed_button.dart new file mode 100644 index 0000000..6d39cec --- /dev/null +++ b/examples/bloc/lib/ui/widgets/themed_button.dart @@ -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), + ), + ), + ); + } +} diff --git a/examples/bloc/pubspec.yaml b/examples/bloc/pubspec.yaml index 48f0075..10e99ab 100644 --- a/examples/bloc/pubspec.yaml +++ b/examples/bloc/pubspec.yaml @@ -9,7 +9,8 @@ environment: resolution: workspace dependencies: - disco: ^0.0.2 + disco: + path: ../../packages/disco flutter: sdk: flutter flutter_bloc: ^9.0.0