Skip to content

Instantly share code, notes, and snippets.

@hardy716
Last active January 30, 2024 16:17
Show Gist options
  • Save hardy716/cf67b6a1b7a349b55eac350108682388 to your computer and use it in GitHub Desktop.
Save hardy716/cf67b6a1b7a349b55eac350108682388 to your computer and use it in GitHub Desktop.
플러터챌린지 7일차 기본문제 - 신현호

플러터챌린지 7일차 기본문제 - 신현호

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
debugShowCheckedModeBanner: false,
home: HelloOverlayScreen(),
),
);
}
class HelloOverlayScreen extends StatefulWidget {
const HelloOverlayScreen({super.key});
@override
State<HelloOverlayScreen> createState() => _HelloOverlayScreenState();
}
class _HelloOverlayScreenState extends State<HelloOverlayScreen> {
OverlayEntry? currentOverlayEntry;
final double buttonHeight = 50.0;
void showOverlay(BuildContext context, int index) {
currentOverlayEntry?.remove();
currentOverlayEntry = null;
OverlayEntry overlayEntry = OverlayEntry(
builder: (context) => Positioned(
top: MediaQuery.of(context).viewInsets.bottom + buttonHeight * index - 25,
left: MediaQuery.of(context).size.width / 2 - 20,
child: IgnorePointer(
child: Material(
elevation: 4.0,
child: Container(
margin: const EdgeInsets.all(10),
color: Colors.white,
child: const Text('⬇️ You clicked this 😎'),
),
),
),
),
);
currentOverlayEntry = overlayEntry;
Overlay.of(context).insert(overlayEntry);
Future.delayed(const Duration(seconds: 2), () {
overlayEntry.remove();
if (currentOverlayEntry == overlayEntry) {
currentOverlayEntry = null;
}
});
}
@override
Widget build(BuildContext context) {
const List<String> words = ['Hello!', 'Press', 'any', 'button!'];
return Scaffold(
appBar: AppBar(title: const Text('Hello Overlay')),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: List<Widget>.generate(
words.length,
(index) => SizedBox(
width: double.infinity,
height: buttonHeight,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
child: ElevatedButton(
onPressed: () => showOverlay(context, index + 1),
child: Text(words[index]),
),
),
),
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment