Last active
June 16, 2022 22:08
-
-
Save eli1stark/ceeda8d679454415f0c1c6ffaa8cb8b7 to your computer and use it in GitHub Desktop.
Template for BLoC using Counter example
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
import 'package:bloc/bloc.dart'; | |
import 'package:freezed_annotation/freezed_annotation.dart'; | |
part 'counter_bloc.freezed.dart'; | |
part 'counter_event.dart'; | |
part 'counter_state.dart'; | |
typedef _Event = CounterEvent; | |
typedef _Emit = Emitter<CounterState>; | |
class CounterBloc extends Bloc<CounterEvent, CounterState> { | |
CounterBloc() : super(const CounterState()) { | |
on<Initial>(_initital); | |
on<Increment>(_increment); | |
on<Decrement>(_decrement); | |
add(const Initial()); | |
} | |
void _initital(_Event event, _Emit emit) { | |
emit(state.copyWith(value: 100)); | |
} | |
void _increment(_Event event, _Emit emit) { | |
emit(state.copyWith(value: state.value + 1)); | |
} | |
void _decrement(_Event event, _Emit emit) { | |
emit(state.copyWith(value: state.value - 1)); | |
} | |
} |
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
part of 'counter_bloc.dart'; | |
@freezed | |
class CounterEvent with _$CounterEvent { | |
const factory CounterEvent.initial() = Initial; | |
const factory CounterEvent.increment() = Increment; | |
const factory CounterEvent.decrement() = Decrement; | |
} |
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
part of 'counter_bloc.dart'; | |
@freezed | |
class CounterState with _$CounterState { | |
const factory CounterState({ | |
@Default(0) int value, | |
}) = _CounterState; | |
} |
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
class CounterView extends StatelessWidget { | |
const CounterView({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MultiBlocProvider( | |
providers: [ | |
BlocProvider( | |
create: (_) => CounterBloc(), | |
), | |
], | |
child: const _CounterView(), | |
); | |
} | |
} | |
class _CounterView extends StatelessWidget { | |
const _CounterView(); | |
@override | |
Widget build(BuildContext context) { | |
final bloc = context.watch<CounterBloc>(); | |
return Scaffold( | |
body: Center( | |
child: Column( | |
children: [ | |
Text('${bloc.state.value}'), | |
], | |
), | |
), | |
floatingActionButton: Column( | |
mainAxisAlignment: MainAxisAlignment.end, | |
crossAxisAlignment: CrossAxisAlignment.end, | |
children: [ | |
FloatingActionButton( | |
onPressed: () => bloc.add(const Increment()), | |
child: const Icon(Icons.add), | |
), | |
const SizedBox(height: 8), | |
FloatingActionButton( | |
onPressed: () => bloc.add(const Decrement()), | |
child: const Icon(Icons.remove), | |
), | |
], | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment