【问题标题】:How can I pass a custom widget into another custom widget in flutter?如何将自定义小部件传递给颤振中的另一个自定义小部件?
【发布时间】:2020-05-08 17:20:07
【问题描述】:

我正在尝试将自定义容器(具有背景颜色、标题和 onPressed 属性)传递到另一个自定义小部件,该小部件创建一行三个这些容器。目标是能够在第二个小部件中为每个按钮输入标题,例如 TriButton(title1, title2, title3)。任何提示或技巧将不胜感激!

自定义容器

class RectButton extends StatelessWidget {
  RectButton({this.buttonChild, this.bgColor, this.onPress});

  final Widget buttonChild;
  final Color bgColor;
  final Function onPress;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPress,
      child: Container(
        constraints: BoxConstraints.expand(width: 100, height: 50),
        child: Center(child: buttonChild),
        decoration: BoxDecoration(
            color: bgColor,
            shape: BoxShape.rectangle,
            border: Border.all(width: 1, color: Colors.white)),
        padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
      ),
    );
  }
}

`Tri-button code`

enum Weight {
  ideal,
  actual,
  adjusted,
}

class TriButton extends StatefulWidget {
  TriButton({this.title1, this.title2, this.title3, this.buttonChild});

  final Text title1;
  final Text title2;
  final Text title3;
  final RectButton buttonChild;

  @override
  _TriButtonState createState() => _TriButtonState();
}

class _TriButtonState extends State<TriButton> {
  Weight selectedWeight;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        constraints: BoxConstraints(maxWidth: 300),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(
              child: RectButton(
                buttonChild: GOAL TO ENTER TITLE HERE,
                onPress: () {
                  setState(() {
                    selectedWeight = Weight.adjusted;
                  });
                },
                bgColor: selectedWeight == Weight.adjusted
                    ? Colors.orange[600]
                    : Colors.grey[600],
              ),
            ),

【问题讨论】:

  • 你不能在你的行中再添加两个 RectButtons 吗?
  • 我确实有两个其他按钮(在上面的示例中将它们关闭以节省空间)但是当我将 RectButton 实现到 TriButton 类中时,它不会让我插入标题。我不知道如何为 RectButton 授予属性,以便我可以在 TriButton 中自定义它

标签: flutter enums widget final


【解决方案1】:

使用 StatefulWidget 时,您需要在实现中使用“widget.property”。

你的情况

 Expanded(
              child: RectButton(
                buttonChild: Text(widget.title1),
                onPress: () {
                  setState(() {
                    selectedWeight = Weight.adjusted;
                  });
                },
                bgColor: selectedWeight == Weight.adjusted
                    ? Colors.orange[600]
                    : Colors.grey[600],
              ),
            ),
 Expanded(
              child: RectButton(
                buttonChild: Text(widget.title2),
                onPress: () {
                  setState(() {
                    selectedWeight = Weight.adjusted;
                  });
                },
                bgColor: selectedWeight == Weight.adjusted
                    ? Colors.orange[600]
                    : Colors.grey[600],
              ),
            ),

.....

【讨论】:

  • Rleffler 感谢您的帮助!当我尝试使用“widget.property”方法时,我收到错误消息“参数类型'String'不能分配给参数类型'Widget'”
猜你喜欢
  • 2021-08-19
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 2020-07-27
  • 2021-11-06
  • 2019-12-20
  • 1970-01-01
相关资源
最近更新 更多