如何解决在listView中删除textView
当按下添加按钮时,它将添加一个TextField。当我单击减号按钮时,我希望删除TextField
。但它会不断删除错误的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,))
]));
}
});
}
}
例如:我有文本1和文本2。我要删除文本1而不是文本2。
解决方法
每个TextField应该与定义的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,))
]));
}
});
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。