简体   繁体   中英

How to pass flutter textFormField onChange and onSubmitted as a parameter?

I'm trying to create a custom TextFormField that will accepts parameters that I needed like labelText , controller or prefixIcon etc. What I don't know is how to pass a function to an OnChange and onSubmitted events. Please check the sample code below.

utils.dart

class TextFieldTemplate extends StatelessWidget {
  final String initialValue;
  final String labelText;
  final String hintText;
  final IconData prefixIcon;
  
  final TextEditingController controller;
  final Function onTap;

  TextFieldTemplate(
      {this.initialValue,
      this.labelText,
      this.prefixIcon,
      this.controller,
      });

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      onChanged: (){},
       onSubmitted: (){},
      controller: controller,
      decoration: InputDecoration(
        contentPadding: decorationPadding,
        labelText: labelText,
        prefixIcon: prefixIcon != null
            ? Icon(
                prefixIcon,
              )
            : null,
      ),
    );
  }
}

Usage for the TextFieldTemplate:

     var _personName = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            childtren[
              TextFieldTemplate(
                labelText: 'Person Name',
                controller: _personName,
                prefixIcon: Icons.person,
//              onSubmitted : ???
//              OnChange : ???
              ),
              ]
          ),
        );
      }

You can pass onChanged and onSubmitted parameter like this

Function(String) onChanged;
Function(String) onSubmitted;

And use like this

TextFieldTemplate(
                labelText: 'Person Name',
                controller: _personName,
                prefixIcon: Icons.person,
                onSubmitted: (value){},
                onChanged: (value){},
              ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM