Your First Flutter App: An App From Scratch

Feb 22 2022 · Dart 2.14.1, Flutter 2.5, Visual Studio Code 1.6

Part 3: Create UI with Flutter

24. Manage Widget State

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 23. Add Spacing & Padding Next episode: 25. Work with Strings

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In Part 1, we used a Boolean variable to track the state of whether or not an alert was visible on the screen. We toggled the variable from false to true and back again based on user actions.

class GameModel {

}
static const sliderStart = 50;
static const scoreStart = 0;
static const roundStart = 1;
int target;
int current;
int totalScore;
int round;
GameModel(this.target,
      [this.current = sliderStart,
      this.totalScore = scoreStart,
      this.round = roundStart]);
import 'game_model.dart';
class _GamePageState extends State<GamePage> {
  late GameModel _model;
  @override
  void initState() {
    super.initState();
    _model = GameModel(50);
  }  
import 'game_model.dart';
const Control({Key? key, required this.model}) : super(key: key);
final GameModel model;
child: Slider(
  value: widget.model.current.toDouble(),
Slider(
  value: _currentValue,
  onChanged: (newValue) {
    setState(() {
      _currentValue = newValue;
      widget.model.current = newValue.toInt();
    });
  },
  min: 1.0,
  max: 100.0,
),
const Prompt(targetValue: _model.target),
Control(model: _model),
Score(totalScore: _model.totalScore, round: _model.round)