Created
July 7, 2019 09:59
-
-
Save SAGARSURI/2e5b94f78a343e8d7cc26b67752c11da 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/material.dart'; | |
import '../models/item_model.dart'; | |
import '../blocs/movies_bloc.dart'; | |
class MovieList extends StatefulWidget { | |
final MoviesBloc _bloc; | |
MovieList(this._bloc); | |
@override | |
State<StatefulWidget> createState() { | |
return MovieListState(); | |
} | |
} | |
class MovieListState extends State<MovieList> { | |
@override | |
void initState() { | |
super.initState(); | |
widget._bloc.init(); | |
widget._bloc.fetchAllMovies(); | |
} | |
@override | |
void dispose() { | |
widget._bloc.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Popular Movies'), | |
), | |
body: StreamBuilder( | |
stream: widget._bloc.allMovies, | |
builder: (context, AsyncSnapshot<ItemModel> snapshot) { | |
if (snapshot.hasData) { | |
return buildList(snapshot); | |
} else if (snapshot.hasError) { | |
return Text(snapshot.error.toString()); | |
} | |
return Center(child: CircularProgressIndicator()); | |
}, | |
), | |
); | |
} | |
Widget buildList(AsyncSnapshot<ItemModel> snapshot) { | |
return GridView.builder( | |
itemCount: snapshot.data.results.length, | |
gridDelegate: | |
new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2), | |
itemBuilder: (BuildContext context, int index) { | |
return GridTile( | |
child: InkResponse( | |
enableFeedback: true, | |
child: Image.network( | |
'https://image.tmdb.org/t/p/w185${snapshot.data | |
.results[index].poster_path}', | |
fit: BoxFit.cover, | |
), | |
onTap: () => openDetailPage(snapshot.data, index), | |
), | |
); | |
}); | |
} | |
openDetailPage(ItemModel data, int index) { | |
Navigator.pushNamed( | |
context, | |
'movieDetail', arguments: data.results[index]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment