【问题标题】:How to show button for each value in list?如何显示列表中每个值的按钮?
【发布时间】:2019-09-08 11:18:26
【问题描述】:

我有一个字符串列表“_choice”,我想为“_choice”列表中的每个值显示一个按钮。 我有这个错误:“元素类型Set MaterialButton不能分配给列表类型'Widget'”

    import 'package:flutter/material.dart';


class QuestionPage extends StatelessWidget {

  final String playerName;   final String question;   final List<String> _choice;   final String answer;


  QuestionPage(this.playerName, this.question, this._choice, this.answer);


  Widget build(BuildContext context) {
    return Scaffold(
        //AppBar
        appBar: AppBar(
        title: Text("$playerName to play", style: TextStyle(color: Colors.white)),
    centerTitle: true,
    backgroundColor: Colors.black,
    ),

      body: Stack(children: <Widget>[
      new Container(
      child: SingleChildScrollView(
          child: Container(
              padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
              child: Center(
                child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      Text('Question: \n $question'),

                      //------------PROBLEM HERE------------------
                      for (var i = 0; i < _choice.length; i++) {
                      new MaterialButton(
                        minWidth: 120.0,
                        color: Colors.blueGrey,
                        onPressed: null,
                        child: new Text(_choice[i],
                          style: new TextStyle(
                              fontSize: 20.0,
                              color: Colors.white
                          ),),
                      ),
                      }

                      ]   ))))]

    )} }

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    删除for循环花括号。

    你需要改变这个:

        for (var i = 0; i < _choice.length; i++) {
              new MaterialButton(
                minWidth: 120.0,
                color: Colors.blueGrey,
                onPressed: null,
                child: new Text(_choice[i],
                  style: new TextStyle(
                      fontSize: 20.0,
                      color: Colors.white
                  ),),
              ),
              }
    

    到这里:

    for (var i = 0; i < _choice.length; i++)
      new MaterialButton(
        minWidth: 120.0,
        color: Colors.blueGrey,
        onPressed: null,
        child: new Text(_choice[i],
          style: new TextStyle(
              fontSize: 20.0,
              color: Colors.white
          ),
          ),
      ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-26
      • 2012-08-21
      • 1970-01-01
      相关资源
      最近更新 更多