首页 > 解决方案 > 如何在颤动中显示更多数字选择器列表的元素?

问题描述

我用来为我的颤振应用程序开发一个数字选择器,但它只显示数字选择器中数字列表的三个元素。我想显示更多元素(例如 7),此外,我希望所选元素恰好是中间元素(在我的情况下是 3 个元素之前和 3 个之后)应该更改库代码的哪一部分?

当仅更改“_listViewHeight = 3 * itemExtent”时,在库中我们会看到我在下图中提到的问题。首先选中的Item不是最大值旁边的中间那个不能被选中。

在此处输入图像描述

标签: flutterdartnumberpicker

解决方案


I had this same problem and was able to get it working for odd display number lengths of 3 or greater after digging into the numberpicker code and making some changes. Here is a diff of the changes I made. This is working for me using my repo. You can also just copy the lib/numberpicker.dart file from my repo into your project and include that file. Hopefully will get this pulled into the numberpicker project at some point. Below is my example use for 7 numbers displayed at a time:

NumberPicker.horizontal(
    initialValue: _currentValue,
    minValue: 1,
    maxValue: 10,
    numberToDisplay: 7,
    onChanged: (newValue) =>
        setState(() => _currentValue = newValue));

enter image description here

and scrolled to the first number

enter image description here

you can pick a different odd number for the numberToDisplay parameter such as 5

enter image description here

and removing the parameter will use the default of 3.

enter image description here

More info here


推荐阅读