Skip to content

Commit

Permalink
chore: refactor bloc example
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-plavsic committed Jan 25, 2025
1 parent 13791f4 commit 7171404
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 54 deletions.
8 changes: 8 additions & 0 deletions examples/bloc/lib/cubit/theme_cubit.dart
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);
}
4 changes: 4 additions & 0 deletions examples/bloc/lib/di/providers.dart
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());
57 changes: 4 additions & 53 deletions examples/bloc/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -6,28 +8,18 @@ void main() {
runApp(const MyApp());
}

// ThemeCubit manages the light/dark theme state
class ThemeCubit extends Cubit<bool> {
// 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
Expand All @@ -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),
),
),
);
}
}
16 changes: 16 additions & 0 deletions examples/bloc/lib/ui/screens/theme_switcher_page.dart
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(),
),
);
}
}
30 changes: 30 additions & 0 deletions examples/bloc/lib/ui/widgets/themed_button.dart
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),
),
),
);
}
}
3 changes: 2 additions & 1 deletion examples/bloc/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ environment:
resolution: workspace

dependencies:
disco: ^0.0.2
disco:
path: ../../packages/disco
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
Expand Down

0 comments on commit 7171404

Please sign in to comment.