-
-
Save red-star25/b50a7e6e2ee260422a012ddabd8a5f4f to your computer and use it in GitHub Desktop.
This file contains 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: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