Skip to content

Instantly share code, notes, and snippets.

@hariangr
Created March 30, 2019 17:42
Show Gist options
  • Save hariangr/0c54129602a38b6570dec8b4a62ba998 to your computer and use it in GitHub Desktop.
Save hariangr/0c54129602a38b6570dec8b4a62ba998 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MainPage(),
);
}
}
class MainPage extends StatelessWidget {
final iBloc = IntBloc();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Put something'),
),
body: CustomScrollView(
slivers: <Widget>[
StreamBuilder<int>(
stream: iBloc.currentInt,
builder: (ctx, snapshot) {
// This one works
// return SliverToBoxAdapter(
// child: Text('trying using sliver'),
// );
switch (snapshot.connectionState) {
case ConnectionState.active:
// Neither of these works
// return Text('active');
return SliverToBoxAdapter(
child: Text('active'),
);
break;
case ConnectionState.done:
return Text('done');
break;
case ConnectionState.waiting:
return Text('waiting');
break;
default:
return Text('something');
}
},
)
],
),
floatingActionButton: FloatingActionButton(
onPressed: () {
iBloc.addOne();
},
child: Icon(Icons.add),
),
);
}
}
class IntBloc {
int n = 0;
final _intBloc = BehaviorSubject<int>();
Observable<int> get currentInt => _intBloc.stream;
IntBloc() {
_intBloc.add(n);
}
void addOne() {
n += 1;
_intBloc.add(n);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment