簡體   English   中英

在 flutter 中動態地將小部件添加到列的子項

[英]Dynamically add widgets to a column's children in flutter

我正在創建一個測驗應用程序,需要根據特定問題的選項數量動態顯示 mcq 選項。

例如:

在此處輸入圖片說明

現在按鈕的代碼在這里:

    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: (){},
            ),
          ],
        ),
      ),
    );

如您所見,我能做的是“修復”2 個按鈕。 有沒有辦法根據該特定問題的選項數量動態添加按鈕?

我有一個名為questions的列表,它是一個問題列表(這是一個類):

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",
)

您應該遍歷您的選項以創建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(),
.........................

我做了一個完整的例子,我使用動態小部件在屏幕上顯示和隱藏小部件,你也可以在dart fiddle上看到它在線運行。

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());
  }
}

也許它可以幫助某人。 如果它對您有用,請點擊向上箭頭讓我知道。 謝謝。

https://dartpad.dev/b37b08cc25e0ccdba680090e9ef4b3c1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM