Help us understand the problem. What is going on with this article?

Flutter DemoアプリをBLoCパターンで書き直してみた

More than 1 year has passed since last update.

Flutterアプリの新規作成時にできているでもアプリをBLoCパターンで書き直してみました。
BLoCの詳しい説明はここ
https://www.youtube.com/watch?v=PLHln7wHgPE&app=desktop

BLoCパターンとは

BLoCとはBusiness Logic Componentの略で、BLoCを使ったアプリの実装パターンをblocパターンと呼ぶ。
コンポーネントと言っても、viewを構築するコンポーネントではなく、「ひとかたまりの要素」という意味でのコンポーネントである。

以下の方針に従って開発を進めればBLoCパターンであると言える。

  • BLoCの入出力インターフェースはStream,Sinkでやる
  • BLoCの依存は注入可能で環境に依存しない
  • BLoC内に環境ごとの条件分岐は持たない
  • 以上のルールに基づくなら実装は自由

BLoCで実装

新規作成時にできている以下のアプリをBLoCパターンにしていく

スクリーンショット 2018-06-30 21.42.37.png

main.dart
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:rxdart/rxdart.dart';

// BLoCオブジェクト作成
class CounterBloc {
  StreamController counterAdditionController = StreamController();
  Sink get counterAddition => counterAdditionController.sink;

  BehaviorSubject<int> _count = BehaviorSubject<int>(seedValue: 0);
  Stream<int> get countString => _count.stream;

  CounterBloc(){
    counterAdditionController.stream.listen( (addition) {
      _count.add(_count.value + 1);
    });
  }
}

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  String title;

  MyHomePage({this.title});
  CounterBloc counterBloc = CounterBloc();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(
              'You have pushed the button this many times:',
            ),
            // データストリームから現在のカウントを取得
            StreamBuilder<int>(
              stream: counterBloc.countString,
              builder: (context, snapshot) =>
                  Text(
                    snapshot.data.toString(),
                  ),
            ),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
         // ボタンが押された時Streamに通知
        onPressed: () => counterBloc.counterAddition.add(null),
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした