Created
October 16, 2020 18:20
-
-
Save VB10/2cec77f081745355fa823a8414528a46 to your computer and use it in GitHub Desktop.
Call To Bloc Instance on init state
This file contains hidden or 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 'package:flutter_bloc/flutter_bloc.dart'; | |
import 'package:statemanagement/bloc/cats_cubit.dart'; | |
import 'package:statemanagement/bloc/cats_repository.dart'; | |
import 'package:statemanagement/bloc/cats_state.dart'; | |
class BlocCatsView extends StatefulWidget { | |
@override | |
_BlocCatsViewState createState() => _BlocCatsViewState(); | |
} | |
class _BlocCatsViewState extends State<BlocCatsView> { | |
BuildContext _context; | |
@override | |
void initState() { | |
// TODO: implement initState | |
super.initState(); | |
WidgetsBinding.instance.addPostFrameCallback((timeStamp) { | |
_context.bloc<CatsCubit>().getCats(); | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return BlocProvider( | |
create: (c) { | |
return CatsCubit(SampleCatsRepository()); | |
}, | |
child: buildScaffold(context), | |
); | |
} | |
Scaffold buildScaffold(BuildContext context) => Scaffold( | |
appBar: AppBar( | |
title: Text("Hello"), | |
), | |
body: BlocConsumer<CatsCubit, CatsState>( | |
listener: (c, state) { | |
if (state is CatsError) { | |
Scaffold.of(context).showSnackBar(SnackBar(content: Text(state.message))); | |
} | |
}, | |
builder: (c, state) { | |
_context = c; | |
if (state is CatsInitial) { | |
return buildCenterInitialChild(context); | |
} else if (state is CatsLoading) { | |
return buildCenterLoading(); | |
} else if (state is CatsCompleted) { | |
return buildListViewCats(state); | |
} else { | |
return buildError(state); | |
} | |
}, | |
), | |
); | |
Text buildError(CatsState state) { | |
final error = state as CatsError; | |
return Text(error.message); | |
} | |
ListView buildListViewCats(CatsCompleted state) { | |
return ListView.builder( | |
itemBuilder: (context, index) => ListTile( | |
title: Image.network(state.response[index].imageUrl), | |
subtitle: Text(state.response[index].description), | |
), | |
itemCount: state.response.length, | |
); | |
} | |
Center buildCenterLoading() { | |
return Center( | |
child: CircularProgressIndicator(), | |
); | |
} | |
Center buildCenterInitialChild(BuildContext context) { | |
return Center( | |
child: Column( | |
children: [Text("Hello"), buildFloatingActionButtonCall(context)], | |
), | |
); | |
} | |
FloatingActionButton buildFloatingActionButtonCall(BuildContext context) { | |
return FloatingActionButton( | |
child: Icon(Icons.clear_all), | |
onPressed: () { | |
context.bloc<CatsCubit>().getCats(); | |
}, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment