0

I am building a chatbot using flutter. I have listview of buttons inside a container. If the user presses any button, the container must disappear. I tried with a boolean variable but nothing changes as we cannot alter the state. I cannot change the class to state because it affects other functionality. is there any method to achieve this?

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;
  }
}
ramya
  • 33
  • 1
  • 8
  • 1
    If you want to update UI in a Stateless widget its not possible. You would neeed to use a Statemanagement package such as Provider or Bloc – DEFL Dec 28 '21 at 20:25
  • Thank you for reply ! . i will try to use Bloc – ramya Dec 29 '21 at 05:43

1 Answers1

0

Visibility class Whether to show or hide a child.

By default, the visible property controls whether the child is included in the subtree or not; when it is not visible, the replacement child (typically a zero-sized box) is included instead. https://api.flutter.dev/flutter/widgets/Visibility-class.html

omid
  • 11
  • 2