I tried these widget and these don’t work.
showDialog(
context: context,
builder: (BuildContext context) {
return Positioned(
.....,
child: Dialog( child: .....),
);
},
);
showGeneralDialog(
context: context,
pageBuilder: (BuildContext buildContext, Animation animation, Animation secondaryAnimation) {
return Positioned(
.....,
child: .....,
);
},
);
The only dialog function working is showMenu
showMenu(
items: [],
context: context,
position: .....,
);
But this function is hard to custom. ex. It can’t remove space.
For example, I using this at this moment.
import 'package:flutter/material.dart';
class PositionDialogCustomWidget extends StatefulWidget {
final Widget child;
final Widget dialog;
final double left;
final double top;
const PositionDialogCustomWidget({super.key, required this.child, required this.dialog, required this.left, required this.top});
@override
State<PositionDialogCustomWidget> createState() => _PositionDialogCustomWidgetState();
}
class _PositionDialogCustomWidgetState extends State<PositionDialogCustomWidget> {
//controller
bool _isShowDialog = false;
//basic widget
late final _overlayEntry = OverlayEntry(
builder: (BuildContext context) => Positioned(
left: widget.left,
top: widget.top,
child: Material(child: widget.dialog),
),
);
void _closeDialog() {
if (!_isShowDialog) return;
_overlayEntry.remove();
Navigator.pop(context);
_isShowDialog = false;
}
void _showDialog() {
_isShowDialog = true;
showDialog(
context: context,
barrierColor: Colors.transparent,
builder: (BuildContext context) => GestureDetector(onTap: _closeDialog),
);
Overlay.of(context).insert(_overlayEntry);
}
@override
void dispose() {
_closeDialog();
super.dispose();
}
@override
Widget build(BuildContext context) {
return InkWell(onTap: _showDialog, child: widget.child);
}
}
This custom widget can show the dialog widget at this position and this can click outside of the dialog widget area to remove this dialog.
But this solution is not direct. It is an indirect solution like using the Stack
widget.
Is there any way to set the position for any show dialog function?
3
Answers
From this answer below:
Positioned
should only be used inside aStack
widget. From the documentation:Try using an
Align
widget instead.There is also
alignment
property forDialog
which you can control how to align theDialog
.From this answer, the
Positioned
widget can't be used with the dialog function. So I do this:I tried to use the
Align
widget instead of thePositioned
widget by calculating the align value from the position value and I got this function to do that.and I tried to test that with the example below and it worked successfully.
I implement this solution to create a custom widget below.
Positioned
should only be used inside aStack
widget. From the documentation:Try using an
Align
widget instead.There is also
alignment
property forDialog
which you can control how to align theDialog
.You could use a
Stack
widget as parent, then you can use thePositioned
, something like this:Result:
More info about Stack: https://api.flutter.dev/flutter/widgets/Stack-class.html