简体   繁体   English

如何在颤动中将函数传递给可重用的文本输入小部件?

[英]How to pass Function to a reusable Text Input widget in flutter?

I just refactor my code and make all my input widget as a separate widget as follows: import 'package:flutter/material.dart';我只是重构我的代码并将所有输入小部件作为一个单独的小部件,如下所示: import 'package:flutter/material.dart';

class textInput extends StatelessWidget {
  const textInput({
    Key? key,
    required this.label,
    required this.inputController,
    required this.inputIcon,
    this.helperText,
    this.suffixHelpText,
  }) : super(key: key);

  final String label;
  final TextEditingController inputController;
  final Icon inputIcon;
  final Widget? suffixHelpText;
  final String? helperText;

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: inputController,
      decoration: InputDecoration(
        labelText: label,
        helperText: helperText != null ? helperText : '',
        icon: inputIcon,
        suffixIcon: suffixHelpText,
      ),
    );
  }
}

Now I want to pass OnChnaged event to this widget which can be optional.现在我想将OnChnaged事件传递给这个可以是可选的小部件。 I am trying as follows:我正在尝试如下:

import 'package:flutter/material.dart';

class textInput extends StatelessWidget {
  const textInput({
    Key? key,
    required this.label,
    required this.inputController,
    required this.inputIcon,
    this.helperText,
    this.suffixHelpText,
    this.onChangeEvent,
  }) : super(key: key);

  final String label;
  final TextEditingController inputController;
  final Icon inputIcon;
  final Widget? suffixHelpText;
  final String? helperText;
  final Function? onChangeEvent;
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: inputController,
      onChanged: onChangeEvent !=null? onChangeEvent:(){},
      decoration: InputDecoration(
        labelText: label,
        helperText: helperText != null ? helperText : '',
        icon: inputIcon,
        suffixIcon: suffixHelpText,
      ),
    );
  }
}

But getting error with this onChanged: onChangeEvent !=null? onChangeEvent:(){},但是这个onChanged: onChangeEvent !=null? onChangeEvent:(){}, onChanged: onChangeEvent !=null? onChangeEvent:(){}, line. onChanged: onChangeEvent !=null? onChangeEvent:(){},行。 Am I trying something which is not possible in deed?我在尝试一些实际上不可能的事情吗? Or Is there any good approach?或者有什么好的方法吗? I love to clean my code in entire project.我喜欢在整个项目中清理我的代码。 Please help.请帮忙。

The Function expected in onChanged is void Function(String)? onChanged onChanged中预期的Functionvoid Function(String)? onChanged void Function(String)? onChanged

Just change只是改变

final Function? onChangeEvent

as below:如下:

final Function(String?)? onChangeEvent;

and just pass the onChangeEvent as below:只需传递onChangeEvent如下:

onChanged: onChangeEvent
onChanged: onChangeEvent != null ? (_) => onChangeEvent!() : null, 

works for me.为我工作。 Hope this will help other.希望这对其他人有帮助。 Here is the full code:这是完整的代码:

import 'package:flutter/material.dart';

class textInput extends StatelessWidget {
  const textInput({
    Key? key,
    required this.label,
    required this.inputController,
    required this.inputIcon,
    this.helperText,
    this.suffixHelpText,
    this.onChangeEvent,
  }) : super(key: key);

  final String label;
  final TextEditingController inputController;
  final Icon inputIcon;
  final Widget? suffixHelpText;
  final String? helperText;
  final Function? onChangeEvent;
  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: inputController,
      onChanged: onChangeEvent != null ? (_) => onChangeEvent!() : null,
      decoration: InputDecoration(
        labelText: label,
        helperText: helperText != null ? helperText : '',
        icon: inputIcon,
        suffixIcon: suffixHelpText,
      ),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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