ошибка в том, что когда начинаю удалять форму с середины или сначала, как я понял, не удаляется именно та форма которая была нажата первой, что мне надо написать правильно?
вот мой код:
import 'package:flutter/material.dart';
import 'data.dart';
class FormData extends StatefulWidget {
@override
_FormDataState createState() => _FormDataState();
}
class _FormDataState extends State<FormData> {
List<Data> _data = [];
List<Widget> _children = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AddPage'),
actions: <Widget>[
IconButton(
icon: Icon(Icons.save),
onPressed: () {},
)
],
),
body: SingleChildScrollView(
child: Center(
child: Column(
children: <Widget>[
Container(
child: _children.length <= 0
? Text(
'tap [+] button for add',
style: TextStyle(color: Colors.yellow, fontSize: 18.0),
)
: Column(
children: _children,
),
),
Container(
child: RaisedButton(
child: Icon(Icons.add),
onPressed: () => onAddForm(_data.length),
),
)
],
),
),
),
);
}
void onAddForm(int keyValue) {
setState(() {
_data.add(Data());
});
_children = List.from(_children)
..add(Card(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
flex: 85,
child: Column(
children: <Widget>[
TextField(
style: TextStyle(color: Colors.lime[600]),
onChanged: (val) {
onChangedName(keyValue, val);
},
decoration: InputDecoration(
hintText: 'name',
hintStyle: TextStyle(color: Colors.lime[900])),
),
TextField(
keyboardType: TextInputType.number,
onChanged: (val) {
onChangedNumber(keyValue, val);
},
decoration: InputDecoration(
hintText: 'phoneNumber',
hintStyle: TextStyle(color: Colors.grey)
),
)
],
),
),
Expanded(
flex: 15,
child: FlatButton(
child: Stack(
children: <Widget>[
Icon(Icons.delete, size: 28,),
Positioned(
top: -20,
right: -15,
child: CircleAvatar(
radius: 12,
backgroundColor: Colors.red,
child: Text(
'$keyValue',
style: TextStyle(color: Colors.white),
),
),
)
],
overflow: Overflow.visible,
),
onPressed: ()=>onDelete(keyValue),
),
)
],
),
));
}
void onDelete(int index) {
setState(() {
_data.removeAt(index);
_children.removeAt(index);
});
print(_data);
}
onChangedName(int index, String record) {
setState(() {
_data[index].phoneNumber = record;
});
}
onChangedNumber(int index, String record) {
setState(() {
_data[index].phoneNumber = record;
});
}
}
RangeError (index): Invalid value: Only valid value is 0: 2
тоесть цифра 2 в ошибке – индекс которая была нажата первой