0

我正在使用颤振构建一个聊天机器人。我有一个容器内的按钮列表视图。如果用户按下任何按钮,容器必须消失。我尝试了一个布尔变量,但没有任何变化,因为我们无法改变状态。我无法将类更改为 state,因为它会影响其他功能。有什么方法可以实现吗?

class Body extends StatelessWidget {
  bool show = true;
  Body({
    Key? key,
    required this.show,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView.separated(
         //properties of bigger list view
        return Column(
          crossAxisAlignment:
              isUserMessage ? CrossAxisAlignment.end : CrossAxisAlignment.start,
          children: [
            Row(
             //row contents
            ),
            SizedBox(height: 20.0),
            if (show != null)
              !show
                  ? const SizedBox.shrink()
                  : Container(
                      constraints: BoxConstraints(maxWidth: 250),
                      padding: EdgeInsets.only(top: 10),
                      child: ListView.builder(
                          itemCount: label.length,
                          shrinkWrap: true,
                          itemBuilder: (context, i) {
                            return ElevatedButton(
                              child: Text(label[i]),
                              onPressed: () => {hi!(label[i]), show = false},
                              style: ElevatedButton.styleFrom(
                                  primary: Colors.blueAccent,
                                  padding: EdgeInsets.symmetric(
                                      horizontal: 10, vertical: 5),
                                  textStyle:
                                      TextStyle(fontWeight: FontWeight.bold)),
                            );
                          }))
          ],
        );
      },
      separatorBuilder: (_, i) => Container(height: 10),
      itemCount: messages.length,
      reverse: true,
      padding: const EdgeInsets.symmetric(
        horizontal: 10,
        vertical: 20,
      ),
    );
  }

  void hide() {
    show = false;
  }
}
4

1 回答 1

0

可见性类 是否显示或隐藏子级。

默认情况下,visible 属性控制子树是否包含在子树中;当它不可见时,替换的孩子(通常是一个零大小的盒子)被包括在内。 https://api.flutter.dev/flutter/widgets/Visibility-class.html

于 2021-12-29T07:15:01.403 回答