Build: Counter Screen
A simple screen that shows a number and a button to increase it. The number updates when the button is pressed.
Target UI
------------------- | Counter: 0 | | | | [ Increase ] | -------------------
------------------- | Counter: 0 | | | | [ Increase ] | -------------------
import 'package:flutter/material.dart'; class CounterScreen extends StatefulWidget { @override State<CounterScreen> createState() => _CounterScreenState(); } class _CounterScreenState extends State<CounterScreen> { int _count = 0; void _increment() { // TODO: Increase the count and update UI } @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'), ), ], ), ), ); } }
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.
------------------- | Counter: 1 | | | | [ Increase ] | -------------------
StatefulWidget in Flutter?StatefulWidget class in Flutter?class CounterWidget extends StatefulWidget {
@override
_CounterWidgetState createState() => _CounterWidgetState();
}
class _CounterWidgetState extends State<CounterWidget> {
int count = 0;
void increment() {
setState(() {
count += 1;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Count: $count'),
ElevatedButton(onPressed: increment, child: Text('Add'))
],
);
}
}class MyWidget extends StatefulWidget {
@override
State<MyWidget> createState() {
return _MyWidgetState();
}
}
class _MyWidgetState extends State<MyWidget> {
int counter = 0;
void increment() {
counter += 1;
}
@override
Widget build(BuildContext context) {
return Text('Counter: $counter');
}
}StatefulWidget that increments the count only if the current count is even. Which modification to the increment method below achieves this?
void increment() {
setState(() {
count += 1;
});
}Pass the test and this pattern moves unknown