After I inserted an OverlayEntry, I would like to disable any user interaction on the underlying Widget so that you can only interact with the OverlayEntry. How could I do this?
I’ve run the example code below inside Chrome. After I’ve clicked on the button to open the OverlayEntry, I can just enter TAB which focuses the TextField where I can enter text.
Is it possible to make the OverlayEntry absorb any key input so that it doesn’t reach the Widget underneath it? Or failing that, is it somehow possible to fully disable the Widget’s user interaction until the OverlayEntry is closed?
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
OverlayEntry? overlayEntry;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const TextField(
decoration: InputDecoration(
labelText: "Input",
),
),
TextButton(
onPressed: () {
overlayEntry = _newOverlayEntry();
Overlay.of(context).insert(overlayEntry!);
},
child: const Text("Open Overlay"),
),
],
),
),
);
}
OverlayEntry _newOverlayEntry() {
return OverlayEntry(
builder: (context) {
final Size mediaSize = MediaQuery.of(context).size;
return Container(
color: Theme.of(context).disabledColor,
width: mediaSize.width,
height: mediaSize.height,
child: Align(
alignment: Alignment.centerRight,
child: Container(
width: 200,
height: 200,
color: Colors.green,
child: TextButton(
onPressed: () {
overlayEntry?.remove();
overlayEntry?.dispose();
overlayEntry = null;
},
child: const Text("Close"),
),
),
),
);
},
);
}
}
denny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Use showDialog
instead of OverlayEntry
OverlayEntry is not really meant to do what you want to achieve. Overlay is similar to Stack. What you need is a Dialog
that you can push onto the navigator with the showDialog
function:
class _MyHomePageState extends State<MyHomePage> {
OverlayEntry? overlayEntry;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const TextField(
decoration: InputDecoration(
labelText: "Input",
),
),
TextButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => Dialog(
insetPadding: EdgeInsets.zero,
alignment: Alignment.centerRight,
child: Container(
width: 200,
height: 200,
color: Colors.green,
child: TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("Close"),
),
),
),
);
},
child: const Text("Open Overlay"),
),
],
),
),
);
}
}
This will push the Dialog
into a new route (which basically is a layer) which prevents the user from interacting with the route beneath this one.
0