Last active
August 13, 2019 00:30
-
-
Save mono0926/52c8441960cdd5b62aaf31d9eb336072 to your computer and use it in GitHub Desktop.
This file contains 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 'package:flutter/widgets.dart'; | |
import 'package:rxdart/rxdart.dart'; | |
/// - https://raw.githubusercontent.com/Kavantix/bloc_streambuilder/master/lib/src/value_observable_builder.dart | |
/// - https://twitter.com/_mono/status/1078948544784941058 | |
class ValueObservableBuilder<T> extends StatefulWidget { | |
const ValueObservableBuilder({ | |
Key key, | |
@required this.builder, | |
@required this.stream, | |
this.child, | |
}) : super(key: key); | |
final ValueObservable<T> stream; | |
final AsyncWidgetBuilder<T> builder; | |
final Widget child; | |
@override | |
_ValueObservableBuilderState createState() => | |
_ValueObservableBuilderState<T>(); | |
} | |
class _ValueObservableBuilderState<T> extends State<ValueObservableBuilder<T>> { | |
@override | |
Widget build(BuildContext context) { | |
return StreamBuilder<T>( | |
initialData: widget.stream.value, | |
stream: widget.stream, | |
builder: (context, snapshot) { | |
if (snapshot.connectionState == ConnectionState.waiting && | |
snapshot.hasData) { | |
snapshot = AsyncSnapshot<T>.withData( | |
ConnectionState.active, | |
snapshot.data, | |
); | |
} | |
return widget.builder( | |
context, | |
snapshot, | |
widget.child, | |
); | |
}, | |
); | |
} | |
} | |
typedef AsyncWidgetBuilder<T> = Widget Function( | |
BuildContext context, | |
AsyncSnapshot<T> snapshot, | |
Widget child, | |
); |
仰る通りなるべくネストはさせたくないですね。。
child は他にネットで見かけた StreamBuilder 拡張よりもよいなーと感じたので、自作のコードに採用させて頂いております 🙏
返信ありがとうございます!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rebuild避けたい場合は、
child
に指定すれば良いだけで、不要な最適化だと思い直したので、このGistおよびパッケージ版で常にリビルドするように変更しました。(
child
で逃している部分はリビルド対象外のままです。)