Skip to content

Instantly share code, notes, and snippets.

@ricardogobbo
Created December 15, 2019 21:54
Show Gist options
  • Save ricardogobbo/42fd3350c4683ddea243f2fb9975477a to your computer and use it in GitHub Desktop.
Save ricardogobbo/42fd3350c4683ddea243f2fb9975477a to your computer and use it in GitHub Desktop.
//////////////////////////////////////////////////////////////////////
/// Código do main.dart
//////////////////////////////////////////////////////////////////////
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return BlocProvider(
blocs: [
Bloc((i) => VideoSearchBloc())
],
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomeScreen(),
),
);
}
}
//////////////////////////////////////////////////////////////////////
// codigo do video_search_bloc.dart
//////////////////////////////////////////////////////////////////////
class VideoSearchBloc implements BlocBase{
List<Video> videos = [];
final _videosController = StreamController<List<Video>>();
Stream<List<Video>> get outVideos => _videosController.stream;
final _searchController = StreamController<String>();
Sink get inSearch => _searchController.sink;
VideoSearchBloc(){
_searchController.stream.listen(_search);
}
_search(query) async{
videos = await API().search(search: query);
_videosController.sink.add(videos);
print(videos);
}
@override
void addListener(listener) {
// TODO: implement addListener
}
@override
void dispose() {
_videosController.close();
_searchController.close();
}
@override
// TODO: implement hasListeners
bool get hasListeners => null;
@override
void notifyListeners() {
// TODO: implement notifyListeners
}
@override
void removeListener(listener) {
// TODO: implement removeListener
}
}
//////////////////////////////////////////////////////////////////////
// codigo do StreamBuilder dentro do HomeScreen
//////////////////////////////////////////////////////////////////////
StreamBuilder(
stream: BlocProvider.getBloc<VideoSearchBloc>().outVideos,
initialData: List<Video>(),
builder: (context, snapshot){
print('grite!!');
print(BlocProvider.getBloc<VideoSearchBloc>().outVideos);
if(snapshot.hasData){
//print('we have data!');
List<Video> list = snapshot.data;
return ListView.separated(
physics: BouncingScrollPhysics(),
separatorBuilder: (context, i){
return Divider(height: 2, color: Colors.grey);
},
itemCount: list.length,
itemBuilder: (context, index){
return Column(
children: <Widget>[
Image.network(list[index].thumb),
Text(list[index].title, style: TextStyle(fontSize:16, color: Colors.white),),
Text(list[index].channel, style: TextStyle(color: Colors.white),),
SizedBox(height: 16)
],
);
}
);
}
return Center(child: CircularProgressIndicator());
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment