您好,我是 Flutter 的新手,我在一个名为 split row 的小部件中工作,我使用一个带有三个子项的 ListView:Text Widget、ListView Builder 小部件和一个 Container Widget。
我使用文本作为标题,使用地图填充 ListView Builder,使用容器将小部件添加到 ListView Builder,一切正常,但是当我删除一个项目时,这个项目也会从地图中删除,从 ListView Builder 中删除但是不更新值。
为了更好地解释我自己,我用一个 textField 和一个 Text 做了一个例子,第一次我输入了 map 键的值三次,第二次用 word data 和 map 键形成了文本。这就是发生的事情
正如您所看到的,当我删除 data1 时,它消失的项目只留下数据 0、数据 2 和数据 3,但值 111 仍然存在,值 333 消失。显然values不对应map元素除了000 => data 0
我已经在论坛上进行了研究,我已经在网站上搜索了 stackoverflow,但我没有找到任何对我有帮助的东西
这是代码:
import 'package:flutter/material.dart';
class SplitRow extends StatefulWidget {
@override
_SplitRowState createState() => _SplitRowState();
}
class _SplitRowState extends State<SplitRow> {
int _index = 0;
Map<String, Widget> splitRows = {};
Map records = {};
Map col1 = {};
// Crete Widget Split Row
Widget _splitRow(index) {
return Column(
children: <Widget>[
Container(
color: Colors.white,
height: 40.0,
child: Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.remove_circle),
iconSize: 24,
color: Color(0xFFFF3B30),
onPressed: () {
removeItem(index);
},
),
Container(
width: 100,
child: TextField(
keyboardType: TextInputType.numberWithOptions(
signed: false,
decimal: true,
),
decoration: InputDecoration(
hintText: 'Put direction here...',
border: InputBorder.none,
),
style: TextStyle(fontFamily: 'Lekton'),
onChanged: (value) {
col1["$index"] = value;
},
),
),
_VerticalDivider(),
Text('data $index'),
],
),
),
_HorizontalDivider(),
],
);
}
// Create Widget Add Row
Widget _addRow() {
return Container(
height: 40,
color: Colors.white,
child: Row(
children: <Widget>[
Container(
height: 40,
color: Colors.white,
child: IconButton(
icon: Icon(Icons.add_circle),
iconSize: 24,
color: Color(0xFF34C759),
onPressed: () {
addItem();
},
),
),
Text('New Item')
],
),
);
}
// Add splitRow
void addItem() {
int key = _index;
print('Index antes $_index');
setState(() {
splitRows['$key'] = _splitRow(key);
++_index;
});
print(splitRows);
}
// Remove splitRow
void removeItem(key) {
setState(() {
splitRows.remove('$key');
col1.remove('$key');
});
}
// Save Values
void saveItems() {
records = {'qty': col1};
print(records);
}
@override
void initState() {
super.initState();
splitRows['$_index'] = _splitRow(_index);
++_index;
// splitRows['$_index'] = _addRow(_index);
// ++_index;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Directions'),
actions: <Widget>[
FlatButton(
child: Text(
'Save',
style: TextStyle(color: Colors.white),
),
onPressed: () {
saveItems();
},
)
],
),
backgroundColor: Color(0xFFE5E5EA),
body: Column(
children: <Widget>[
Container(
constraints: BoxConstraints(
minHeight: 40.0,
maxHeight: 500.0,
minWidth: double.infinity,
maxWidth: double.infinity,
),
child: ListView(
children: <Widget>[
Padding(padding: EdgeInsets.only(top: 10.0)),
Text('DIRECTIONS'),
ListView.builder(
shrinkWrap: true,
itemCount: splitRows.length,
itemBuilder: (BuildContext context, int index) {
String key = splitRows.keys.elementAt(index);
return splitRows[key];
},
physics: NeverScrollableScrollPhysics(),
),
_addRow()
],
))
],
));
}
}
class _HorizontalDivider extends StatelessWidget {
const _HorizontalDivider({
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: Colors.white,
child: Divider(
height: 2.0,
),
);
}
}
class _VerticalDivider extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
height: 20,
width: 2,
margin: EdgeInsets.symmetric(horizontal: 5.0, vertical: 5.0),
child: VerticalDivider(
width: 2.0,
),
);
}
}
我将永远感激你能给我的任何帮助或建议,我已经很长时间没有解决方案了