gpt4 book ai didi

flutter - 从数组中删除特定项目会删除UI中的错误项目

转载 作者:行者123 更新时间:2023-12-03 04:10:11 24 4
gpt4 key购买 nike

我为此苦苦挣扎了几天,但无法弄清楚。我有一个简单的小部件,我想在其中动态添加和删除输入(用于集/代表)。问题是,如果我要删除第一个项目,那么最后一个项目将从UI中删除。

我已经为该应用创建了一个dartpad:dartpad

我的main.dart:

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}

class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SetBuilderWidget();
}
}

class TempSet {
int weight;
int rep;
int order;

TempSet(this.order, this.weight, this.rep);
}

class SetBuilderWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => _SetBuilderWidgetState();
}

class _SetBuilderWidgetState extends State<SetBuilderWidget> {
final sets = [];
final List<TempSet> tempSet = [];

Widget _row(int index) {
return Row(
children: <Widget>[
Text("Set $index"),
Flexible(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40),
child: TextField(
decoration: InputDecoration(
labelText: "Rep count",
),
),
),
),
IconButton(
icon: Icon(Icons.delete),
onPressed: () => _removeSet(index),
),
],
);
}

List<Widget> _rows() {
return tempSet.map((s) => _row(s.order)).toList();
}

void _addSet() {
if (tempSet.length == 0) {
tempSet.add(TempSet(0, null, null));
} else {
final lastSet = tempSet.last;
tempSet.add(TempSet(tempSet.indexOf(lastSet) + 1, null, null));
}
}

void _removeSet(int index) {
setState(() {
tempSet.removeAt(index);
_updateIndexes();
});
}

void _updateIndexes() {
tempSet.asMap().forEach((index, s) {
s.order = index;
});
}

@override
void initState() {
_addSet();
super.initState();
}

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
..._rows(),
RaisedButton(
onPressed: () {
setState(() {
_addSet();
});
},
child: Text("Add set"),
)
],
);
}
}

问题:

enter image description here

最佳答案

您应该在 map 中保存这些TextField的状态。

import 'package:flutter/material.dart';

final Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}

class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SetBuilderWidget();
}
}

class TempSet {
int weight;
int rep;
int order;

TempSet(this.order, this.weight, this.rep);
}

class SetBuilderWidget extends StatefulWidget {
@override
State<StatefulWidget> createState() => _SetBuilderWidgetState();
}

class _SetBuilderWidgetState extends State<SetBuilderWidget> {
final sets = [];
final List<TempSet> tempSet = [];

Widget _row(int index, int rep) {
TextEditingController _controller =
TextEditingController(text: rep != null ? rep.toString() : "0");

return Row(
children: <Widget>[
Text("Set $index"),
Flexible(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40),
child: TextField(
onChanged: (text) {
tempSet[index].rep = num.parse(text).toInt();
print(tempSet);
},
controller: _controller,
decoration: InputDecoration(
labelText: "Rep count",
),
),
),
),
IconButton(
icon: Icon(Icons.delete),
onPressed: () => _removeSet(index),
),
],
);
}

List<Widget> _rows() {
return tempSet.map((s) => _row(s.order, s.rep)).toList();
}

void _addSet() {
if (tempSet.length == 0) {
tempSet.add(TempSet(0, null, null));
} else {
final lastSet = tempSet.last;
tempSet.add(TempSet(tempSet.indexOf(lastSet) + 1, null, null));
}
}

void _removeSet(int index) {
setState(() {
tempSet.removeAt(index);
_updateIndexes();
});
}

void _updateIndexes() {
tempSet.asMap().forEach((index, s) {
s.order = index;
});
}

@override
void initState() {
_addSet();
super.initState();
}

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
..._rows(),
RaisedButton(
onPressed: () {
setState(() {
_addSet();
});
},
child: Text("Add set"),
)
],
);
}
}
顺便说一句 you should avoid using widget functions。将它们声明为类。

关于flutter - 从数组中删除特定项目会删除UI中的错误项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60026207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com