Skip to content

Commit 9553493

Browse files
committed
feat: read only list (MarcinusX#127)
1 parent c11a01b commit 9553493

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lib/src/numberpicker.dart

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class NumberPicker extends StatefulWidget {
1515
final int value;
1616

1717
/// Called when selected value changes
18-
final ValueChanged<int> onChanged;
18+
final ValueChanged<int>? onChanged;
1919

2020
/// Specifies how many items should be shown - defaults to 3
2121
final int itemCount;
@@ -61,7 +61,7 @@ class NumberPicker extends StatefulWidget {
6161
required this.minValue,
6262
required this.maxValue,
6363
required this.value,
64-
required this.onChanged,
64+
this.onChanged,
6565
this.itemCount = 3,
6666
this.step = 1,
6767
this.itemHeight = 50,
@@ -110,9 +110,11 @@ class _NumberPickerState extends State<NumberPicker> {
110110
_intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide);
111111

112112
if (widget.value != intValueInTheMiddle) {
113-
widget.onChanged(intValueInTheMiddle);
114-
if (widget.haptics) {
115-
HapticFeedback.selectionClick();
113+
if (widget.onChanged != null) {
114+
widget.onChanged!(intValueInTheMiddle);
115+
if (widget.haptics) {
116+
HapticFeedback.selectionClick();
117+
}
116118
}
117119
}
118120
Future.delayed(
@@ -166,6 +168,7 @@ class _NumberPickerState extends State<NumberPicker> {
166168
children: [
167169
if (widget.infiniteLoop)
168170
InfiniteListView.builder(
171+
physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(),
169172
scrollDirection: widget.axis,
170173
controller: _scrollController as InfiniteScrollController,
171174
itemExtent: itemExtent,
@@ -174,6 +177,7 @@ class _NumberPickerState extends State<NumberPicker> {
174177
)
175178
else
176179
ListView.builder(
180+
physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(),
177181
itemCount: listItemsCount,
178182
scrollDirection: widget.axis,
179183
controller: _scrollController,
@@ -215,11 +219,13 @@ class _NumberPickerState extends State<NumberPicker> {
215219
return InkWell(
216220
onTap: () {
217221
if (!isExtra) {
218-
widget.onChanged(value);
219-
Future.delayed(
220-
const Duration(milliseconds: 100),
221-
() => _maybeCenterValue(),
222-
);
222+
if (widget.onChanged != null) {
223+
widget.onChanged!(value);
224+
Future.delayed(
225+
const Duration(milliseconds: 100),
226+
() => _maybeCenterValue(),
227+
);
228+
}
223229
}
224230
},
225231
child: Container(

0 commit comments

Comments
 (0)