#android #flutter #widget
Вопрос:
прямо сейчас я пытаюсь создать страницу редактирования для редактирования профиля пользователя. И у меня есть ConsumerStateFulWidget, который был создан для создания виджета TextFormField, чтобы избежать повторения кода, и у меня здесь небольшая проблема. wehat происходит следующее, У меня есть слушатель на riverpod поставщика, которая делает вызов API, чтобы получить usere детали, после этого он перестраивает форму с данными и поля формы, которые в раздельных виджет с отслеживанием состояния, который я создал, и которые имеют 3 параметра, контроллер поля, а formKey, и значение, которое внутри виджета я назначить контроллер текста.
Проблема здесь в том, что при построении формы: 1 — она не отображает данные 2 — Значения, которые поступают из API, отображаются как пустые в виджете поля формы и не перестраиваются
Вот мой виджет textFormFieeld и часть моей формы, в которой я отправляю параметры виджету
class TextInputWidget extends ConsumerStatefulWidget {
final String title;
final int lines;
String value;
final TextEditingController? controller;
final formKey;
TextInputWidget({required this.title, this.lines = 1, this.controller, this.formKey, this.value = ""});
@override _TextInputWidget createState() => _TextInputWidget();
}
class _TextInputWidget extends ConsumerState<TextInputWidget> {
@override
void initState() {
print(widget.value);
widget.controller?.text = widget.value;
super.initState();
}
@override
Widget build(BuildContext context) {
return SizedBox(
height: widget.lines == 1 ? 4 * SizeConfig.heightMultiplier : null,
child: Align(
child: TextFormField(
key: widget.key,
controller: widget.controller,
maxLines: widget.lines,
autocorrect: false,
enableSuggestions: false,
cursorColor: AppColors.lightGrayText,
style: const TextStyle(
color: AppColors.lightGrayText,
decoration: TextDecoration.none,
),
decoration: InputDecoration(
contentPadding: widget.lines == 1
? EdgeInsets.only(left: 2 * SizeConfig.widthMultiplier)
: null,
alignLabelWithHint: true,
labelText: widget.title,
labelStyle: TextStyle(
fontSize: 1.5 * SizeConfig.textMultiplier,
color: AppColors.lightGrayText,
decoration: TextDecoration.none,
),
fillColor: AppColors.lightGrayText,
focusedBorder: OutlineInputBorder(
borderSide:
const BorderSide(color: AppColors.darkBlue, width: 0.8),
borderRadius: BorderRadius.circular(5),
),
enabledBorder: OutlineInputBorder(
borderSide:
const BorderSide(color: AppColors.borderColor, width: 0.8),
borderRadius: BorderRadius.circular(5),
),
),
),
),
);
}
}
SizedBox(
width: 50 * SizeConfig.widthMultiplier,
child: TextInputWidget(title: "Nome", controller: usernameController, formKey: formKey, value: userNotifier.user?.userName ?? "",),
),
const VerticalSpacer(divider: 15),
SizedBox(
width: 50 * SizeConfig.widthMultiplier,
child: TextInputWidget(title: "Telefone", controller: emailController, formKey: formKey,),
),
const VerticalSpacer(divider: 15),
SizedBox(
width: 50 * SizeConfig.widthMultiplier,
child: TextInputWidget(title: "Email", controller: phoneController, formKey: formKey,),
),
const VerticalSpacer(divider: 15),
Заранее спасибо, ребята!