Skip to content

Instantly share code, notes, and snippets.

@rena2019
Created September 15, 2025 05:43
Show Gist options
  • Save rena2019/87fdd1c8826512dc4ee14a19540e814b to your computer and use it in GitHub Desktop.
Save rena2019/87fdd1c8826512dc4ee14a19540e814b to your computer and use it in GitHub Desktop.
Flutter showDialog
// showDialog
/*
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Dialog(
child: Text('DIALOG'),
);
},
);
}
*/
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: 'Simple Flutter App',
theme: ThemeData(primarySwatch: Colors.blue),
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _counter = 0;
final TextEditingController _controller = TextEditingController();
String _display = '';
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _increment() {
setState(() => _counter++);
}
void _updateDisplay() {
setState(() => _display = _controller.text);
_controller.clear();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Simple Flutter App')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text('Counter: $_counter', style: const TextStyle(fontSize: 24)),
const SizedBox(height: 12),
ElevatedButton(
onPressed: () {
showDialog(
context: context,
// barrierDismissible: false,//click outside of dialog -> no close dialog!
builder: (context) {
return Dialog(
//backgroundColor: Colors.white, //Colors.transparent,
//insetPadding: const EdgeInsets.all(200), // padding screen - dialog
child: ThreeButtons(
onPrimary: () => debugPrint('Primary pressed'),
onSecondary: () => debugPrint('Secondary pressed'),
onIcon: () => Navigator.of(context).pop(), //close
),
);
},
);
},
child: const Text('Increment'),
),
const Divider(height: 32),
TextField(
controller: _controller,
decoration: const InputDecoration(
labelText: 'Gib etwas ein',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 8),
ElevatedButton(
onPressed: _updateDisplay,
child: const Text('Anzeigen'),
),
const SizedBox(height: 12),
if (_display.isNotEmpty)
Text(
'Eingegeben: $_display',
style: const TextStyle(fontSize: 18),
),
],
),
),
);
}
}
class ThreeButtons extends StatelessWidget {
final VoidCallback? onPrimary;
final VoidCallback? onSecondary;
final VoidCallback? onIcon;
const ThreeButtons({Key? key, this.onPrimary, this.onSecondary, this.onIcon})
: super(key: key);
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(onPressed: onPrimary, child: const Text('Primary')),
OutlinedButton(onPressed: onSecondary, child: const Text('Secondary')),
IconButton(
onPressed: onIcon,
icon: const Icon(Icons.thumb_up),
tooltip: 'Like',
),
],
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment