【问题标题】:Flutter Custom Widget Property TransferFlutter 自定义 Widget 属性转移
【发布时间】:2019-04-30 11:13:58
【问题描述】:

我想为 TextField 小部件创建一个自定义小部件,并且我想将所有属性传输到我的自定义小部件。我在 React Native 中使用过 ...props 语法。

我的自定义小部件:

return Container(
  decoration: BoxDecoration(
      color: Colors.white,
      border: Border.all(
        color: Colors.grey,
        width: 1,
      ),
      borderRadius: BorderRadius.all(Radius.circular(5))),
  margin: margin,
  padding: EdgeInsets.symmetric(horizontal: 10),
  child: TextField(
    decoration: InputDecoration(
        labelStyle: TextStyle(color: Colors.black),
        labelText: label,
        border: InputBorder.none,
        counterText: ""),
    textDirection: TextDirection.ltr,
    //I want to put here custom widget properties
  ),
);

我想使用自定义小部件,例如:

Input(
    label: "Şifre",
    margin: EdgeInsets.only(bottom: marginBottom),
    obscureText: true, //**
    textInputAction: TextInputAction.send, //**
    controller: passwordController, //**
)

我想将带注释的 ** 属性转移到自定义小部件的注释行

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您使用的是TextField,但要查找TextFormField。它收集了您要求的所有属性。

      TextFormField(
          decoration: InputDecoration(
            labelStyle: TextStyle(color: Colors.black),
            labelText: label,
            border: InputBorder.none,
            counterText: ""),
            obscureText: true,
            textInputAction: TextInputAction.send,
            controller: passwordController,
        );
    

    【讨论】:

    • 感谢您的回答。但这是一个使用示例。我想将它用于其他小部件。有没有办法做到这一点?我研究过,但没有结果。
    • 什么意思?您可以随时使用TextFormField。毕竟是一个小部件。
    • 我想创建一个全局小部件,但我不想手动创建所有属性。在 react native {...props} 语法中进行此操作。在颤振中使用相同吗?
    • 但是您已经在 TextFormField 小部件中拥有所有这些属性。我没有得到你真正想要的东西。
    • TextField 就是一个例子。我只想将其他小部件的功能提供给另一个小部件。例如,我想创建一个自定义相机小部件,它具有按钮小部件的所有属性。我希望我能告诉你我想要什么。
    猜你喜欢
    • 2020-10-10
    • 2022-08-10
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多