我是一个初学者,我不能得到我想要的结果,因为我有一个“布局问题”。我试了几个小时……
技术问题:
当我点击“添加播放器按钮”来添加一个新的TextField来设置更多的播放名称时,我得到了一个错误:"BoxConstraints forces a infinite“。因此,不会显示TextField。
其他我不知道如何实现的特性:
import 'package:flutter/material.dart';
/// Styles
///
class Homepage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyHomePageState();
}
}
class _MyHomePageState extends State<Homepage> {
String dropdownValue = 'Normal';
List<TextField> _playerList = new List(); //_playerList is my List name
int playerListIndex = 0; //Count the number of times "Add" button clicked
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(children: <Widget>[
// To have a gradient background
new Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
decoration: new BoxDecoration(
gradient: new LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
stops: [0.1, 1],
colors: [
Colors.redAccent[200],
Colors.red[300],
],
tileMode:
TileMode.repeated,
),
),
//I need SingleChildScrollView to haven't "Overflow yellow bar on bottom screen"
child: SingleChildScrollView(
child: Container(
padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
// Box to add big image
SizedBox(
height: 450,
),
// DROP DOWN MENU to choose difficulty modes
InputDecorator(
decoration: InputDecoration(
prefixIcon: const Icon(Icons.games),
filled: true,
),
child: DropdownButtonHideUnderline(
child: new DropdownButton<String>(
value: dropdownValue,
hint: Text('Difficulty normal'),
isDense: true,
items: <String>['Normal', 'Easy', 'Hard']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
),
),
),
//Field player name 1. At least I need 1 player
new TextField(
maxLength: 20,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.person),
filled: true,
hintText:"Player 1",
counterText: "",
),
),
//Field for other player name 2, 3, 4, ...
new ListView.builder(
padding: EdgeInsets.all(0),
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: _playerList.length,
itemBuilder: (context, index) {
TextField widget = _playerList.elementAt(index);
return widget;
}),
//Button to add other field text to set player name. The user can delete a player on click on "clear icon button"
new Align(
alignment: Alignment.centerRight,
child: FlatButton.icon(
color: Colors.black,
icon: Icon(
Icons.add,
color: Colors.white,
size: 18,
),
label: Text('Add player',
style:
TextStyle(fontSize: 12, color: Colors.white)),
onPressed: () {
setState(() {
playerListIndex++;
_playerList.add(
new TextField(
maxLength: 20,
decoration: InputDecoration(
prefixIcon: const Icon(Icons.person),
suffixIcon: new IconButton(
icon: Icon(Icons.clear),
onPressed: () {
_playerList.removeAt(playerListIndex); //Doesn't work
}),
filled: true,
hintText: "Player $playerListIndex", //Doesn't work i would like Player 2, Player 3, ...
counterText: "",
),
),
);
print(playerListIndex);
print(_playerList);
print(_playerList[0]);
});
}),
),
new ButtonTheme(
minWidth: 350,
height: 45,
child: FlatButton(
color: Colors.black,
child: Text('Play',
style:
TextStyle(fontSize: 18, color: Colors.white)),
onPressed: () {
// Perform some action
},
),
),
],
),
),
),
),
),
]),
);
}
}
发布于 2019-05-26 19:04:16
我想我明白是怎么回事了。让我们开始吧:
第一。你的TextField
没有显示,你得到一个错误,因为你没有为它设置宽度和高度,所以,它试图获得它能得到的最大尺寸。这个问题的解决方案?将其包装在Container
小部件(或SizedBox
)中,并为其指定宽度和高度。这应该可以解决第一个问题。
第二。您的DropDownMenu的提示文本(难度正常)没有显示,因为您没有在DropDown小部件See the hint propierty below中传递参数“提示”。
第三。您希望在单击后缀时清除TextField
字符串,对吗?这应该可以解决这个问题。
第四。你希望随着你添加更多的玩家,玩家字符串的列表也会增加,对吧?你有很多方法可以做到这一点,其中之一是创建一个空的列表,你可以模拟来测试它,然后,每次你点击"Add player“按钮,你就会在一个for循环中添加一个与for循环中的值相对应的字符串,在"Player $index”中传递for的索引作为int值。同样,for方法将接收一个setState方法,并在setState中接收添加一个玩家的逻辑。
最后,你的梯度不是固定的?嗯,我认为您可以在Scaffold小部件中传递一个名为resizeToAvoidBottomInset
的专有属性,并将其设置为false。
如果有些东西没有像你预期的那样工作,请回复它,我会帮你解决的。
https://stackoverflow.com/questions/56312567
复制相似问题