Skip to content

Instantly share code, notes, and snippets.

@100xA
Created October 8, 2025 08:42
Show Gist options
  • Select an option

  • Save 100xA/fad34529ad1efff45f63e6a4e9d91799 to your computer and use it in GitHub Desktop.

Select an option

Save 100xA/fad34529ad1efff45f63e6a4e9d91799 to your computer and use it in GitHub Desktop.
Stack, Alertdialogs und ScaffoldMessenger
import 'package:flutter/material.dart';
void main() => runApp(const SnackbarDialogDemoApp());
class SnackbarDialogDemoApp extends StatelessWidget {
const SnackbarDialogDemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SnackBars & AlertDialogs Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: true,
colorSchemeSeed: Colors.redAccent,
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
void _showSimpleSnackBar(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
showCloseIcon: true,
content: Text('Hello! 👋 This is a simple SnackBar.'),
duration: Duration(seconds: 10),
),
);
}
void _showSnackBarWithAction(BuildContext context) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.green,
content: const Text('File deleted'),
action: SnackBarAction(
label: 'UNDO',
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Undo successful ✅')),
);
},
),
),
);
}
Future<void> _showBasicDialog(BuildContext context) async {
await showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Basic Dialog'),
content: const Text('This is a simple AlertDialog message.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'),
),
],
),
);
}
Future<void> _showConfirmDialog(BuildContext context) async {
final confirmed = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Delete item?'),
content: const Text('This action cannot be undone. Continue?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Delete'),
),
],
),
);
if (confirmed == true) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Item deleted 🗑️')),
);
} else if (confirmed == false) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Action canceled ❌')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('SnackBars & AlertDialogs')),
body: Padding(
padding: const EdgeInsets.all(16),
child: ListView(
children: [
const Text(
'🧩 SnackBar Examples',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
FilledButton.icon(
onPressed: () => _showSimpleSnackBar(context),
icon: const Icon(Icons.message),
label: const Text('Show Simple SnackBar'),
),
const SizedBox(height: 8),
FilledButton.icon(
onPressed: () => _showSnackBarWithAction(context),
icon: const Icon(Icons.undo),
label: const Text('Show SnackBar with UNDO Action'),
),
const Divider(height: 32),
const Text(
'💬 AlertDialog Examples',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 12),
FilledButton.icon(
onPressed: () => _showBasicDialog(context),
icon: const Icon(Icons.info_outline),
label: const Text('Show Basic Dialog'),
),
const SizedBox(height: 8),
FilledButton.icon(
onPressed: () => _showConfirmDialog(context),
icon: const Icon(Icons.delete_outline),
label: const Text('Show Confirmation Dialog'),
),
],
),
),
);
}
}
import 'package:flutter/material.dart';
void main() => runApp(const StackDemoApp());
class StackDemoApp extends StatelessWidget {
const StackDemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Stack Example',
debugShowCheckedModeBanner: false,
theme: ThemeData(useMaterial3: true, colorSchemeSeed: Colors.indigo),
home: const StackExample(),
);
}
}
class StackExample extends StatelessWidget {
const StackExample({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView(
children: [
Text("Tasd"),
Text("Tasd"),
SizedBox(
height: 2000,
),
Stack(
alignment: Alignment.center,
children: [
Container(
width: 250,
height: 250,
color: Colors.blue.shade100,
),
Container(
width: 150,
height: 150,
decoration: BoxDecoration(
color: Colors.indigo.shade300,
shape: BoxShape.circle,
),
),
const Text(
'Stack Example',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
Positioned.fill(
child: Container(
color: Colors.red,
child: Text("data"),
),
),
],
),
],
)),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment