GotJimmy / accordion

Other
46 stars 43 forks source link

open a section programmatically #66

Closed dustinpham235 closed 3 months ago

dustinpham235 commented 4 months ago

Hi Jimmy, thank you for such a great widget.

Is there a way to programmatically open a section by passing index? I'm trying to do have it auto open the next section when user scroll to the end of the previous section.

Sorry if I miss something obvious in the doc, but I've read back and forth a few times.

Thank you

GotJimmy commented 4 months ago

Hi Dustin, you're welcome.

You need to use some state management to set isOpen of a section dynamically. The following example uses GetX state management to do this:


  @override
  build(context) => Scaffold(
        appBar: AppBar(
          title: const Text('Accordion'),
        ),
        body: Column(
          children: [
            TextButton(
                child: const Text('1'), onPressed: () => section.value = 1),
            TextButton(
                child: const Text('2'), onPressed: () => section.value = 2),
            Obx(() => Accordion(
                  children: [
                    AccordionSection(
                      isOpen: section.value == 1,
                      header: const Text('Section 1'),
                      content: const Text('Section 1 content'),
                    ),
                    AccordionSection(
                      isOpen: section.value == 2,
                      header: const Text('Section 2'),
                      content: const Text('Section 2 content'),
                    ),
                  ],
        ),
          ),
          ],
        ),
      );

Hope this helps!

dustinpham235 commented 4 months ago

Thanks Jimmy, I'll give it a try 🙏