Last active
November 4, 2020 06:13
-
-
Save manofi21/5c5d10caeeb46423b7451257e408e74d to your computer and use it in GitHub Desktop.
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:flutter/material.dart'; | |
import 'package:flutter_grocery_shopping/counter_bloc.dart'; | |
import 'package:flutter_grocery_shopping/counter_event.dart'; | |
class HomePages extends StatefulWidget { | |
@override | |
_HomePagesState createState() => _HomePagesState(); | |
} | |
class _HomePagesState extends State<HomePages> { | |
// inisialisasi _bloc | |
final _bloc = CounterBloc(); | |
@override | |
void dispose(){ | |
super.dispose(); | |
_bloc.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("data"), | |
), | |
body: Center( | |
// StreamBuilder untuk menjalankan BLoC | |
child: StreamBuilder( | |
stream: _bloc.counter, | |
// menggunakan initialData, untuk menggantikan ProgressCircularIndicator | |
// karena StreamBuilder untuk mendapatkan nilai dan membutuhkan waktu. Tanpa inisialData, | |
// nilai yang keluar adalah "null" . | |
initialData: 0, | |
builder: (context, AsyncSnapshot<int> snapshot) { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [Text('lol'), Text('${snapshot.data}')], | |
); | |
})), | |
floatingActionButton: Row( | |
mainAxisAlignment: MainAxisAlignment.end, | |
children: [ | |
FloatingActionButton( | |
// memberikan event Increament | |
onPressed: () => _bloc.counterEventSink.add(IncreamentEvent()), | |
tooltip: 'Increament', | |
child: Icon(Icons.add), | |
), | |
SizedBox( | |
width: 10, | |
), | |
FloatingActionButton( | |
// memberikan event Decreament | |
onPressed: () => _bloc.counterEventSink.add(DecreamentEvent()), | |
tooltip: 'Decreament', | |
child: Icon(Icons.remove), | |
) | |
], | |
), | |
); | |
} | |
} |
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 'dart:async'; | |
import 'package:flutter_grocery_shopping/counter_event.dart'; | |
import 'package:flutter_grocery_shopping/counter_state.dart'; | |
import 'package:bloc/bloc.dart'; | |
// Alurnya: | |
//1. Event akan dikirim ke BLoC, | |
//2. Kemudian masukkan ke mapEventToState, | |
//3. Masukkan data yang di inisialisasi di BLoC | |
//4. Olah sesuai Event yang ditentukan; | |
class CounterBloc { | |
// inisialisasi _counter | |
int _counter = 0; | |
// StreamController = controller untuk menerima nilai dari _counter dan mengirim nilai dari stream | |
final _counterStateController = StreamController<int>(); | |
// StreamSink menerima nilai dari inisialisasi _counter dan mengirimnya ke _mapEventToState | |
StreamSink<int> get _inCounter => _counterStateController.sink; | |
// Stream<int> mengembalikan nilai dari _mapEventToState. counter kan ditampilkan ke UI | |
Stream<int> get counter => _counterStateController.stream; | |
// StreamController<CounterEvent> = controller untuk menerima dan mengambil event dari UI | |
final _counterEventController = StreamController<CounterEvent>(); | |
// Sink<CounterEvent> untuk mengirim event ke _mapEventToState | |
Sink<CounterEvent> get counterEventSink => _counterEventController.sink; | |
// Constructor di counter_bloc, untuk menghubungkan state dan value, yang berhubungan | |
CounterBloc(){ | |
_counterEventController.stream.listen(_mapEventToState); | |
} | |
void _mapEventToState(CounterEvent event) { | |
if (event is IncreamentEvent) | |
_counter++; | |
else | |
_counter--; | |
// kemudian disini _inCounter diganti dengan yang baru | |
_inCounter.add(_counter); | |
} | |
void dispose() { | |
_counterStateController.close(); | |
_counterEventController.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
// di file ini akan berisi event yang akan menentukan bagaimana data di olah | |
abstract class CounterEvent{} | |
class IncreamentEvent extends CounterEvent {} | |
class DecreamentEvent extends CounterEvent {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment