admin管理员组文章数量:1377676
I’m working on a Flutter app and trying to show a widget when the keyboard appears while ensuring that the TextField stays fixed in its position.
I want to show widget when keyboard is shown while keeping textfield position like this (zalo app):
Or I want widget overlay keyboard like this:
I want when I tap a button, it will show a widget while screen not resize and the textfield keep its position.
I’m working on a Flutter app and trying to show a widget when the keyboard appears while ensuring that the TextField stays fixed in its position.
I want to show widget when keyboard is shown while keeping textfield position like this (zalo app):
Or I want widget overlay keyboard like this:
I want when I tap a button, it will show a widget while screen not resize and the textfield keep its position.
Share Improve this question edited Mar 19 at 6:46 DarkBee 15.5k8 gold badges72 silver badges117 bronze badges asked Mar 19 at 0:25 Dương Nguyễn Thị NgọcDương Nguyễn Thị Ngọc 193 bronze badges1 Answer
Reset to default -1Try this code which will show an overlay when longPressed
on ListTile
and will hide the overlay
when tap outside
class OverlayDemo extends StatefulWidget {
const OverlayDemo({super.key});
@override
State<OverlayDemo > createState() => _OverlayDemoState();
}
class _OverlayDemoState extends State<OverlayDemo > {
OverlayEntry? _overlayEntry;
void showOverlay(BuildContext context, Offset position, int index) {
hideOverlay();
_overlayEntry = OverlayEntry(
builder:
(context) => SizedBox(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Stack(
children: [
ModalBarrier(onDismiss: hideOverlay),
Positioned(
left: position.dx,
top: position.dy,
child: Material(
color: Colors.transparent,
child: GestureDetector(
onTap: hideOverlay,
child: Container(
padding: EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.black87,
borderRadius: BorderRadius.circular(8),
),
child: Text(
"Item $index Options",
style: TextStyle(color: Colors.white),
),
),
),
),
),
],
),
),
);
Overlay.of(context).insert(_overlayEntry!);
}
void hideOverlay() {
_overlayEntry?.remove();
_overlayEntry = null;
}
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: 10,
itemBuilder: (_, index) {
return GestureDetector(
onLongPressStart:
(details) => showOverlay(context, details.globalPosition, index),
child: ListTile(title: Text("Item $index")),
);
},
);
}
}
if you want to learn more about overlay checkout this blog https://www.flutteris/blog/fr/astuce-6-calque-suite-a-un-longpress-dans-un-scroll-container
本文标签:
版权声明:本文标题:flutter - Show widget when keyboard is shown while keeping textfield position. Or overlay widget on keyboard (not above keyboard 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744486112a2608459.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论