0

Hello i'm new in flutter development i try to make a simple quiz app and keep the score in a list.

I have a list of icons i put it in a Row widget so when i press the true or false button my list add a widget(icon) to my row and display it .

But my screen doesn't update when i press the button until i hot reload or hot restart the app

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(Quizzler());

class Quizzler extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        backgroundColor: Colors.grey.shade900,
        body: SafeArea(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 10.0),
            child: QuizPage(),
          ),
        ),
      ),
    );
  }
}

class QuizPage extends StatefulWidget {
  @override
  _QuizPageState createState() => _QuizPageState();
}

class _QuizPageState extends State<QuizPage> {
  List<Widget> scorekepper = [];

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        Expanded(
          flex: 5,
          child: Padding(
            padding: EdgeInsets.all(10.0),
            child: Center(
              child: Text(
                'This is where the question text will go.',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 25.0,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.all(15.0),
            child: FlatButton(
              textColor: Colors.white,
              color: Colors.green,
              child: Text(
                'True',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 20.0,
                ),
              ),
              onPressed: () {
                scorekepper.add(Icon(
                  Icons.check,
                  color: Colors.green,
                ));
              },
            ),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.all(15.0),
            child: FlatButton(
              color: Colors.red,
              child: Text(
                'False',
                style: TextStyle(
                  fontSize: 20.0,
                  color: Colors.white,
                ),
              ),
              onPressed: () {
                scorekepper.add(
                  Icon(
                    Icons.close,
                    color: Colors.red,
                  ),
                );
              },
            ),
          ),
        ),
        Row(
          children: scorekepper,
        ),
      ],
    );
  }
}

3 Answers 3

2
setState(() {
              scorekepper.add(
                Icon(
                  Icons.close,
                  color: Colors.red,
                ),
              );
            });
Sign up to request clarification or add additional context in comments.

Comments

1

You need to put setState(() {}); after your function. This will run the build function so that it can refresh your screen and display the icon. See more here.

 onPressed: () {
                scorekepper.add(Icon(
                  Icons.check,
                  color: Colors.green,
                ));
                setState(() {});
              },

Comments

0

What is happening that you are adding item to list but not refreshing the UI,so in order to refresh the UI you have many options, the easiest way is to do setState(){} on add, Although i recommend you to search about state management like: Bloc, Provider ...etc, which will optimize the performance the app when rebuilding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.