当我从下拉列表中选择值时,我从输入字段中的API获取输入字段数据,当我尝试在文本字段中输入数据时,当键盘打开时,它会重置下拉方法中的数据我已经尝试过: 1- 将下拉菜单包装在构建器中并给它一个全局键 ----// 不起作用 2-为listviewbuilder制作一个单独的小部件但不起作用 3-默认创建一个bool变量,当屏幕在didchangedependency函数中构建时,它是true,当该值为true时,我给它一个假值,那么它应该构建listview构建器,否则它将构建一个sizedbox--- // did不起作用 在此处输入图像描述 打开键盘时,CarAdOne 小部件内的 ListView.builder 会重建,导致下拉值重置。出现此问题的原因是 ListView.builder 正在重建其项目,并且每次都会创建小部件的新实例,包括下拉列表。但是当调用构建时,文本字段中的值不会重置,仅会重置下拉列表值当我尝试为单选按钮选择值时,也会发生同样的情况,它会保留其他值
class CarAdOne extends StatefulWidget {
const CarAdOne({super.key});
@override
State<CarAdOne> createState() => _CarAdOneState();
}
class _CarAdOneState extends State<CarAdOne> {
@override
void didChangeDependencies() {
// TODO: implement didChangeDependencies
super.didChangeDependencies();
// context.read<AdsInputField>().makeSelectedList();
}
@override
void initState() {
Provider.of<AdsInputField>(context, listen: false)
.getInputFields(categoryId: '135', pageId: '1');
// TODO: implement initState
super.initState();
}
final ImagePicker picker = ImagePicker();
getImages({required AdsInputFieldsModel data}) async {
// Pick multiple images.
final List<XFile> images = await picker.pickMultiImage();
if (images.isNotEmpty) {
data.attributes =
images.map((e) => File(e.path)).toList() as List<Attribute>;
setState(() {});
}
print(images);
print(data.attributes);
}
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
body: Consumer<AdsInputField>(builder: (context, value, child) {
return value.isLoading
? const Center(
child: CircularProgressIndicator(),
)
: value.adsInputFieldList.isEmpty
? Center(child: H2Bold(text: 'No Data'))
Padding(
padding: EdgeInsets.symmetric(horizontal: 30.w),
child: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: value.adsInputFieldList.length,
itemBuilder: (context, index) {
value.selectedValueList =
value.adsInputFieldList.map((element) {
if (element.categoryTypeId == '4') {
// If the category id is '4', check if the value length should be attributes.length
if (element.attributes.isEmpty) {
// Handle the case where attributes is an empty list
return AddSummaryModel(
name: element.categoryFieldName,
nameAr: element.categoryFieldNameArabic,
value: [''],
field: element.categoryTypeName,
);
} else {
// Add values to selectedValueList with length equal to attributes.length
return AddSummaryModel(
nameAr: element.categoryFieldNameArabic,
value: List.generate(
element.attributes.length,
(index) => 'false'),
field: element.categoryTypeName,
);
}
} else {
// For categories other than '4', add values with an empty list
return AddSummaryModel(
name: element.categoryFieldName,
nameAr: element.categoryFieldNameArabic,
value: [''],
field: element.categoryTypeName,
);
}
}).toList();
print('listviewbuilder called here');
final data = value.adsInputFieldList[index];
switch (data.categoryTypeId) {
case "2":
var dropdownList = data.attributes
.map((e) => e.attributeName)
.toSet()
.toList();
dropdownList.insert(0,
data.categoryFieldName); // Add data.categoryFieldName at the 0th index
return Padding(
padding:
EdgeInsets.symmetric(vertical: 10.h),
child: SelectOptionForAd(
index: index,
dropdownName: data.categoryFieldName,
dropDownValue: data.categoryFieldName,
dropdownlist: dropdownList,
),
);
case "6":
return Padding(
padding:
EdgeInsets.symmetric(vertical: 10.h),
child: AdInputField(
onChanged: (newValue) {
value.selectedValueList[index]
.value[0] = newValue.toString();
},
title: data.categoryFieldName,
// controller: TextEditingController(),
keybordType: TextInputType.number),
);
case "1":
return Padding(
padding:
EdgeInsets.symmetric(vertical: 10.h),
child: AdInputField(
onChanged: (newValue) {
value.selectedValueList[index]
.value[0] = newValue.toString();
// setState(() {});
},
title: data.categoryFieldName,
// controller: TextEditingController(),
keybordType:
TextInputType.emailAddress),
);
case "3":
return Padding(
padding:
EdgeInsets.symmetric(vertical: 10.h),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
H2semi(text: data.categoryFieldName),
for (int index = 0;
index < data.attributes.length;
index++)
RadioListTile(
title: H3semi(
text: data.attributes[index]
.attributeName),
value: index,
groupValue:
data.categoryFieldId,
onChanged: (newValue) {
value.updateRadioValue(
newValue,
index,
data.attributes[index]
.systemValue);
}),
],
),
);
case "4":
return SizedBox(
height: 300.h,
width: 1.sw,
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
H2semi(text: data.categoryFieldName),
Expanded(
child: GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio:
0.45.sw / 24.h,
),
itemCount:
data.attributes.length,
itemBuilder:
(context, attributesIndex) {
print(
'check boxes length: ${data.attributes.length}');
print(
'checkboxes selected values: ${value.selectedValueList[index].value}');
return SizedBox(
width: 0.45.sw,
child: ExtraCheckBox(
mainListIndex: index,
data: data,
attributeIndex:
attributesIndex,
label: data
.attributes[
attributesIndex]
.attributeName,
isChecked: false),
);
}),
),
],
),
);
case "7":
return Container(
padding:
EdgeInsets.symmetric(vertical: 10.h),
alignment: Alignment.topLeft,
height: 210.h,
width: 350.w,
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(3.r),
border: Border.all(
width: 1,
color: ksecondaryColor2,
),
),
child: TextField(
onChanged: (newValue) {
value.selectedValueList[index].value
.insert(0, newValue.toString());
},
maxLines: 15,
keyboardType: TextInputType.multiline,
style: TextStyle(
fontSize: 12.sp,
fontFamily: GoogleFonts.montserrat()
.fontFamily,
fontWeight: FontWeight.w400,
color: Theme.of(context).primaryColor,
),
decoration: InputDecoration(
hintText: TextName.describeYourCar,
enabledBorder:
const OutlineInputBorder(
borderSide: BorderSide.none,
),
focusedBorder:
const OutlineInputBorder(
borderSide: BorderSide.none,
),
),
),
);
case "8":
return GestureDetector(
onTap: () async {
await getImages(data: data);
print(data.attributes);
},
child: Container(
alignment: Alignment.center,
padding: EdgeInsets.symmetric(
vertical: 10.h),
width: 350.w,
height: 48.h,
decoration: BoxDecoration(
border: Border.all(
width: 2.w,
color: khelperTextColor,
),
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.center,
children: [
SvgPicture.asset(
'assets/camera.svg'),
SizedBox(
width: 5.w,
),
H3semi(text: TextName.addPictures),
],
),
),
);
default:
return Center(
child: H2Bold(text: 'No Data'),
);
}
}),
),
Padding(
padding: EdgeInsets.symmetric(vertical: 20.h),
child: MainButton(
isLoading:
Provider.of<AddSummary>(context, listen: true)
.isLoading,
text: 'Next',
onTap: () {
context
.read<AddSummary>()
.postAddSummary(value.selectedValueList);
},
),
),
],
),
);
}),
);
}
}
如果您查看文本字段,我将直接从 onchanged() 函数获取值。问题是在这种情况下我没有使用 TextInputField 控制器。因此,当我从下拉列表中选择一个值时,我使用 setstate 在下拉列表中显示所选值。 setstate 重建屏幕,因为我没有使用任何控制器,在文本字段中输入的值被删除,文本字段被重置。
因此,如果屏幕重建文本字段将保留其值,请使用文本字段控制器...