Created
August 1, 2025 11:21
-
-
Save bambuh/fcbb2ccf3f4702512b9d2a191c0c7152 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import 'package:bloc_presentation/bloc_presentation.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:flutter_bloc/flutter_bloc.dart'; | |
| sealed class CounterPresentationEvent {} | |
| class ShowEncouragementSnackBar extends CounterPresentationEvent { | |
| final int remaining; | |
| ShowEncouragementSnackBar(this.remaining); | |
| } | |
| class NavigateToSuccessPage extends CounterPresentationEvent {} | |
| typedef CounterState = int; | |
| class CounterCubit extends Cubit<CounterState> | |
| with BlocPresentationMixin<CounterState, CounterPresentationEvent> { | |
| CounterCubit() : super(0); | |
| void increment() { | |
| final newValue = state + 1; | |
| emit(newValue); | |
| if (newValue == 25 || newValue == 45) { | |
| final remaining = 50 - newValue; | |
| emitPresentation(ShowEncouragementSnackBar(remaining)); | |
| } else if (newValue == 50) { | |
| emitPresentation(NavigateToSuccessPage()); | |
| } | |
| } | |
| } | |
| class CounterPage extends StatelessWidget { | |
| const CounterPage({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return BlocPresentationListener<CounterCubit, CounterPresentationEvent>( | |
| listener: (context, event) { | |
| if (event is ShowEncouragementSnackBar) { | |
| ScaffoldMessenger.of(context).showSnackBar( | |
| SnackBar( | |
| content: Text('Keep it up! Only ${event.remaining} taps left.'), | |
| ), | |
| ); | |
| } else if (event is NavigateToSuccessPage) { | |
| Navigator.of( | |
| context, | |
| ).push(MaterialPageRoute(builder: (_) => const SuccessPage())); | |
| } | |
| }, | |
| child: BlocBuilder<CounterCubit, CounterState>( | |
| builder: (context, count) { | |
| return Scaffold( | |
| appBar: AppBar(title: const Text('Tap to 50')), | |
| body: Center( | |
| child: Column( | |
| mainAxisSize: MainAxisSize.min, | |
| children: [ | |
| const Text( | |
| 'Tap the button until you reach 50!', | |
| style: TextStyle(fontSize: 20), | |
| ), | |
| const SizedBox(height: 20), | |
| Text('$count', style: const TextStyle(fontSize: 48)), | |
| const SizedBox(height: 20), | |
| ElevatedButton( | |
| onPressed: () => context.read<CounterCubit>().increment(), | |
| child: const Text('Tap me'), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ); | |
| }, | |
| ), | |
| ); | |
| } | |
| } | |
| class SuccessPage extends StatelessWidget { | |
| const SuccessPage({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return const Scaffold( | |
| body: Center( | |
| child: Text( | |
| 'Congratulations! You reached 50 taps!', | |
| style: TextStyle(fontSize: 20), | |
| textAlign: TextAlign.center, | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| // --- Entry Point --- | |
| void main() { | |
| runApp(const CounterApp()); | |
| } | |
| class CounterApp extends StatelessWidget { | |
| const CounterApp({super.key}); | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Presentation events example', | |
| theme: ThemeData(primarySwatch: Colors.blue), | |
| home: BlocProvider( | |
| create: (_) => CounterCubit(), | |
| child: const CounterPage(), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment