Skip to content

Instantly share code, notes, and snippets.

@behrends
Last active February 2, 2022 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behrends/cc822a6eb0ef86ee8381b32638579bd6 to your computer and use it in GitHub Desktop.
Save behrends/cc822a6eb0ef86ee8381b32638579bd6 to your computer and use it in GitHub Desktop.
TIF19 Flutter Counter
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: const MyHomePage(title: 'Zähler'),
theme: ThemeData(
primarySwatch: Colors.orange,
)
);
}
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0; // initialer Zustand
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
const Text('So oft wurde der Button gedrückt:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline2,
)
]),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Plus 1',
child: const Icon(Icons.add),
));
}
}
class MyHomePage extends StatefulWidget {
final String title; // Instanzvariable
// Konstruktor
const MyHomePage({
required this.title,
});
@override
_MyHomePageState createState() => _MyHomePageState();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment