Last active
February 2, 2022 10:57
-
-
Save behrends/cc822a6eb0ef86ee8381b32638579bd6 to your computer and use it in GitHub Desktop.
TIF19 Flutter Counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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