2

I'm creating a quiz app and need to display mcq options dynamically based on how many options there are for a particular question.

So for example:

enter image description here

Now the code for the buttons is here :

    final quizOptions = Container(
      width: MediaQuery.of(context).size.width,
      child: Center(
        child: Column(
          children: <Widget>[
            SimpleRoundButton(
                backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
                buttonText: Text(questions[questionNum].options[0], 
                    style: TextStyle(
                        color: Colors.white
                    ),
                ),
                textColor: Colors.white,
                onPressed: (){},
            ),
            SimpleRoundButton(
                backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
                buttonText: Text(questions[questionNum].options[1], 
                    style: TextStyle(
                        color: Colors.white
                    ),
                ),
                textColor: Colors.white,
                onPressed: (){},
            ),
          ],
        ),
      ),
    );

As you can see, what I am able to do is to "fix" 2 buttons. Is there a way to dynamically add buttons based on how many options there are for that particular question ?

I have a list named questions and it is a list of questions (which is a class):

class Question {
  String title;
  List options;
  String imagePath;

  Question(
      {this.title, this.options, this.imagePath,});
}


//Example:
Question(
 title: "How fast does the drone go ?",
 options: ['80km/h', '90km/h', '100km/h'],
 imagePath: "assets/images/drones1.jpg",
)

2 Answers 2

6

You should iterate through your options to create SimpleRoundButton

...................................
    child: Column(
              children: questions[questionNum].options.map<Widget>(
                (option) =>  SimpleRoundButton(
                    backgroundColor: Color.fromRGBO(58, 66, 86, 1.0),
                    buttonText: Text(option, 
                        style: TextStyle(
                            color: Colors.white
                        ),
                    ),
                    textColor: Colors.white,
                    onPressed: (){},
                ),
           ).toList(),
.........................
Sign up to request clarification or add additional context in comments.

1 Comment

Yep this worked thanks :)), I had to use .map<Widget>( for it to work because I was getting this error :stackoverflow.com/questions/49603021/…
0

I made a complete example that I use dynamic widgets to show and hide widgets on screen, you can see it running online on dart fiddle, too.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List item = [
    {"title": "Button One", "color": 50},
    {"title": "Button Two", "color": 100},
    {"title": "Button Three", "color": 200},
    {"title": "No show", "color": 0, "hide": '1'},
  ];

  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text("Dynamic Widget - List<Widget>"),backgroundColor: Colors.blue),
        body: Column(
          children: <Widget>[
            Center(child: buttonBar()),
            Text('Click the buttons to hide it'),
          ]
        )
      )
    );
  }

  Widget buttonBar() {
    return Column(
      children: item.where((e) => e['hide'] != '1').map<Widget>((document) {
        return new FlatButton(
          child: new Text(document['title']),
          color: Color.fromARGB(document['color'], 0, 100, 0),
          onPressed: () {
            setState(() {
              print("click on ${document['title']} lets hide it");
              final tile = item.firstWhere((e) => e['title'] == document['title']);
              tile['hide'] = '1';
            });
          },
        );
      }
    ).toList());
  }
}

Maybe it helps someone. If it was is useful to you, let me know clicking in up arrow, please. Thanks.

https://dartpad.dev/b37b08cc25e0ccdba680090e9ef4b3c1

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.