Dart 颤振设置ListView生成器上的默认ItemCount
我正在尝试使用ListView Builder构建一些输入字段,并将默认构建字段设置为3,然后使用floatActionButton将其添加到数字中以生成更多字段 但是我设置的3个默认字段没有显示,我无法理解发生了什么 这是我的密码Dart 颤振设置ListView生成器上的默认ItemCount,dart,flutter,Dart,Flutter,我正在尝试使用ListView Builder构建一些输入字段,并将默认构建字段设置为3,然后使用floatActionButton将其添加到数字中以生成更多字段 但是我设置的3个默认字段没有显示,我无法理解发生了什么 这是我的密码 int newSULength = 3; new Expanded( child: new Form( key: formKey, child: new ListView.
int newSULength = 3;
new Expanded(
child: new Form(
key: formKey,
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return buildfields(index);
},
itemCount: newSULength,
scrollDirection: Axis.vertical,
),
),
)
floatingActionButton: new FloatingActionButton(
onPressed: () {
setState(() {
newSULength++;
});
},
child: new Icon(Icons.add),
)
下面是我试图建立的领域
Widget buildfields(int index) {
return new Container(
margin: EdgeInsets.only(bottom: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
_deleteMode ? deleteButton(index) : null,
Container(
width: 110.0,
child: TextFormField(
onSaved: (String value) {
setState(() {
_course = value;
});
},
initialValue: 'GNS101',
inputFormatters: [
new LengthLimitingTextInputFormatter(6),
],
validator: (val) {
return val.isEmpty ? "Enter Course Code" : null;
},
textCapitalization: TextCapitalization.characters,
decoration: new InputDecoration(
border: UnderlineInputBorder(),
contentPadding: EdgeInsets.fromLTRB(20.0, 5.0, 20.0, 8.0),
),
),
),
new Container(
child: new DropdownButton<num>(
onChanged: (num value) {
setState(() {
selectedGrade[index] = value;
});
},
hint: new Text('Grade Point'),
value: selectedGrade[index],
items: <num>[0, 1, 2, 3, 4, 5].map((num value) {
return new DropdownMenuItem<num>(
value: value,
child: new Text(value.toString()),
);
}).toList(),
),
),
new Container(
child: new DropdownButton<num>(
onChanged: (num value) {
setState(() {
selectedUnit[index] = value;
});
},
hint: new Text('Course Unit'),
value: selectedUnit[index],
items: <num>[1, 2, 3, 4, 5].map((num value) {
return new DropdownMenuItem<num>(
value: value,
child: new Text(value.toString()),
);
}).toList(),
),
),
].where(notNull).toList(),
),
);
}
我试着使用selectedUnit
和selectedGrade
作为映射,它可以工作
var selectedUnit = {};
var selectedGrade = {};
但是我需要使用selectedUnit.contain()
和selectedUnit.add()
检查列表是否包含某个值,这些值在Map中不可用
请帮助。以下内容应该适合您。我不确定确切的问题。我必须用你的代码填充一些东西才能构建它。检查您的
notNull
逻辑,因为它可能返回一个空的列表
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formKey = GlobalKey<FormState>();
int newSULength = 3;
var selectedUnit = [0, 0, 0];
var selectedGrade = [0, 0, 0];
var _course = '';
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: Column(
children: <Widget>[
Expanded(
child: Form(
key: _formKey,
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return buildfields(index);
},
itemCount: newSULength,
scrollDirection: Axis.vertical,
),
),
)
],
),
floatingActionButton: new FloatingActionButton(
onPressed: () {
setState(() {
newSULength++;
selectedUnit.add(0);
selectedGrade.add(0);
});
},
child: new Icon(Icons.add),
),
);
}
Widget buildfields(int index) {
return new Container(
margin: EdgeInsets.only(bottom: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 110.0,
child: TextFormField(
onSaved: (String value) {
setState(() {
_course = value;
});
},
initialValue: 'GNS101',
inputFormatters: [
// new LengthLimitingTextInputFormatter(6),
],
validator: (val) {
return val.isEmpty ? "Enter Course Code" : null;
},
textCapitalization: TextCapitalization.characters,
decoration: new InputDecoration(
border: UnderlineInputBorder(),
contentPadding: EdgeInsets.fromLTRB(20.0, 5.0, 20.0, 8.0),
),
),
),
new Container(
child: new DropdownButton<num>(
onChanged: (num value) {
setState(() {
selectedGrade[index] = value;
});
},
hint: new Text('Grade Point'),
value: selectedGrade[index] != 0 ? selectedGrade[index] : null,
items: <num>[0, 1, 2, 3, 4, 5].map((num value) {
return new DropdownMenuItem<num>(
value: value,
child: new Text(value.toString()),
);
}).toList(),
),
),
new Container(
child: new DropdownButton<num>(
onChanged: (num value) {
setState(() {
selectedUnit[index] = value;
});
},
hint: new Text('Course Unit'),
value: selectedUnit[index] != 0 ? selectedUnit[index] : null,
items: <num>[1, 2, 3, 4, 5].map((num value) {
return new DropdownMenuItem<num>(
value: value,
child: new Text(value.toString()),
);
}).toList(),
),
),
].where((value) => value != null).toList(),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(新的MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“颤振演示”,
主题:新主题数据(
主样本:颜色。蓝色,
),
主页:新MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
final _formKey=GlobalKey();
int newSULength=3;
var selectedUnit=[0,0,0];
var selectedGrade=[0,0,0];
var_课程=“”;
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(widget.title),
),
正文:专栏(
儿童:[
扩大(
孩子:表格(
键:_formKey,
子项:新建ListView.builder(
itemBuilder:(构建上下文,int索引){
返回构建字段(索引);
},
itemCount:newSULength,
滚动方向:轴垂直,
),
),
)
],
),
floatingActionButton:新的floatingActionButton(
已按下:(){
设置状态(){
newSULength++;
选择单位添加(0);
选择升级。添加(0);
});
},
子:新图标(Icons.add),
),
);
}
小部件构建字段(int索引){
退回新货柜(
边距:仅限边缘组(底部:10.0),
孩子:排(
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
容器(
宽度:110.0,
子项:TextFormField(
onSaved:(字符串值){
设置状态(){
_课程=价值;
});
},
初始值:“GNS101”,
输入格式化程序:[
//新的长度限制文本输入格式化程序(6),
],
验证器:(val){
return val.isEmpty?“输入课程代码”:空;
},
textcapitalize:textcapitalize.characters,
装饰:新的输入装饰(
边框:下划线InputBorder(),
内容填充:来自LTRB(20.0,5.0,20.0,8.0)的EdgeInsets,
),
),
),
新容器(
孩子:新的下拉按钮(
onChanged:(num值){
设置状态(){
selectedGrade[索引]=值;
});
},
提示:新文本(“坡度点”),
值:selectedGrade[index]!=0?selectedGrade[index]:空,
项:[0,1,2,3,4,5]。映射((数值){
返回新的DropdownMenuItem(
价值:价值,
子项:新文本(value.toString()),
);
}).toList(),
),
),
新容器(
孩子:新的下拉按钮(
onChanged:(num值){
设置状态(){
selectedUnit[索引]=值;
});
},
提示:新文本(“课程单元”),
值:selectedUnit[index]!=0?selectedUnit[index]:空,
项:[1,2,3,4,5]。映射((数值){
返回新的DropdownMenuItem(
价值:价值,
子项:新文本(value.toString()),
);
}).toList(),
),
),
].where((value)=>value!=null).toList(),
),
);
}
}
更新:如果要继续使用
List
s存储值,请使用占位符值初始化List
成员变量。存储在列表
成员变量中的值数量必须等于列表视图
中的元素数量。更新下拉按钮的值
参数以检查占位符,如果找到则返回null
。在晶圆厂的onPressed
中,确保为新元素添加新的占位符值<代码>地图
s或合适的型号都是更好的/未来的选择。谢谢,我已经发现了问题所在。请检查,因为我已经更新了我的帖子。请提供帮助。如果这是一个简单的bool
检查,那么您可以使用selectedUnit.values.contains(someValue)
访问Map
的值。您使用的是构建器,因此返回的索引与映射
配合良好。如果希望使用selectedUnit['10']=1
,您可以手动创建新的键/值对——如果您希望这样做,只需将其替换为下一个有效键即可。看起来每个元素都与一个课程相关,因此唯一的键可以看到
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formKey = GlobalKey<FormState>();
int newSULength = 3;
var selectedUnit = [0, 0, 0];
var selectedGrade = [0, 0, 0];
var _course = '';
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text(widget.title),
),
body: Column(
children: <Widget>[
Expanded(
child: Form(
key: _formKey,
child: new ListView.builder(
itemBuilder: (BuildContext context, int index) {
return buildfields(index);
},
itemCount: newSULength,
scrollDirection: Axis.vertical,
),
),
)
],
),
floatingActionButton: new FloatingActionButton(
onPressed: () {
setState(() {
newSULength++;
selectedUnit.add(0);
selectedGrade.add(0);
});
},
child: new Icon(Icons.add),
),
);
}
Widget buildfields(int index) {
return new Container(
margin: EdgeInsets.only(bottom: 10.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 110.0,
child: TextFormField(
onSaved: (String value) {
setState(() {
_course = value;
});
},
initialValue: 'GNS101',
inputFormatters: [
// new LengthLimitingTextInputFormatter(6),
],
validator: (val) {
return val.isEmpty ? "Enter Course Code" : null;
},
textCapitalization: TextCapitalization.characters,
decoration: new InputDecoration(
border: UnderlineInputBorder(),
contentPadding: EdgeInsets.fromLTRB(20.0, 5.0, 20.0, 8.0),
),
),
),
new Container(
child: new DropdownButton<num>(
onChanged: (num value) {
setState(() {
selectedGrade[index] = value;
});
},
hint: new Text('Grade Point'),
value: selectedGrade[index] != 0 ? selectedGrade[index] : null,
items: <num>[0, 1, 2, 3, 4, 5].map((num value) {
return new DropdownMenuItem<num>(
value: value,
child: new Text(value.toString()),
);
}).toList(),
),
),
new Container(
child: new DropdownButton<num>(
onChanged: (num value) {
setState(() {
selectedUnit[index] = value;
});
},
hint: new Text('Course Unit'),
value: selectedUnit[index] != 0 ? selectedUnit[index] : null,
items: <num>[1, 2, 3, 4, 5].map((num value) {
return new DropdownMenuItem<num>(
value: value,
child: new Text(value.toString()),
);
}).toList(),
),
),
].where((value) => value != null).toList(),
),
);
}
}