I use the following function to check and display the content either in Dialog or Bottom Sheet, but when executing it does not work properly, as it displays both together, what is the reason and how can the problem be solved?
Content function:
content(BuildContext context, dynamic dialog, dynamic bottomSheet) {
(MediaQuery.of(context).orientation == Orientation.landscape) ? dialog : bottomSheet;
}
Implementation:
ElevatedButton(
child: Text('Button'),
onPressed: () {
content(context, dialog(context), bottomSheet(context));
},
),
How can this be solved?
4
Answers
The reason the function is not working properly is because you’re not actually showing the dialog or bottom sheet. To show the dialog or bottom sheet, you need to call showDialog or showModalBottomSheet, respectively, and pass them the result of calling dialog or bottomSheet.
try this
In order to determine the Orientation of the screen, we can use the OrientationBuilder Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.
here are screenshots:
happy coding…
You have a fundamental misunderstanding as to what your code is doing.
Take your "Implementation" and
revealContent
code, for example:You think that
revealContent
will invoke eitherdialog
orbottomSheet
based on the orientation of the screen. What you are actually doing, however, is you are invoking both of them and then passing the result of the invocations torevealContent
, which isn’t actually doing anything with them.What you need to be doing is passing the functions as callbacks to
revealContent
and then invoking the callbacks within the function:You should be calling
showDialog
andshowModalBottomSheet
insiderevealContent
.Dialog
BottomSheet
Reveal Content