import 'package:flutter/material.dart';
class CounterScreen extends StatefulWidget {
@override
State<CounterScreen> createState() => _CounterScreenState();
}
class _CounterScreenState extends State<CounterScreen> {
int _count = 0;
void _increment() {
setState(() {
_count += 1;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Counter Screen')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Counter: $_count', style: TextStyle(fontSize: 24)),
SizedBox(height: 20),
ElevatedButton(
onPressed: _increment,
child: Text('Increase'),
),
],
),
),
);
}
}
This app uses a StatefulWidget because the number shown changes when the user taps the button. The _count variable holds the current number. When the button is pressed, the _increment method calls setState() to tell Flutter to update the UI with the new count. This is how Flutter knows to redraw the screen with the updated number.
The UI shows the count in a large text and a button labeled 'Increase' below it. Pressing the button increases the count by 1.