Skip to content

Instantly share code, notes, and snippets.

@felangel
Last active May 11, 2021 13:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felangel/75f1ca6fc954f3672daf7962577d56f5 to your computer and use it in GitHub Desktop.
Save felangel/75f1ca6fc954f3672daf7962577d56f5 to your computer and use it in GitHub Desktop.
showDialog sample
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:bloc/bloc.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final _dialogBloc = DialogBloc();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Demo'),
),
body: BlocBuilder(
bloc: _dialogBloc,
builder: (BuildContext context, DialogState state) {
if (state is DialogVisible) {
SchedulerBinding.instance.addPostFrameCallback((_) {
showDialog(
context: context,
barrierDismissible: false,
builder: (_) {
return Scaffold(
body: Center(
child: RaisedButton(
child: Text('dismiss'),
onPressed: () {
_dialogBloc.dispatch(HideDialog());
},
),
),
);
},
);
});
}
if (state is DialogHidden) {
SchedulerBinding.instance.addPostFrameCallback((_) {
Navigator.pop(context);
});
}
return Container(
child: Center(
child: Text('Dialog Sample'),
),
);
},
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.notifications),
onPressed: () {
_dialogBloc.dispatch(ShowDialog());
},
),
);
}
@override
void dispose() {
_dialogBloc.dispose();
super.dispose();
}
}
abstract class DialogEvent {}
class ShowDialog extends DialogEvent {}
class HideDialog extends DialogEvent {}
abstract class DialogState {}
class InitialDialogState extends DialogState {}
class DialogHidden extends DialogState {}
class DialogVisible extends DialogState {}
class DialogBloc extends Bloc<DialogEvent, DialogState> {
@override
DialogState get initialState => InitialDialogState();
@override
Stream<DialogState> mapEventToState(
DialogState currentState,
DialogEvent event,
) async* {
if (event is ShowDialog) {
yield DialogVisible();
} else {
yield DialogHidden();
}
}
}
@nicodoss
Copy link

nicodoss commented Aug 10, 2020

Login dart - studentworld - Visual Studio Code
i notice that the bloc show the dialog but when i inject the dialogclose event this don't dismiss the dialog.
Another think that i notice is when i use bloclistener the dialog just show once.
See bloc delegate transition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment