我正在使用颤振构建一个聊天机器人。我有一个容器内的按钮列表视图。如果用户按下任何按钮,容器必须消失。我尝试了一个布尔变量,但没有任何变化,因为我们无法改变状态。我无法将类更改为 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;
}
}