Build: Counter Screen
A simple screen that shows a number and a button. When the button is pressed, the number increases by one using setState to update the UI.
Target UI
------------------- | Counter: 0 | | | | [ Increment ] | -------------------
------------------- | Counter: 0 | | | | [ Increment ] | -------------------
import 'package:flutter/material.dart'; class CounterScreen extends StatefulWidget { @override State<CounterScreen> createState() => _CounterScreenState(); } class _CounterScreenState extends State<CounterScreen> { int _counter = 0; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Counter Screen')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Counter: $_counter', style: TextStyle(fontSize: 24)), SizedBox(height: 20), ElevatedButton( onPressed: () { // TODO: Add code to increment counter using setState }, child: Text('Increment'), ), ], ), ), ); } }
import 'package:flutter/material.dart'; class CounterScreen extends StatefulWidget { @override State<CounterScreen> createState() => _CounterScreenState(); } class _CounterScreenState extends State<CounterScreen> { int _counter = 0; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Counter Screen')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Counter: $_counter', style: TextStyle(fontSize: 24)), SizedBox(height: 20), ElevatedButton( onPressed: () { setState(() { _counter += 1; }); }, child: Text('Increment'), ), ], ), ), ); } }
This screen uses a StatefulWidget to hold a local integer state called _counter. The setState method is called inside the button's onPressed callback to tell Flutter that the state changed. Inside setState, we increase _counter by 1. Flutter then rebuilds the UI, showing the updated number. This is the basic way to update local state and refresh the UI in Flutter.
------------------- | Counter: 1 | | | | [ Increment ] | -------------------
setState() inside a StatefulWidget in Flutter?setState()setState() tells Flutter that the widget's state has changed and the UI should update.setState().setState() in Flutter?setState syntaxsetState(() { ... });StatefulWidget:
int count = 0;
void increment() {
setState(() {
count += 2;
});
}
// Widget build shows count value
What will be the displayed count after calling increment() twice starting from 0?increment() adds 2 to count.StatefulWidget?
int counter = 0;
void increment() {
counter += 1;
}
// UI shows counter value
Why does the UI not update when increment() is called?setState() is called to notify changes.counter but does not call setState(), so UI does not refresh.setState() is not called to notify Flutter of the change -> Option CsetState()?setState() trigger UI updates.setState(). if (count % 2 == 0) {
count += 2;
} else {
count += 1;
} updates state outside setState, so UI won't refresh. setState(() => count += (count % 2 == 0) ? 2 : 1); uses arrow syntax but must return void, so it's invalid. count = (count % 2 == 0) ? count + 2 : count + 1; updates state without setState.Pass the test and this pattern moves unknown