我正在创建颤振应用程序。我还使用 GetX 进行状态管理。我创建了自定义文本字段来满足我的期望。在这里,我创建了两个相邻的文本字段,并希望在按下图片时更新它们的值。值正在正确更改,但是每当我开始编写光标时,问题就不是在每个字母后继续,而是从每个字母后的起点开始。
但是,如果我删除 obx,什么也没有发生,光标行为正常,但 UI 没有更新。
这是与此问题相关的代码部分:
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
SizedBox(
height: Dimensions.height50,
width: Dimensions.width130,
child: Obx(
() => CustomTextField(
onChanged: (value) => searchCargoesController
.fromWhereController.value = value,
hintText: "from".tr,
textEditingController: TextEditingController(
text: searchCargoesController
.fromWhereController.value,
),
textInputType: TextInputType.streetAddress,
),
),
),
InkWell(
onTap: () {
var from =
searchCargoesController.fromWhereController.value;
var to =
searchCargoesController.toWhereController.value;
searchCargoesController.fromWhereController.value = to;
searchCargoesController.toWhereController.value = from;
searchCargoesController.update();
},
child: SizedBox(
width: Dimensions.width20,
child: SvgPicture.asset("assets/icons/from_to1.svg"),
),
),
SizedBox(
height: Dimensions.height50,
width: Dimensions.width130,
child: Obx(
() => CustomTextField(
onChanged: (value) => searchCargoesController
.toWhereController.value = value,
hintText: "to".tr,
textEditingController: TextEditingController(
text:
searchCargoesController.toWhereController.value,
),
textInputType: TextInputType.streetAddress,
),
),
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(
Dimensions.radius10,
),
gradient: const LinearGradient(
colors: [
Colors.amber,
Colors.yellow,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: IconButton(
onPressed: () {},
icon: Icon(
Icons.search_outlined,
color: Colors.white,
size: Dimensions.icon20,
),
),
),
],
),
有人知道我该如何修复它吗?
确保每个 CustomTextField 都有唯一的键。如果未提供密钥或密钥不唯一,则可能会导致意外行为。如果这不能解决问题,请提供更多与 CustomTextField 相关的代码
// Assuming you have GetX controllers like this:
class SearchCargoesController extends GetxController {
final fromWhereController = "".obs;
final toWhereController = "".obs;
}
// CustomTextField usage in your widget:
Obx(
() => CustomTextField(
onChanged: (value) => searchCargoesController.fromWhereController.value = value,
// Other properties...
),
),
InkWell(
onTap: () {
var from = searchCargoesController.fromWhereController.value;
var to = searchCargoesController.toWhereController.value;
searchCargoesController.fromWhereController.value = to;
searchCargoesController.toWhereController.value = from;
searchCargoesController.update(); // Ensure you update the controller after changing the values.
},
child: SvgPicture.asset("assets/icons/from_to1.svg"),
),
Obx(
() => CustomTextField(
onChanged: (value) => searchCargoesController.toWhereController.value = value,
// Other properties...
),
),