Skip to content

Instantly share code, notes, and snippets.

@red-star25
Created January 6, 2022 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save red-star25/b50a7e6e2ee260422a012ddabd8a5f4f to your computer and use it in GitHub Desktop.
Save red-star25/b50a7e6e2ee260422a012ddabd8a5f4f to your computer and use it in GitHub Desktop.
import 'package:bloc_api_example/bloc/joke_bloc/joke_bloc.dart';
import 'package:bloc_api_example/data/repositories/joke_repository.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => JokeBloc(
RepositoryProvider.of<JokeRepository>(context),
)..add(LoadJokeEvent()),
child: Scaffold(
appBar: AppBar(
title: const Text('The Joke App'),
),
body: BlocBuilder<JokeBloc, JokeState>(
builder: (context, state) {
if (state is JokeLoadingState) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (state is JokeLoadedState) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ExpansionTile(
title: Text(
state.joke.setup,
textAlign: TextAlign.center,
),
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
state.joke.delivery,
style: const TextStyle(
fontSize: 20,
),
textAlign: TextAlign.center,
),
),
],
),
ElevatedButton(
onPressed: () {
BlocProvider.of<JokeBloc>(context).add(LoadJokeEvent());
},
child: const Text('Load New Joke'),
),
],
),
);
}
if (state is JokeErrorState) {
return Center(
child: Text(state.error.toString()),
);
}
return Container();
},
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment