Skip to content

Instantly share code, notes, and snippets.

@ashishrawat2911
Created July 11, 2020 08:40
Show Gist options
  • Save ashishrawat2911/989377a5fc61da2a9d34be155c584bf3 to your computer and use it in GitHub Desktop.
Save ashishrawat2911/989377a5fc61da2a9d34be155c584bf3 to your computer and use it in GitHub Desktop.
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
context.bloc<MovieBloc>().add(LoadMovies());
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Movies"),
),
body: BlocBuilder<MovieBloc, ResultState<List<Movie>>>(
builder: (BuildContext context, ResultState<List<Movie>> state) {
return state.when(
loading: () {
return Center(child: CircularProgressIndicator());
},
idle: () {
return Container();
},
data: (List<Movie> data) {
return dataWidget(data);
},
error: (NetworkExceptions error) {
return Text(NetworkExceptions.getErrorMessage(error));
},
);
},
),
);
}
Widget dataWidget(List<Movie> data) {
return ListView.builder(
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 300,
width: 300,
child: Card(
elevation: 1,
child: Image.network(
"https://image.tmdb.org/t/p/w342${data[index].posterPath}",
),
),
);
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment