Flutter 在listView中删除textView
当按下添加按钮时,它将添加一个文本字段。单击减号按钮时,我希望删除Flutter 在listView中删除textView,flutter,listview,textfield,Flutter,Listview,Textfield,当按下添加按钮时,它将添加一个文本字段。单击减号按钮时,我希望删除TextField。但它不断删除错误的文本字段 import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp(
TextField
。但它不断删除错误的文本字段
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var commentList = List();
TextEditingController _commentsController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sample"),
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Text("Comments", style: TextStyle(color: Colors.grey)),
SizedBox(width: 20),
Container(
height: 35,
padding: const EdgeInsets.all(10.0),
child: InkWell(
onTap: () {
setState(() {
commentList.insert(
0, _commentsController.text);
});
},
child: Row(children: [
Icon(
Icons.add,
color: Colors.white,
size: 15,
),
SizedBox(width: 5),
Text(
"Add",
style: TextStyle(color: Colors.white),
)
])),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: const Color(0xff0000ff),
)),
]),
SizedBox(height: 15),
Padding(
padding: EdgeInsets.all(10),
child: _showListViewComments()),
],
))));
}
Widget _showListViewComments() {
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: commentList.length + 1,
itemBuilder: (BuildContext ctxt, int index) {
if (index < commentList.length) {
return Padding(
padding: EdgeInsets.only(bottom: 10),
child: Row(children: [
Expanded(
child: TextField(
maxLines: 3,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0))),
contentPadding: EdgeInsets.all(10),
))),
SizedBox(width: 10),
Container(
width: 30,
height: 30,
child: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () {
setState(() {
print(index);
commentList.removeAt(index);
});
},
child: Icon(
Icons.remove,
color: Colors.white,
),
))
]));
}
});
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
var commentList=List();
TextEditingController_commentsController=TextEditingController();
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“样本”),
),
正文:SingleChildScrollView(
孩子:填充(
填充:边缘设置。全部(10),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
世界其他地区(儿童:[
文本(“注释”,样式:TextStyle(颜色:Colors.grey)),
尺寸箱(宽度:20),
容器(
身高:35,
填充:常数边集全部(10.0),
孩子:InkWell(
onTap:(){
设置状态(){
commentList.insert(
0,_commentsController.text);
});
},
子对象:行(子对象:[
图标(
Icons.add,
颜色:颜色,白色,
尺码:15,
),
尺寸箱(宽度:5),
正文(
“添加”,
样式:TextStyle(颜色:Colors.white),
)
])),
装饰:盒子装饰(
边界半径:边界半径。圆形(10.0),
颜色:常量颜色(0xff0000ff),
)),
]),
尺寸箱(高度:15),
填充物(
填充:边缘设置。全部(10),
子项:_showListViewComments()),
],
))));
}
小部件_showListViewComments(){
返回ListView.builder(
收缩膜:对,
物理学:NeverscrollableScroll物理学(),
itemCount:commentList.length+1,
itemBuilder:(BuildContext ctxt,int index){
如果(索引
我有课文1和课文2。我想删除文本1而不是文本2
每个文本字段都应与定义的TextEditingController配对。我将您的代码编辑为
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<TextEditingController> commentList = List();
//TextEditingController _commentsController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Sample"),
),
body: SingleChildScrollView(
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(children: [
Text("Comments", style: TextStyle(color: Colors.grey)),
SizedBox(width: 20),
Container(
height: 35,
padding: const EdgeInsets.all(10.0),
child: InkWell(
onTap: () {
setState(() {
commentList.add(TextEditingController());
// commentList.insert(
// 0, _commentsController.text);
});
},
child: Row(children: [
Icon(
Icons.add,
color: Colors.white,
size: 15,
),
SizedBox(width: 5),
Text(
"Add",
style: TextStyle(color: Colors.white),
)
])),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
color: const Color(0xff0000ff),
)),
]),
SizedBox(height: 15),
Padding(
padding: EdgeInsets.all(10),
child: _showListViewComments()),
],
))));
}
Widget _showListViewComments() {
return ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: commentList.length + 1,
itemBuilder: (BuildContext ctxt, int index) {
if (index < commentList.length) {
return Padding(
padding: EdgeInsets.only(bottom: 10),
child: Row(children: [
Expanded(
child: TextField(
maxLines: 3,
controller: commentList[index],
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: const BorderRadius.all(
const Radius.circular(5.0))),
contentPadding: EdgeInsets.all(10),
))),
SizedBox(width: 10),
Container(
width: 30,
height: 30,
child: FloatingActionButton(
backgroundColor: Colors.red,
onPressed: () {
setState(() {
print(index);
commentList.removeAt(index);
});
},
child: Icon(
Icons.remove,
color: Colors.white,
),
))
]));
}
});
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
List commentList=List();
//TextEditingController_commentsController=TextEditingController();
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“样本”),
),
正文:SingleChildScrollView(
孩子:填充(
填充:边缘设置。全部(10),
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
世界其他地区(儿童:[
文本(“注释”,样式:TextStyle(颜色:Colors.grey)),
尺寸箱(宽度:20),