繁体   English   中英

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

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

我只是重构我的代码并将所有输入小部件作为一个单独的小部件,如下所示: 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,
      ),
    );
  }
}

现在我想将OnChnaged事件传递给这个可以是可选的小部件。 我正在尝试如下:

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,
      ),
    );
  }
}

但是这个onChanged: onChangeEvent !=null? onChangeEvent:(){}, onChanged: onChangeEvent !=null? onChangeEvent:(){},行。 我在尝试一些实际上不可能的事情吗? 或者有什么好的方法吗? 我喜欢在整个项目中清理我的代码。 请帮忙。

onChanged中预期的Functionvoid Function(String)? onChanged void Function(String)? onChanged

只是改变

final Function? onChangeEvent

如下:

final Function(String?)? onChangeEvent;

只需传递onChangeEvent如下:

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

为我工作。 希望这对其他人有帮助。 这是完整的代码:

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