DEV Community

loading...

Discussion on: Star Rating with Flutter Reactive Forms

Collapse
inmer profile image
Inmer

Hi Joan, thanks for reply.

I should clarify my question my bad sorry, RDateTimePicker is just a visual customization of date_time_picker from m3uzz.com on pub.dev, here is my implementation:

class RDateTimePicker extends StatelessWidget {
  final TextEditingController controller;
  final String labelText;
  final Function(String) validator;
  final Function(String) onChanged;

  const RDateTimePicker({
    Key key,
    this.controller,
    this.labelText,
    this.validator,
    this.onChanged,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.all(5),
      child: DateTimePicker(
          onChanged: onChanged,
          controller: controller,
          style:
              TextStyle(color: Colors.black, decoration: TextDecoration.none),
          decoration: InputDecoration(
            labelText: labelText,
            enabledBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.black)),
            focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(color: Colors.black)),
            labelStyle: TextStyle(color: Colors.black)
          ),
          validator: validator),
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

And with "normal" form, I am able to show errors using validator property, and a controller. Can't I use its implementation to show the error or anyway I have to create an InputDecorator. By the way I would like an example for those InputDecorator, because I have created a completely custom component in which I would need what you say. And thanks for such a great library.

Thread Thread
joanpablo profile image
Joan Pablo Author • Edited

There you have an InputDecoration. You should avoid using validator, and instead use the errorText of the decoration. You can search for the implementation of the ReactiveTextField to see how it is using this approach.

builder: (ReactiveFormFieldState field) {
   final state = field as _ReactiveTextFieldState;
   final InputDecoration effectiveDecoration = (decoration ?? const InputDecoration())
   .applyDefaults(Theme.of(state.context).inputDecorationTheme);

   return DateTimePicker(
      decoration: effectiveDecoration.copyWith(errorText: state.errorText),
   );         

Enter fullscreen mode Exit fullscreen mode

In the above code, I'm not using your RDateTimePicker just to simplify things, but it also applies with your wrapper widget.

Thread Thread
inmer profile image
Inmer

Thank you, Joan finally I was able to make it work for both cases, greatly appreciate your help.