Skip to content

Instantly share code, notes, and snippets.

@achinverma
Created July 5, 2021 05:38
Show Gist options
  • Save achinverma/cca20a751f4fa26a9468b7ec594c652c to your computer and use it in GitHub Desktop.
Save achinverma/cca20a751f4fa26a9468b7ec594c652c to your computer and use it in GitHub Desktop.
class HomeScreen extends StatefulWidget {
HomeScreen();
@override
_HomeScreenState createState() {
return _HomeScreenState();
}
}
class _HomeScreenState extends State<HomeScreen> {
final postBloc = PostBloc();
@override
void initState() {
super.initState();
postBloc.eventSink.add(PostBlocAction.FetchPosts);
}
@override
void dispose() {
super.dispose();
postBloc.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: StreamBuilder<PostResponse>(
stream: postBloc.postStream,
builder: (context, snapshotPostData) {
if (snapshotPostData.hasData) {
if (snapshotPostData.data == null) {
return Container();
} else {
if(snapshotPostData.data == null || snapshotPostData.data.length == 0){
return Text("No Data Found!");
}else{
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.fromLTRB(0, 20, 0, 0),
child: ListView.builder(
padding: EdgeInsets.only(
top: 20.0,
left: 20.0,
bottom: 20.0,
right: 20.0,
),
itemCount: snapshotPostData.data.length,
itemBuilder: (context, index) {
return Padding(
padding: EdgeInsets.only(
bottom: 5,
),
child: ListTile(
title: snapshotPostData.data[index].title,
subtitle: snapshotPostData.data[index].body,
),
);
},
),
);
}
}
} else if (snapshotPostData.hasError) {
return Padding(
padding: EdgeInsets.only(
top: 10.0,
left: 20.0,
bottom: 20.0,
right: 20.0,
),
child: Center(
child: Text('${snapshotPostData.error}'),
),
);
}
return Padding(
padding: EdgeInsets.all(20.0),
child: Center(
child: CircularProgressIndicator(
valueColor: new AlwaysStoppedAnimation<Color>(
Colors.blue)),
)
);
},
), );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment