0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Flutter] カウントダウン機能を Stream + StreamBuilder + Providerで実現する。

Posted at

Stream とStreamBuilder, Provider を使って、簡単なカウントダウン機能を実現してみます。

Aug-09-2020 23-03-20.gif

StreamController にロジックを書き、WidgetにProviderで流し込んでます。

StreamBuilderを使うことで、データが変わるとこだけリビルドできるので、Statefull Widget の setStateより効率的です。ロジックとViewも分離できます。


class CountdownController {
  CountdownController();

  var _controller = StreamController<int>();
  var _count = 10;
  var _max = 10;
  get stream => _controller.stream; // streamにアクセスできるようにする

  Future<void> countdownStart() async {
    _count = _max;
    while (_count > 0) {
      await Future.delayed(Duration(seconds: 1));
      _count--;
      _controller.add(_count); //データを流す。
    }
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // Providerでロジックを注入
    return Provider(
      create: (_) => CountdownController(),
      // 同じ階層ではProviderを使えないためBuilderもしくは別のWidgetに切り出す必要がある。
      child: Builder( 
        builder: (context) => Scaffold(
          body: Center(
            child: StreamBuilder(
              stream: Provider.of<CountdownController>(context).stream,
              // snapShot.dataでstreamを取得する。
              builder: (context, snapShot) {
                if (!snapShot.hasData)
                  return Text("Start Timer");
                else
                  return Text(snapShot.data.toString());
              },
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: Provider.of<CountdownController>(context).countdownStart,
            child: Icon(Icons.play_arrow),
          ),
        ),
      ),
    );
  }
}

最近知ったのですが、BLoCパターンはStreamを使ってロジックとViewを分離するものなんですね。この実装も簡単ですがBLoCもどきということでしょう。

この記事はブログのクロス投稿です。
https://shuent.qrunch.io/entries/jHL5XPzalsNWZbz2

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?