All Projects → felangel → inherited_stream

felangel / inherited_stream

Licence: MIT license
An inherited widget for Streams, which updates its dependencies when the stream emits data.

Programming Languages

C++
36643 projects - #6 most used programming language
CMake
9771 projects
dart
5743 projects
HTML
75241 projects
c
50402 projects - #5 most used programming language
swift
15916 projects

Inherited Stream

Pub build coverage License: MIT

An InheritedWidget for Streams, which updates its dependencies when the Stream emits.

Usage

Create an InheritedStream

class ProgressModel extends InheritedStream<ValueStream<double>> {
  const ProgressModel({
    Key? key,
    required ValueStream<double> stream,
    required Widget child,
  }) : super(key: key, stream: stream, child: child);

  static double of(BuildContext context) {
    return context
        .dependOnInheritedWidgetOfExactType<ProgressModel>()!
        .stream
        .value!;
  }
}

Insert into the Widget Tree

class _MyState extends State<MyPage> {
  final _subject = BehaviorSubject<double>.seeded(0.0);

  @override
  void dispose() {
    _subject.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ProgressModel(
        stream: _subject.stream,
        child: Progress(),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _subject.add(Random.nextDouble()),
      ),
    );
  }
}

Register Dependant(s)

class Progress extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CircularProgressIndicator(value: ProgressModel.of(context));
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].