Created
December 15, 2019 21:54
-
-
Save ricardogobbo/42fd3350c4683ddea243f2fb9975477a 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
////////////////////////////////////////////////////////////////////// | |
/// 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