请帮我解决问题。提前谢谢你。
当我试图编辑已经包含在 StatefulBuilder 中的对话框小部件内的内容并且在编辑完成时和调用另一个方法之前使用 setState 更改值但似乎值仍然与初始值相同时,就会出现问题。我该怎么办?
问题图片
对话框小部件
createDialogSpecialCase(
EditingDialogType enums, String masterKey, List<dynamic> data) {
String currentEditingDialog = '';
List listData = [];
ScrollController scrllController = ScrollController();
return showDialog(
context: context,
barrierDismissible: false,
builder: (context) {
return StatefulBuilder(builder: (context, setState) {
return LayoutBuilder(builder: (context, constraints) {
return AlertDialog(
titlePadding:
const EdgeInsets.only(top: 5, right: 5, left: 5),
title: SizedBox(
height: 50,
child: Stack(
children: [
Align(
alignment: Alignment.bottomCenter,
child: Text(
enums.name,
style: TextStyle(
color: Colors.blueAccent,
fontSize: ResponsiveStyle().bodyFontSize()),
),
),
Align(
alignment: Alignment.topRight,
child: GestureDetector(
onTap: () => Navigator.pop(context),
child: const Icon(
Icons.close,
size: 45,
),
),
),
],
),
),
contentPadding: const EdgeInsets.all(30),
content: SingleChildScrollView(
child: Column(
children: List.generate(listData.length, (index) {
var objval = listData[index];
Map objvalJson = {};
objvalJson = objval.toJson();
return Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
"Item: ${(index + 1)}",
style: TextStyle(
fontSize:
ResponsiveStyle().bodyFontSize()),
),
),
const SizedBox(
height: 15,
),
Container(
height: constraints.maxHeight / 1.45,
width: constraints.minWidth / 1.25,
padding:
const EdgeInsets.symmetric(horizontal: 24),
child: Scrollbar(
controller: scrllController,
thumbVisibility: true,
thickness: 10,
radius: const Radius.circular(25),
interactive: true,
child: DataTable2(
scrollController: scrllController,
dataRowHeight: 100,
headingRowColor:
MaterialStateColor.resolveWith(
(states) =>
Utils.convertHexToColor(
'cfd8dc')),
border:
TableBorder.all(color: Colors.black),
columns: createColumnTable,
rows: objvalJson.entries.map((data) {
String dataKey = data.key.toString();
var dataValue = data.value;
TextEditingController valueController =
TextEditingController();
valueController.text =
dataValue.toString();
return DataRow(cells: [
DataCell(Align(
alignment: Alignment.center,
child: Text(data.key.toString(),
style: TextStyle(
fontSize: ResponsiveStyle()
.bodyFontSize())),
)),
DataCell(Align(
alignment: Alignment.centerLeft,
child: Row(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: [
currentEditingDialog ==
data.key.toString()
? data.value.runtimeType ==
String ||
data.value
.runtimeType ==
double ||
data.value
.runtimeType ==
int
? Expanded(
child:
ConstrainedBox(
constraints:
const BoxConstraints(
maxWidth:
500),
child: TextField(
controller:
valueController,
style: TextStyle(
fontSize:
ResponsiveStyle()
.bodyFontSize()),
decoration:
const InputDecoration(
isDense: true,
),
onEditingComplete:
() async {
setState(() {
dataValue =
valueController
.text;
objvalJson.update(
dataKey,
(value) =>
dataValue);
});
await updateValuelv2(
masterKey,
dataKey,
dataValue,
objvalJson,
enums);
FocusScope.of(
context)
.unfocus();
},
),
),
)
: data.value.runtimeType ==
bool
? Row(
children: [
CupertinoSwitch(
value:
dataValue,
onChanged:
(bool
flag) async {
setState(
() {
dataValue =
flag;
objvalJson.update(
dataKey,
(value) => dataValue);
});
await updateValuelv2(
masterKey,
dataKey,
dataValue,
objvalJson,
enums);
}),
const SizedBox(
width: 7,
),
Text(
dataValue
.toString(),
style: TextStyle(fontSize:ResponsiveStyle().bodyFontSize()),
)
],
)
: Expanded(
child:
ConstrainedBox(
constraints:
const BoxConstraints(
maxWidth:
500),
child: Text(
dataValue
.toString(),
style: TextStyle(
fontSize:
ResponsiveStyle().bodyFontSize())),
),
)
: data.value.runtimeType
.toString()
.contains("List")
? GestureDetector(
onTap: () {
if (data.value
.runtimeType
.toString()
.contains(
"ApproverModel")) {
createDialogSpecialCase(
EditingDialogType
.ApproverModel,
data.key
.toString(),
data.value);
} else if (data
.value
.runtimeType
.toString()
.contains(
"CartItems")) {
createDialogSpecialCase(
EditingDialogType
.CartItems,
data.key
.toString(),
data.value);
} else if (data
.value
.runtimeType
.toString()
.contains(
"Payment")) {
createDialogSpecialCase(
EditingDialogType
.Payment,
data.key
.toString(),
data.value);
} else if (data
.value
.runtimeType
.toString()
.contains(
"WarningCheckCartPromotionModel")) {
createDialogSpecialCase(
EditingDialogType
.WarningCheckCartPromotionModel,
data.key
.toString(),
data.value);
} else if (data
.value
.runtimeType
.toString()
.contains(
"PromotionDetail")) {
createDialogSpecialCase(
EditingDialogType
.PromotionDetail,
data.key
.toString(),
data.value);
}
},
child: Container(
decoration:
BoxDecoration(
borderRadius:
BorderRadius
.circular(
40),
color: Colors
.white,
border:
Border.all(
color: Colors
.blueAccent,
width: 2,
),
),
padding: const EdgeInsets
.symmetric(
vertical: 7,
horizontal:
20),
child: Text(
"Click for more detail.",
style: TextStyle(
color: Colors
.blueAccent,
fontSize:
ResponsiveStyle()
.bodyFontSize()),
),
),
)
: Expanded(
child:
ConstrainedBox(
constraints:
const BoxConstraints(
maxWidth:
500),
child: Text(
data.value
.toString(),
style: TextStyle(
fontSize:
ResponsiveStyle()
.bodyFontSize())),
),
),
data.value.runtimeType ==
String ||
data.value
.runtimeType ==
double ||
data.value
.runtimeType ==
int ||
data.value
.runtimeType ==
bool
? currentEditingDialog !=
dataKey
? GestureDetector(
onTap: () {
setState(() {
currentEditingDialog =
dataKey;
});
},
child: Icon(
Icons.edit,
size: ResponsiveStyle()
.bodyFontSize(),
color: Colors.blue
.shade700,
),
)
: Row(
children: [
GestureDetector(
onTap: () {
FocusScope.of(
context)
.unfocus();
setState(() {
currentEditingDialog =
'';
});
},
child: Icon(
Icons.check,
size: ResponsiveStyle()
.headerFontSize(),
color: Colors
.green,
),
),
const SizedBox(
width: 10,
),
GestureDetector(
onTap: () {
setState(() {
currentEditingDialog =
'';
});
},
child: Icon(
Icons.close,
size: ResponsiveStyle()
.headerFontSize(),
color: Colors
.red,
),
)
],
)
: const SizedBox(),
],
),
)),
]);
}).toList()),
),
),
],
);
}),
),
));
});
});
});
}
请帮我解决这个问题。我已经尝试修复这 3 天了。