Flutter 如何为列表中的每个值显示按钮?
我有一个字符串列表_选项,我想为_选项列表中的每个值显示一个按钮。 我有一个错误:元素类型Set MaterialButton无法分配给列表类型“Widget”Flutter 如何为列表中的每个值显示按钮?,flutter,dart,Flutter,Dart,我有一个字符串列表_选项,我想为_选项列表中的每个值显示一个按钮。 我有一个错误:元素类型Set MaterialButton无法分配给列表类型“Widget” import 'package:flutter/material.dart'; class QuestionPage extends StatelessWidget { final String playerName; final String question; final List<String>
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
),),
),
}
] ))))]
)} }
拆下用于卷曲支架的环 您需要更改以下内容:
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
),
),
),