简体   繁体   中英

How to change color of selected item in ListWheelScrollView?

I have a ListWheelScrollView widget. I need that when I scroll the list, the font color of the selected item changes, something like highlighting. How can this be implemented?

Update: Still no idea how to implement this...

body: new Column(
          children: <Widget>[
               ListWheelScrollView.useDelegate(
                  perspective: 0.001,
                  diameterRatio: 10,
                  useMagnifier: true,
                  itemExtent: _itemHeight,
                  childDelegate: ListWheelChildLoopingListDelegate(
                    children: _values
                        .map(
                          (value) => SizedBox(
                            width: MediaQuery.of(context).size.width,
                            height: _itemHeight,
                            child: Container(
                              margin: EdgeInsets.fromLTRB(0, 0.0, 0.0, 3),
                              color: Colors.white,
                                child: Align(
                                  alignment: Alignment.center,
                                  child: Text(
                                    '$value',
                                    textAlign: TextAlign.center,
                                    style: TextStyle(
                                        fontWeight: FontWeight.w600,
                                        fontSize: 17,
                                        color:
                                            Color...,
                                  ),
                                ),
                                ),
                              ),
                            ),
                          ),
                        )
                        .toList(),
                  ),
                )),
          ],
        ),
      ),
    );
  }
}

ListWheelScrollView has FixedExtentScrollController property. Could reach selectedItem with this controller.

int get selectedItem {
  assert(
    positions.isNotEmpty,
    'FixedExtentScrollController.selectedItem cannot be accessed before a '
    'scroll view is built with it.',
  );
  assert(
    positions.length == 1,
    'The selectedItem property cannot be read when multiple scroll views are '
    'attached to the same FixedExtentScrollController.',
  );
  final _FixedExtentScrollPosition position = this.position;
  return position.itemIndex;
}

It returns int type and you could customize it into your list.

You should check if the item is selected from the data source and change the style based on that. something like the code below:

final txtStyle = TextStyle(color: Colors.white, fontWeight: FontWeight.w400, fontSize: 12.0);

final selectedTxtStyle = TextStyle(color: Colors.blue, fontWeight: FontWeight.w600, fontSize: 14.0);

body: new Column(
      children: <Widget>[
           ListWheelScrollView.useDelegate(
              perspective: 0.001,
              diameterRatio: 10,
              useMagnifier: true,
              itemExtent: _itemHeight,
              childDelegate: ListWheelChildLoopingListDelegate(
                children: _values
                    .map(
                      (value) => SizedBox(
                        width: MediaQuery.of(context).size.width,
                        height: _itemHeight,
                        child: Container(
                          margin: EdgeInsets.fromLTRB(0, 0.0, 0.0, 3),
                          color: Colors.white,
                            child: Align(
                              alignment: Alignment.center,
                              child: Text(
                                '$value',
                                textAlign: TextAlign.center,
                                style: '$selected' ? selectedTxtStyle : txtStyle,
                            ),
                            ),
                          ),
                        ),
                      ),
                    )
                    .toList(),
              ),
            )),
      ],
    ),
  ),
);

} }

First, you need to define a state _selectedItemIndex :

int _selectedItemIndex = 0;

Because ListwheelScrollView has a Function called onSelectedItemChanged that you can use to highlight an item.

 child: new ListWheelScrollView.useDelegate(
              onSelectedItemChanged: (index) {
                setState(() {
                  _selectedItemIndex = index;
                });
              },

After setting the state of the index each time it is changed with the function above, you can now change the through color in the container made in your childDelegate:

              childDelegate: ListWheelChildLoopingListDelegate(
                children: _values
                    .map(
                      (value) => SizedBox(
                        child: Container(
                          color: Colors.white,
                            child: Text(
                                '$value',
                                style: TextStyle(
                                    color:
                                        _selectedItemIndex == _values.indexOf(value)
                            ? Colors.pink
                            : Colors.grey,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  .toList(),
                 )

I only tried to analyse and fix the logic of the color changing. I am not sure about the rest. Hopefully I have been of use to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM