Last active
October 3, 2019 08:03
-
-
Save nhancv/3fc294230d4279d2c869e2f971570353 to your computer and use it in GitHub Desktop.
Simple bloc
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:rxdart/rxdart.dart'; | |
| import 'package:flutter/material.dart'; | |
| void main() => runApp(MyApp()); | |
| class MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| title: 'Flutter Demo', | |
| theme: ThemeData( | |
| primarySwatch: Colors.blue, | |
| ), | |
| home: MyHomePage(title: 'Flutter Demo Home Page'), | |
| ); | |
| } | |
| } | |
| class MyHomePage extends StatefulWidget { | |
| MyHomePage({Key key, this.title}) : super(key: key); | |
| final String title; | |
| @override | |
| _MyHomePageState createState() => _MyHomePageState(); | |
| } | |
| class _MyHomePageState extends State<MyHomePage> { | |
| final bloc = new TestBloc(); | |
| @override | |
| void dispose() { | |
| bloc.dispose(); | |
| super.dispose(); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: Text(widget.title), | |
| ), | |
| body: Center( | |
| child: Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| Text( | |
| 'You have pushed the button this many times:', | |
| ), | |
| StreamBuilder( | |
| stream: bloc.counter, | |
| builder: (context, snapshot) { | |
| return Text( | |
| '${snapshot.data ?? 0}', | |
| style: Theme.of(context).textTheme.display1, | |
| ); | |
| }, | |
| ), | |
| TestStateless(), | |
| TestStateful() | |
| ], | |
| ), | |
| ), | |
| floatingActionButton: FloatingActionButton( | |
| onPressed: () { | |
| bloc.increment(); | |
| }, | |
| tooltip: 'Increment', | |
| child: Icon(Icons.add), | |
| ), | |
| ); | |
| } | |
| } | |
| class TestStateless extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| print('build TestStateless'); | |
| return Text('TestStateless'); | |
| } | |
| } | |
| class TestStateful extends StatefulWidget { | |
| @override | |
| _TestStatefulState createState() => _TestStatefulState(); | |
| } | |
| class _TestStatefulState extends State<TestStateful> { | |
| @override | |
| Widget build(BuildContext context) { | |
| print('build TestStateful'); | |
| return Text('_TestStatefulState'); | |
| } | |
| } | |
| class TestBloc { | |
| //Model | |
| int _counter = 0; | |
| //Input to bloc | |
| var subject = BehaviorSubject<int>(); | |
| //Output from bloc | |
| Stream<int> get counter => | |
| subject.map((data) => this._counter += data); | |
| void increment() { | |
| subject.add(1); | |
| } | |
| void dispose() { | |
| subject.close(); | |
| } | |
| } |
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
| dependencies: | |
| flutter: | |
| sdk: flutter | |
| cupertino_icons: ^0.1.2 | |
| rxdart: ^0.19.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment