Last active
June 29, 2024 04:27
-
-
Save felangel/75f1ca6fc954f3672daf7962577d56f5 to your computer and use it in GitHub Desktop.
showDialog sample
This file contains hidden or 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 '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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have some issues with how to properly show dialog with flutter_bloc;
check the code below and tell me where i do mistake.
//my bloc code