Created
February 17, 2023 07:07
-
-
Save jerrypm/94132dddd444ad66b6c6d2d0ff7df3d1 to your computer and use it in GitHub Desktop.
add_count.dart
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 'dart:async'; | |
import 'package:flutter/material.dart'; | |
class AddItemsPage extends StatefulWidget { | |
@override | |
AddItemsPageState createState() => AddItemsPageState(); | |
} | |
class AddItemsPageState extends State<AddItemsPage> { | |
// const AddItemsPage({super.key}); | |
CounterBloc bloc = CounterBloc(); | |
@override | |
void dispose() { | |
bloc.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Colors.white, | |
foregroundColor: Colors.grey[900], | |
title: const Text('Add Items'), | |
), | |
// bottomNavigationBar: BottomNavigationBar( | |
// items: const <BottomNavigationBarItem>[ | |
// BottomNavigationBarItem( | |
// icon: Icon(Icons.calculate), | |
// label: 'Calc', | |
// ), | |
// BottomNavigationBarItem( | |
// icon: Icon(Icons.camera), | |
// label: 'Camera', | |
// ), | |
// BottomNavigationBarItem( | |
// icon: Icon(Icons.chat), | |
// label: 'Chat', | |
// ), | |
// ], | |
// ), | |
body: Center( | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
StreamBuilder( | |
stream: bloc.output, | |
initialData: bloc.counter, | |
builder: (BuildContext context, AsyncSnapshot snapshot) { | |
return Text( | |
"Angka: ${snapshot.data}", | |
style: TextStyle( | |
fontSize: 20.0, | |
), | |
); | |
}, | |
), | |
const SizedBox( | |
width: 20.0, | |
), | |
Row( | |
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | |
children: [ | |
IconButton( | |
onPressed: () { | |
bloc.inputan.add("minus"); | |
}, | |
icon: Icon(Icons.remove), | |
), | |
IconButton( | |
onPressed: () { | |
bloc.inputan.add("add"); | |
}, | |
icon: Icon(Icons.add), | |
) | |
], | |
), | |
], | |
), | |
)); | |
} | |
} | |
class CounterBloc { | |
int _counter = 0; | |
int get counter => _counter; | |
StreamController _inputController = StreamController(); | |
StreamSink get inputan => _inputController.sink; | |
StreamController _outputController = StreamController(); | |
StreamSink get sinkOut => _outputController.sink; | |
Stream get output => _outputController.stream; | |
CounterBloc() { | |
_inputController.stream.listen((event) { | |
if (event == "add") { | |
_counter++; | |
} else { | |
_counter--; | |
} | |
sinkOut.add(_counter); | |
}); | |
} | |
void dispose() { | |
_inputController.close(); | |
_outputController.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment