Home > Software engineering >  Can I get a logic/code for highlighting the desired value in the list view builder in flutter
Can I get a logic/code for highlighting the desired value in the list view builder in flutter

Time:10-12

[

Can I get a logic/code for highlighting just the selected value from this list view which is inside a container and it is scrollable also that the axis is set to Horizontal. I have used a list view builder to align the same and also generated the list of numbers. Please check the sample image of the widget attached.

Blockquote

]1

CodePudding user response:

It's hard to tell you exactly how to do it without any code examples, and I'm also not sure what you mean by selected. Is that already decided before building the list, or is it decided when the user selects from the list?

If it is already decided, you can pass a value from the parent component that tells the list to highlight a certain value.

If a user is selecting the value to highlight, you can use a combination of setState and onTap with GestureDetector. Here's a potential rough skeleton:

int selectedIndex?;

ListView.builder(
    itemCount: litems.length,
    itemBuilder: (BuildContext ctx, int index) {
    return GestureDetector(
        onTap: () {
            setState(() {
                selectedIndex = index;
            });
        },
        child: Container(
            backgroundColor: selectedIndex == index ? highlightedColor : undefined;
            child: {{child content}}
        ),
    );
    }
  )
  • Related