Skip to content

Instantly share code, notes, and snippets.

@unacorbatanegra
Created April 17, 2024 00:52
Show Gist options
  • Save unacorbatanegra/da34632fac5111e903fed72bf57ad611 to your computer and use it in GitHub Desktop.
Save unacorbatanegra/da34632fac5111e903fed72bf57ad611 to your computer and use it in GitHub Desktop.
Overlay Entry
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@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> {
final focusNode = FocusNode();
@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>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
focusNode: focusNode,
onTap: onTap,
),
),
],
),
),
);
}
OverlayEntry? entry;
void onTap() {
entry = OverlayEntry(
builder: (context) => Positioned(
left: 8,
top: focusNode.offset.dy - (128 + 24),
right: 8,
child: GestureDetector(
onTap: () => entry!.remove(),
child: Material(
borderRadius: BorderRadius.circular(8),
elevation: 10,
child: Container(
width: MediaQuery.of(context).size.width,
height: 128,
margin: const EdgeInsets.all(4.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
color: Colors.white,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: List.generate(
3,
(index) => Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: List.generate(
8,
(index) => Text('${index + 1}'),
),
),
)),
),
),
),
),
);
Overlay.of(context).insert(entry!);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment