Last active
November 4, 2024 15:41
-
-
Save lukas-h/c405120004cb337e5b6a3c9cb75cf733 to your computer and use it in GitHub Desktop.
BloC Workshop
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'; | |
// Business LOgic Component | |
class CounterCubit extends Cubit<int> { | |
CounterCubit(int? initialValue) : super(initialValue ?? 0); | |
void increment() { | |
emit(state + 1); | |
} | |
void decrement() { | |
emit(state - 1); | |
} | |
Future<void> delayedIncrement() async { | |
await Future.delayed(const Duration(seconds: 1)); | |
emit(state + 1); | |
} | |
Future<void> delayedDecrement() async { | |
await Future.delayed(const Duration(seconds: 1)); | |
emit(state - 1); | |
} | |
void reset() { | |
emit(0); | |
} | |
} | |
void testCubit() { | |
final cubit = CounterCubit(5); | |
cubit.state; | |
cubit.stream.listen((event) {}); | |
cubit.increment(); | |
cubit.delayedIncrement(); | |
cubit.decrement(); | |
cubit.close(); | |
} |
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_workshop/counter_cubit.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_bloc/flutter_bloc.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return BlocProvider( | |
create: (context) => CounterCubit(0), | |
child: MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: const Text('Stream Example'), | |
), | |
body: Column( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisSize: MainAxisSize.max, | |
children: const [ | |
Counter(), | |
ResetCounter(), | |
], | |
), | |
), | |
), | |
); | |
} | |
} | |
class Counter extends StatelessWidget { | |
const Counter({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return Row( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
IconButton( | |
icon: const Icon(Icons.remove), | |
onPressed: () { | |
context.read<CounterCubit>().decrement(); | |
}, | |
), | |
BlocBuilder<CounterCubit, int>( | |
builder: (context, state) { | |
return Text('$state'); | |
}, | |
), | |
IconButton( | |
icon: const Icon(Icons.add), | |
onPressed: () { | |
context.read<CounterCubit>().increment(); | |
}, | |
), | |
], | |
); | |
} | |
} | |
class ResetCounter extends StatelessWidget { | |
const ResetCounter({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return IconButton( | |
icon: const Icon(Icons.refresh), | |
onPressed: () { | |
context.read<CounterCubit>().reset(); | |
}, | |
); | |
} | |
} |
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
Stream<int> generateStream() async* { | |
int counter = 1; | |
while (true) { | |
await Future.delayed(const Duration(seconds: 1)); | |
yield counter++; | |
} | |
} | |
main() { | |
final stream = generateStream(); | |
stream.listen((event) { | |
print(event); | |
}); | |
} |
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
main() async { | |
final list = [1, 2, 3]; | |
list.add(4); | |
list.removeAt(list.length - 1); | |
for (final item in list) { | |
print(item); | |
} | |
final doubledList = list.map((item) => item * 2).toList(); | |
final stream = Stream<int>.fromIterable([1, 2, 3]); | |
await for (final item in stream) { | |
print(item); | |
} | |
final doubledStream = stream.map((item) => item * 2); | |
stream.listen((event) { | |
print(event); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment