This is my code from a closed question: How to create a dropdown menu with sections?
The y position of the accepted anwser change. I don't understand why it's an accepted anwser.
import 'package:flutter/material.dart';
class CustomDropDownMenu extends StatefulWidget {
final Widget child;
const CustomDropDownMenu({Key? key, required this.child}) : super(key: key);
@override
_CustomDropDownMenu createState() => _CustomDropDownMenu();
}
class _CustomDropDownMenu extends State<CustomDropDownMenu>
with SingleTickerProviderStateMixin {
OverlayEntry? overlayEntry;
late AnimationController _animationController;
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this, duration: const Duration(milliseconds: 500));
}
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MouseRegion(
onExit: (onExit) {
setOnExitMenu(context, onExit.delta.dy);
},
child: GestureDetector(
onTap: () {
insertOverlay(context);
_animationController.forward();
},
child: widget.child),
);
}
void setOnExitMenu(BuildContext context, double delta) {
if (delta >= 0) {
return;
}
closeDropDown();
}
void insertOverlay(BuildContext context) {
closeDropDown();
final rect = findParamsData(context);
overlayEntry = _createOverlay(rect: rect);
Overlay.of(context)!.insert(overlayEntry!);
}
OverlayEntry _createOverlay({required Rect rect}) {
return OverlayEntry(builder: (context) {
return Material(
color: Colors.transparent,
child: Stack(
alignment: Alignment.center,
clipBehavior: Clip.none,
children: [
Positioned(
child: IgnorePointer(
child: Container(
color: Colors.black45,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
),
),
),
Positioned.fromRect(
rect: Rect.fromLTWH(
rect.left, rect.top, rect.width * 3, rect.height * 10),
child: SlideTransition(
position: Tween(
begin: const Offset(0, 0.15),
end: const Offset(0, 0))
.animate(_animationController),
child: MouseRegion(
onExit: (onExit) {
setOnExitMenu(context, -1);
},
child: const ContentDropDown(),
)
)),
],
),
);
});
}
Rect findParamsData(BuildContext context) {
final RenderBox renderBox = context.findRenderObject()! as RenderBox;
final size = renderBox.size;
final offset = renderBox.localToGlobal(Offset.zero);
return Rect.fromLTWH(
offset.dx, offset.dy + size.height, size.width, size.height);
}
void closeDropDown() {
if (overlayEntry != null) {
overlayEntry!.remove();
overlayEntry = null;
}
}
}
class ContentDropDown extends StatelessWidget {
const ContentDropDown({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Theme.of(context).colorScheme.secondary,
width: 0.5)),
child: ListView(
shrinkWrap: true,
children: [
TextFormField(),
ListView.builder(
shrinkWrap: true,
itemCount: 10,
itemBuilder: (context, index) {
return ListTile(
leading: Image.network(
"https://upload.wikimedia.org/wikipedia/nap/thumb/f/f3/Logo_UEFA_Champions_League.png/250px-Logo_UEFA_Champions_League.png"),
title: const Text("Champions League"),
);
}),
],
),
);
}
}