我有一个字符串列表“ _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
),),
),
}
] ))))]
)} }
答案 0 :(得分: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
),
),
),