【问题标题】:flutter Error when using color swatch inside PopupMenuItem在 PopupMenuItem 中使用色板时出现颤振错误
【发布时间】:2023-04-02 02:59:01
【问题描述】:

我有以下代码:

class _StandardCardState extends State<StandardCard> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Card(
        margin: EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0.0),
        child: Padding(
          padding: const EdgeInsets.all(12.0),
          child: Row(
            children: <Widget>[
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        Text(
                          'L*: ',
                          style: TextStyle(
                              fontSize: 18.0, color: Colors.grey[600]),
                        ),
                        Text(
                          widget.standard.L.toString(),
                          style: TextStyle(
                              fontSize: 18.0, color: Colors.grey[600]),
                        ),
                      ],
                    ),
                    SizedBox(height: 6.0),
                    Row(
                      children: <Widget>[
                        //some children
                      ],
                    ),
                    SizedBox(height: 6.0),
                    Row(
                      children: <Widget>[
                        //some children
                      ],
                    ),
                  ],
                ),
              ),
              PopupMenuButton<WhyFarther>(
                onSelected: (WhyFarther result) {
                  setState(() {
                    var _selection = result;
                  });
                },
                itemBuilder: (BuildContext context) =>
                    <PopupMenuEntry<WhyFarther>>[
                  const PopupMenuItem<WhyFarther>(
                    value: WhyFarther.harder,
                    child: Text(
                      'Working a lot harder',
                      style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
                    ),
                  ),
                  const PopupMenuItem<WhyFarther>(
                    value: WhyFarther.smarter,
                    child: Text(
                      'Being a lot smarter',
                      style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
                    ),
                  ),
                  const PopupMenuItem<WhyFarther>(
                    value: WhyFarther.selfStarter,
                    child: Text(
                      'Being a self-starter',
                      style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
                    ),
                  ),
                  const PopupMenuItem<WhyFarther>(
                    value: WhyFarther.tradingCharter,
                    child: Text(
                      'Placed in charge of trading charter',
                      style: TextStyle(fontSize: 18.0, color: Colors.grey[600]),
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

它仍然被ListView.builder 通过另一个函数调用。当我尝试使用 Swatches 更改 popupMenuItem 中的文本样式时,我收到错误“'Invalid constant value'”。

为什么这会引发错误,而上面的文本小部件却没有?那么我该如何避免这种情况以及如何在 PopupMenuButtons 中使用色板呢?

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    我只需要在 PopupMenuItem 之前删除“const”...有时在复制编码时阅读会很有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-13
      • 2020-04-01
      • 2021-06-17
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 2021-03-20
      • 2021-10-31
      相关资源
      最近更新 更多