class MySwitcher extends StatelessWidget {
const MySwitcher({super.key});
@override
Widget build(BuildContext context) {
final status = context.select((MyBloc bloc) => bloc.state.status);
switch (status) {
case MyStatus.initial:
case MyStatus.loading:
return const Center(child: CircularProgressIndicator());
case MyStatus.success:
return const MyView();
case MyStatus.error:
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text('Something went wrong.'),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
context.read<MyBloc>().add(MyDataRequested());
},
child: const Text('Try again'),
),
],
),
);
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter