【问题标题】:How to change color of selected item in ListWheelScrollView?如何更改 ListWheelScrollView 中所选项目的颜色?
【发布时间】:2019-11-04 08:05:06
【问题描述】:

我有一个 ListWheelScrollView 小部件。我需要当我滚动列表时,所选项目的字体颜色会发生变化,例如突出显示。如何实施?

更新:仍然不知道如何实现这个...

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(),
                  ),
                )),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

  • 发布完整代码。

标签: flutter


【解决方案1】:

ListWheelScrollView 具有 FixedExtentScrollController 属性。可以使用此控制器到达selectedItem。

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;
}

它返回 int 类型,您可以将其自定义到您的列表中。

【讨论】:

  • 谢谢!你能举一个小例子来说明如何做到这一点吗?
【解决方案2】:

首先,你需要定义一个状态_selectedItemIndex:

int _selectedItemIndex = 0;

因为ListwheelScrollView 有一个名为onSelectedItemChanged 的函数,您可以使用它来突出显示一个项目。

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

用上面的函数设置每次更改索引的状态后,您现在可以更改在您的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(),
                 )

我只是尝试分析和修复颜色变化的逻辑。我不确定其余的。希望我对你有用。

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多