Skip to content

Instantly share code, notes, and snippets.

@felangel felangel/main.dart
Last active Jul 23, 2019

Embed
What would you like to do?
[flutter_bloc_recipes] Show SnackBar
import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:meta/meta.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
builder: (context) => DataBloc(),
child: MaterialApp(
home: Home(),
),
);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home')),
body: BlocListener<DataBloc, DataState>(
listener: (context, state) {
if (state is Success) {
Scaffold.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.green,
content: Text('Success'),
),
);
}
},
child: BlocBuilder<DataBloc, DataState>(
builder: (context, state) {
if (state is Initial) {
return Center(child: Text('Press the Button'));
}
if (state is Loading) {
return Center(child: CircularProgressIndicator());
}
if (state is Success) {
return Center(child: Text('Success'));
}
},
),
),
floatingActionButton: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
child: Icon(Icons.play_arrow),
onPressed: () {
BlocProvider.of<DataBloc>(context).dispatch(FetchData());
},
),
],
),
);
}
}
@immutable
abstract class DataEvent {}
class FetchData extends DataEvent {}
@immutable
abstract class DataState {}
class Initial extends DataState {}
class Loading extends DataState {}
class Success extends DataState {}
class DataBloc extends Bloc<DataEvent, DataState> {
@override
DataState get initialState => Initial();
@override
Stream<DataState> mapEventToState(
DataEvent event,
) async* {
if (event is FetchData) {
yield Loading();
await Future.delayed(Duration(seconds: 2));
yield Success();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.