Overlay example
unknown
dart
a year ago
2.8 kB
8
Indexable
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: MyHomePage(),
),
);
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Overlay Example"),
),
body: Center(
child: ElevatedButton(
onPressed: () {
OverlayMessage(context).showOverlayMessage(
"¡Hola!",
"Este es un mensaje en overlay.",
);
},
child: Text("Mostrar Overlay"),
),
),
);
}
}
class OverlayMessage {
final BuildContext context;
OverlayEntry? _overlayEntry;
OverlayMessage(this.context);
void showOverlayMessage(String title, String subtitle) {
_overlayEntry = OverlayEntry(
builder: (context) => Positioned(
top: 50.0,
left: MediaQuery.of(context).size.width * 0.1,
right: MediaQuery.of(context).size.width * 0.1,
child: Material(
color: Colors.transparent,
child: Stack(
children: [
Container(
width: double.infinity,
padding: EdgeInsets.all(16.0),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
boxShadow: [
BoxShadow(
color: Colors.black26,
blurRadius: 10.0,
offset: Offset(0, 4),
),
],
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
title,
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 8.0),
Text(
subtitle,
style: TextStyle(
fontSize: 16.0,
),
),
],
),
),
Positioned(
right: 8.0,
top: 8.0,
child: GestureDetector(
onTap: () {
_removeOverlay();
},
child: Icon(
Icons.close,
color: Colors.black,
),
),
),
],
),
),
),
);
Overlay.of(context).insert(_overlayEntry!);
}
void _removeOverlay() {
_overlayEntry?.remove();
_overlayEntry = null;
}
}
Editor is loading...
Leave a Comment