I am trying to show an overlay widget whenever the software keyboard appears on the screen. The overlay appears at the top of the keyboard. I've included the code below. I'm stuck in a problem with exception stating "Flutter Error. The specified entry is already present in the target Overlay". I am not sure how to fix this. Can someone help?
Code:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: const MyHomePage(),
);
}
}
class KeyboardAwareWidget extends StatefulWidget {
final Widget child;
final Widget? keyboardWidget;
const KeyboardAwareWidget({
required this.child,
this.keyboardWidget,
Key? key,
}) : super(key: key);
@override
State<KeyboardAwareWidget> createState() => _KeyboardAwareWidgetState();
}
class _KeyboardAwareWidgetState extends State<KeyboardAwareWidget> {
OverlayEntry? _overlayEntry;
@override
void initState() {
super.initState();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_rebuildOverlay();
}
void _rebuildOverlay() {
WidgetsBinding.instance.addPostFrameCallback((_) {
final mediaQuery = MediaQuery.of(context);
final keyboardHeight = mediaQuery.viewInsets.bottom;
if (keyboardHeight > 0) {
// Show the overlay only when the keyboard is visible.
_showOverlay(keyboardHeight);
} else {
_hideOverlay();
}
});
}
void _showOverlay(double keyboardHeight) {
_overlayEntry ??= OverlayEntry(
builder:
(context) => Positioned(
bottom: keyboardHeight,
left: 0,
right: 0,
child: Material(
elevation: 8.0,
child: widget.keyboardWidget ?? Container(),
),
),
);
Overlay.of(context).insert(_overlayEntry!);
}
void _hideOverlay() {
_overlayEntry?.remove();
_overlayEntry = null;
}
@override
void dispose() {
_hideOverlay();
super.dispose();
}
@override
Widget build(BuildContext context) {
return widget.child;
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Keyboard Aware Widget')),
body: Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const TextField(
decoration: InputDecoration(hintText: 'Tap to show keyboard'),
),
const SizedBox(height: 20),
ElevatedButton(onPressed: () {}, child: const Text('A Button')),
],
),
),
),
bottomNavigationBar: KeyboardAwareWidget(
child: Container(
height: 100,
color: Colors.amber,
child: const Center(child: Text('Main Content')),
),
keyboardWidget: Container(
height: 50,
color: Colors.blue,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(onPressed: () {}, icon: const Icon(Icons.add)),
IconButton(onPressed: () {}, icon: const Icon(Icons.remove)),
IconButton(onPressed: () {}, icon: const Icon(Icons.send)),
],
),
),
),
);
}
}