From 1c325adad56e4f550e37c40e62b69b4fe8844662 Mon Sep 17 00:00:00 2001 From: Matt Hilleman Date: Wed, 6 Oct 2021 12:10:51 -0500 Subject: [PATCH 1/9] onChanged allows NULL. This disables scrolling if no onChanged provided. --- lib/src/numberpicker.dart | 45 +++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/src/numberpicker.dart b/lib/src/numberpicker.dart index 63b97a1..b747ea1 100644 --- a/lib/src/numberpicker.dart +++ b/lib/src/numberpicker.dart @@ -16,7 +16,7 @@ class NumberPicker extends StatefulWidget { final int value; /// Called when selected value changes - final ValueChanged onChanged; + final ValueChanged? onChanged; /// Specifies how many items should be shown - defaults to 3 final int itemCount; @@ -62,7 +62,7 @@ class NumberPicker extends StatefulWidget { required this.minValue, required this.maxValue, required this.value, - required this.onChanged, + this.onChanged, this.itemCount = 3, this.step = 1, this.itemHeight = 50, @@ -89,11 +89,9 @@ class _NumberPickerState extends State { @override void initState() { super.initState(); - final initialOffset = - (widget.value - widget.minValue) ~/ widget.step * itemExtent; + final initialOffset = (widget.value - widget.minValue) ~/ widget.step * itemExtent; if (widget.infiniteLoop) { - _scrollController = - InfiniteScrollController(initialScrollOffset: initialOffset); + _scrollController = InfiniteScrollController(initialScrollOffset: initialOffset); } else { _scrollController = ScrollController(initialScrollOffset: initialOffset); } @@ -107,13 +105,14 @@ class _NumberPickerState extends State { } else { indexOfMiddleElement = indexOfMiddleElement.clamp(0, itemCount - 1); } - final intValueInTheMiddle = - _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); + final intValueInTheMiddle = _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); if (widget.value != intValueInTheMiddle) { - widget.onChanged(intValueInTheMiddle); - if (widget.haptics) { - HapticFeedback.selectionClick(); + if (widget.onChanged != null) { + widget.onChanged!(intValueInTheMiddle); + if (widget.haptics) { + HapticFeedback.selectionClick(); + } } } Future.delayed( @@ -138,8 +137,7 @@ class _NumberPickerState extends State { bool get isScrolling => _scrollController.position.isScrollingNotifier.value; - double get itemExtent => - widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; + double get itemExtent => widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; int get itemCount => (widget.maxValue - widget.minValue) ~/ widget.step + 1; @@ -150,12 +148,8 @@ class _NumberPickerState extends State { @override Widget build(BuildContext context) { return SizedBox( - width: widget.axis == Axis.vertical - ? widget.itemWidth - : widget.itemCount * widget.itemWidth, - height: widget.axis == Axis.vertical - ? widget.itemCount * widget.itemHeight - : widget.itemHeight, + width: widget.axis == Axis.vertical ? widget.itemWidth : widget.itemCount * widget.itemWidth, + height: widget.axis == Axis.vertical ? widget.itemCount * widget.itemHeight : widget.itemHeight, child: NotificationListener( onNotification: (not) { if (not.dragDetails?.primaryVelocity == 0) { @@ -167,6 +161,7 @@ class _NumberPickerState extends State { children: [ if (widget.infiniteLoop) InfiniteListView.builder( + physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : null, scrollDirection: widget.axis, controller: _scrollController as InfiniteScrollController, itemExtent: itemExtent, @@ -175,6 +170,7 @@ class _NumberPickerState extends State { ) else ListView.builder( + physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : null, itemCount: listItemsCount, scrollDirection: widget.axis, controller: _scrollController, @@ -196,13 +192,12 @@ class _NumberPickerState extends State { Widget _itemBuilder(BuildContext context, int index) { final themeData = Theme.of(context); final defaultStyle = widget.textStyle ?? themeData.textTheme.bodyText2; - final selectedStyle = widget.selectedTextStyle ?? - themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); + final selectedStyle = + widget.selectedTextStyle ?? themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); final value = _intValueFromIndex(index % itemCount); final isExtra = !widget.infiniteLoop && - (index < additionalItemsOnEachSide || - index >= listItemsCount - additionalItemsOnEachSide); + (index < additionalItemsOnEachSide || index >= listItemsCount - additionalItemsOnEachSide); final itemStyle = value == widget.value ? selectedStyle : defaultStyle; final child = isExtra @@ -221,9 +216,7 @@ class _NumberPickerState extends State { } String _getDisplayedValue(int value) { - final text = widget.zeroPad - ? value.toString().padLeft(widget.maxValue.toString().length, '0') - : value.toString(); + final text = widget.zeroPad ? value.toString().padLeft(widget.maxValue.toString().length, '0') : value.toString(); if (widget.textMapper != null) { return widget.textMapper!(text); } else { From 233c096832e3ab2a6da6e9aa6dce2904e165cec9 Mon Sep 17 00:00:00 2001 From: Matt Hilleman Date: Wed, 6 Oct 2021 12:15:03 -0500 Subject: [PATCH 2/9] Reformatted with default line lengths --- lib/src/numberpicker.dart | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/src/numberpicker.dart b/lib/src/numberpicker.dart index b747ea1..ef3a2ce 100644 --- a/lib/src/numberpicker.dart +++ b/lib/src/numberpicker.dart @@ -89,9 +89,11 @@ class _NumberPickerState extends State { @override void initState() { super.initState(); - final initialOffset = (widget.value - widget.minValue) ~/ widget.step * itemExtent; + final initialOffset = + (widget.value - widget.minValue) ~/ widget.step * itemExtent; if (widget.infiniteLoop) { - _scrollController = InfiniteScrollController(initialScrollOffset: initialOffset); + _scrollController = + InfiniteScrollController(initialScrollOffset: initialOffset); } else { _scrollController = ScrollController(initialScrollOffset: initialOffset); } @@ -105,7 +107,8 @@ class _NumberPickerState extends State { } else { indexOfMiddleElement = indexOfMiddleElement.clamp(0, itemCount - 1); } - final intValueInTheMiddle = _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); + final intValueInTheMiddle = + _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); if (widget.value != intValueInTheMiddle) { if (widget.onChanged != null) { @@ -137,7 +140,8 @@ class _NumberPickerState extends State { bool get isScrolling => _scrollController.position.isScrollingNotifier.value; - double get itemExtent => widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; + double get itemExtent => + widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; int get itemCount => (widget.maxValue - widget.minValue) ~/ widget.step + 1; @@ -148,8 +152,12 @@ class _NumberPickerState extends State { @override Widget build(BuildContext context) { return SizedBox( - width: widget.axis == Axis.vertical ? widget.itemWidth : widget.itemCount * widget.itemWidth, - height: widget.axis == Axis.vertical ? widget.itemCount * widget.itemHeight : widget.itemHeight, + width: widget.axis == Axis.vertical + ? widget.itemWidth + : widget.itemCount * widget.itemWidth, + height: widget.axis == Axis.vertical + ? widget.itemCount * widget.itemHeight + : widget.itemHeight, child: NotificationListener( onNotification: (not) { if (not.dragDetails?.primaryVelocity == 0) { @@ -161,7 +169,9 @@ class _NumberPickerState extends State { children: [ if (widget.infiniteLoop) InfiniteListView.builder( - physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : null, + physics: widget.onChanged == null + ? NeverScrollableScrollPhysics() + : null, scrollDirection: widget.axis, controller: _scrollController as InfiniteScrollController, itemExtent: itemExtent, @@ -170,7 +180,9 @@ class _NumberPickerState extends State { ) else ListView.builder( - physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : null, + physics: widget.onChanged == null + ? NeverScrollableScrollPhysics() + : null, itemCount: listItemsCount, scrollDirection: widget.axis, controller: _scrollController, @@ -192,12 +204,13 @@ class _NumberPickerState extends State { Widget _itemBuilder(BuildContext context, int index) { final themeData = Theme.of(context); final defaultStyle = widget.textStyle ?? themeData.textTheme.bodyText2; - final selectedStyle = - widget.selectedTextStyle ?? themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); + final selectedStyle = widget.selectedTextStyle ?? + themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); final value = _intValueFromIndex(index % itemCount); final isExtra = !widget.infiniteLoop && - (index < additionalItemsOnEachSide || index >= listItemsCount - additionalItemsOnEachSide); + (index < additionalItemsOnEachSide || + index >= listItemsCount - additionalItemsOnEachSide); final itemStyle = value == widget.value ? selectedStyle : defaultStyle; final child = isExtra @@ -216,7 +229,9 @@ class _NumberPickerState extends State { } String _getDisplayedValue(int value) { - final text = widget.zeroPad ? value.toString().padLeft(widget.maxValue.toString().length, '0') : value.toString(); + final text = widget.zeroPad + ? value.toString().padLeft(widget.maxValue.toString().length, '0') + : value.toString(); if (widget.textMapper != null) { return widget.textMapper!(text); } else { From 8842c289584f1afc92de909f8a49eb6c5e87eb43 Mon Sep 17 00:00:00 2001 From: MattyBoy Date: Sun, 3 Jul 2022 13:31:40 -0600 Subject: [PATCH 3/9] Fix: Failed assertion notification.metrics.axis == widget.axis --- lib/src/numberpicker.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/numberpicker.dart b/lib/src/numberpicker.dart index ef3a2ce..2163d80 100644 --- a/lib/src/numberpicker.dart +++ b/lib/src/numberpicker.dart @@ -171,7 +171,7 @@ class _NumberPickerState extends State { InfiniteListView.builder( physics: widget.onChanged == null ? NeverScrollableScrollPhysics() - : null, + : BouncingScrollPhysics(), scrollDirection: widget.axis, controller: _scrollController as InfiniteScrollController, itemExtent: itemExtent, @@ -182,7 +182,7 @@ class _NumberPickerState extends State { ListView.builder( physics: widget.onChanged == null ? NeverScrollableScrollPhysics() - : null, + : BouncingScrollPhysics(), itemCount: listItemsCount, scrollDirection: widget.axis, controller: _scrollController, From e5115439a060d806bb6abac24ec6cd87a0290808 Mon Sep 17 00:00:00 2001 From: MattyBoy Date: Sat, 20 Aug 2022 08:59:28 -0500 Subject: [PATCH 4/9] Fixed: Scrolling with mouse not working --- lib/src/numberpicker.dart | 108 ++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/lib/src/numberpicker.dart b/lib/src/numberpicker.dart index 2163d80..f8ec02f 100644 --- a/lib/src/numberpicker.dart +++ b/lib/src/numberpicker.dart @@ -1,4 +1,5 @@ import 'package:flutter/foundation.dart'; +import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:infinite_listview/infinite_listview.dart'; @@ -89,11 +90,9 @@ class _NumberPickerState extends State { @override void initState() { super.initState(); - final initialOffset = - (widget.value - widget.minValue) ~/ widget.step * itemExtent; + final initialOffset = (widget.value - widget.minValue) ~/ widget.step * itemExtent; if (widget.infiniteLoop) { - _scrollController = - InfiniteScrollController(initialScrollOffset: initialOffset); + _scrollController = InfiniteScrollController(initialScrollOffset: initialOffset); } else { _scrollController = ScrollController(initialScrollOffset: initialOffset); } @@ -107,8 +106,7 @@ class _NumberPickerState extends State { } else { indexOfMiddleElement = indexOfMiddleElement.clamp(0, itemCount - 1); } - final intValueInTheMiddle = - _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); + final intValueInTheMiddle = _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); if (widget.value != intValueInTheMiddle) { if (widget.onChanged != null) { @@ -140,8 +138,7 @@ class _NumberPickerState extends State { bool get isScrolling => _scrollController.position.isScrollingNotifier.value; - double get itemExtent => - widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; + double get itemExtent => widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; int get itemCount => (widget.maxValue - widget.minValue) ~/ widget.step + 1; @@ -151,51 +148,49 @@ class _NumberPickerState extends State { @override Widget build(BuildContext context) { - return SizedBox( - width: widget.axis == Axis.vertical - ? widget.itemWidth - : widget.itemCount * widget.itemWidth, - height: widget.axis == Axis.vertical - ? widget.itemCount * widget.itemHeight - : widget.itemHeight, - child: NotificationListener( - onNotification: (not) { - if (not.dragDetails?.primaryVelocity == 0) { - Future.microtask(() => _maybeCenterValue()); - } - return true; - }, - child: Stack( - children: [ - if (widget.infiniteLoop) - InfiniteListView.builder( - physics: widget.onChanged == null - ? NeverScrollableScrollPhysics() - : BouncingScrollPhysics(), - scrollDirection: widget.axis, - controller: _scrollController as InfiniteScrollController, + return ScrollConfiguration( + behavior: ScrollConfiguration.of(context).copyWith(dragDevices: { + PointerDeviceKind.touch, + PointerDeviceKind.mouse, + }), + child: SizedBox( + width: widget.axis == Axis.vertical ? widget.itemWidth : widget.itemCount * widget.itemWidth, + height: widget.axis == Axis.vertical ? widget.itemCount * widget.itemHeight : widget.itemHeight, + child: NotificationListener( + onNotification: (not) { + if (not.dragDetails?.primaryVelocity == 0) { + Future.microtask(() => _maybeCenterValue()); + } + return true; + }, + child: Stack( + children: [ + if (widget.infiniteLoop) + InfiniteListView.builder( + physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(), + scrollDirection: widget.axis, + controller: _scrollController as InfiniteScrollController, + itemExtent: itemExtent, + itemBuilder: _itemBuilder, + padding: EdgeInsets.zero, + ) + else + ListView.builder( + physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(), + itemCount: listItemsCount, + scrollDirection: widget.axis, + controller: _scrollController, + itemExtent: itemExtent, + itemBuilder: _itemBuilder, + padding: EdgeInsets.zero, + ), + _NumberPickerSelectedItemDecoration( + axis: widget.axis, itemExtent: itemExtent, - itemBuilder: _itemBuilder, - padding: EdgeInsets.zero, - ) - else - ListView.builder( - physics: widget.onChanged == null - ? NeverScrollableScrollPhysics() - : BouncingScrollPhysics(), - itemCount: listItemsCount, - scrollDirection: widget.axis, - controller: _scrollController, - itemExtent: itemExtent, - itemBuilder: _itemBuilder, - padding: EdgeInsets.zero, + decoration: widget.decoration, ), - _NumberPickerSelectedItemDecoration( - axis: widget.axis, - itemExtent: itemExtent, - decoration: widget.decoration, - ), - ], + ], + ), ), ), ); @@ -204,13 +199,12 @@ class _NumberPickerState extends State { Widget _itemBuilder(BuildContext context, int index) { final themeData = Theme.of(context); final defaultStyle = widget.textStyle ?? themeData.textTheme.bodyText2; - final selectedStyle = widget.selectedTextStyle ?? - themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); + final selectedStyle = + widget.selectedTextStyle ?? themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); final value = _intValueFromIndex(index % itemCount); final isExtra = !widget.infiniteLoop && - (index < additionalItemsOnEachSide || - index >= listItemsCount - additionalItemsOnEachSide); + (index < additionalItemsOnEachSide || index >= listItemsCount - additionalItemsOnEachSide); final itemStyle = value == widget.value ? selectedStyle : defaultStyle; final child = isExtra @@ -229,9 +223,7 @@ class _NumberPickerState extends State { } String _getDisplayedValue(int value) { - final text = widget.zeroPad - ? value.toString().padLeft(widget.maxValue.toString().length, '0') - : value.toString(); + final text = widget.zeroPad ? value.toString().padLeft(widget.maxValue.toString().length, '0') : value.toString(); if (widget.textMapper != null) { return widget.textMapper!(text); } else { From 2ce1d6d56293aeb903520dd4ce5ad15fb66fc55f Mon Sep 17 00:00:00 2001 From: MattyBoy Date: Sat, 20 Aug 2022 09:15:55 -0500 Subject: [PATCH 5/9] Revert "Fixed: Scrolling with mouse not working" This reverts commit e5115439a060d806bb6abac24ec6cd87a0290808. --- lib/src/numberpicker.dart | 108 ++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 50 deletions(-) diff --git a/lib/src/numberpicker.dart b/lib/src/numberpicker.dart index f8ec02f..2163d80 100644 --- a/lib/src/numberpicker.dart +++ b/lib/src/numberpicker.dart @@ -1,5 +1,4 @@ import 'package:flutter/foundation.dart'; -import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:infinite_listview/infinite_listview.dart'; @@ -90,9 +89,11 @@ class _NumberPickerState extends State { @override void initState() { super.initState(); - final initialOffset = (widget.value - widget.minValue) ~/ widget.step * itemExtent; + final initialOffset = + (widget.value - widget.minValue) ~/ widget.step * itemExtent; if (widget.infiniteLoop) { - _scrollController = InfiniteScrollController(initialScrollOffset: initialOffset); + _scrollController = + InfiniteScrollController(initialScrollOffset: initialOffset); } else { _scrollController = ScrollController(initialScrollOffset: initialOffset); } @@ -106,7 +107,8 @@ class _NumberPickerState extends State { } else { indexOfMiddleElement = indexOfMiddleElement.clamp(0, itemCount - 1); } - final intValueInTheMiddle = _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); + final intValueInTheMiddle = + _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); if (widget.value != intValueInTheMiddle) { if (widget.onChanged != null) { @@ -138,7 +140,8 @@ class _NumberPickerState extends State { bool get isScrolling => _scrollController.position.isScrollingNotifier.value; - double get itemExtent => widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; + double get itemExtent => + widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; int get itemCount => (widget.maxValue - widget.minValue) ~/ widget.step + 1; @@ -148,49 +151,51 @@ class _NumberPickerState extends State { @override Widget build(BuildContext context) { - return ScrollConfiguration( - behavior: ScrollConfiguration.of(context).copyWith(dragDevices: { - PointerDeviceKind.touch, - PointerDeviceKind.mouse, - }), - child: SizedBox( - width: widget.axis == Axis.vertical ? widget.itemWidth : widget.itemCount * widget.itemWidth, - height: widget.axis == Axis.vertical ? widget.itemCount * widget.itemHeight : widget.itemHeight, - child: NotificationListener( - onNotification: (not) { - if (not.dragDetails?.primaryVelocity == 0) { - Future.microtask(() => _maybeCenterValue()); - } - return true; - }, - child: Stack( - children: [ - if (widget.infiniteLoop) - InfiniteListView.builder( - physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(), - scrollDirection: widget.axis, - controller: _scrollController as InfiniteScrollController, - itemExtent: itemExtent, - itemBuilder: _itemBuilder, - padding: EdgeInsets.zero, - ) - else - ListView.builder( - physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(), - itemCount: listItemsCount, - scrollDirection: widget.axis, - controller: _scrollController, - itemExtent: itemExtent, - itemBuilder: _itemBuilder, - padding: EdgeInsets.zero, - ), - _NumberPickerSelectedItemDecoration( - axis: widget.axis, + return SizedBox( + width: widget.axis == Axis.vertical + ? widget.itemWidth + : widget.itemCount * widget.itemWidth, + height: widget.axis == Axis.vertical + ? widget.itemCount * widget.itemHeight + : widget.itemHeight, + child: NotificationListener( + onNotification: (not) { + if (not.dragDetails?.primaryVelocity == 0) { + Future.microtask(() => _maybeCenterValue()); + } + return true; + }, + child: Stack( + children: [ + if (widget.infiniteLoop) + InfiniteListView.builder( + physics: widget.onChanged == null + ? NeverScrollableScrollPhysics() + : BouncingScrollPhysics(), + scrollDirection: widget.axis, + controller: _scrollController as InfiniteScrollController, itemExtent: itemExtent, - decoration: widget.decoration, + itemBuilder: _itemBuilder, + padding: EdgeInsets.zero, + ) + else + ListView.builder( + physics: widget.onChanged == null + ? NeverScrollableScrollPhysics() + : BouncingScrollPhysics(), + itemCount: listItemsCount, + scrollDirection: widget.axis, + controller: _scrollController, + itemExtent: itemExtent, + itemBuilder: _itemBuilder, + padding: EdgeInsets.zero, ), - ], - ), + _NumberPickerSelectedItemDecoration( + axis: widget.axis, + itemExtent: itemExtent, + decoration: widget.decoration, + ), + ], ), ), ); @@ -199,12 +204,13 @@ class _NumberPickerState extends State { Widget _itemBuilder(BuildContext context, int index) { final themeData = Theme.of(context); final defaultStyle = widget.textStyle ?? themeData.textTheme.bodyText2; - final selectedStyle = - widget.selectedTextStyle ?? themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); + final selectedStyle = widget.selectedTextStyle ?? + themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); final value = _intValueFromIndex(index % itemCount); final isExtra = !widget.infiniteLoop && - (index < additionalItemsOnEachSide || index >= listItemsCount - additionalItemsOnEachSide); + (index < additionalItemsOnEachSide || + index >= listItemsCount - additionalItemsOnEachSide); final itemStyle = value == widget.value ? selectedStyle : defaultStyle; final child = isExtra @@ -223,7 +229,9 @@ class _NumberPickerState extends State { } String _getDisplayedValue(int value) { - final text = widget.zeroPad ? value.toString().padLeft(widget.maxValue.toString().length, '0') : value.toString(); + final text = widget.zeroPad + ? value.toString().padLeft(widget.maxValue.toString().length, '0') + : value.toString(); if (widget.textMapper != null) { return widget.textMapper!(text); } else { From 9889d6def176f6fbfe85046d7cdbfcfbde4dafe3 Mon Sep 17 00:00:00 2001 From: MattyBoy Date: Thu, 11 May 2023 12:33:31 -0500 Subject: [PATCH 6/9] Fixed: accentcolor was removed from flutter --- lib/src/numberpicker.dart | 38 +++++++++++--------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/lib/src/numberpicker.dart b/lib/src/numberpicker.dart index 2163d80..0d4af6e 100644 --- a/lib/src/numberpicker.dart +++ b/lib/src/numberpicker.dart @@ -89,11 +89,9 @@ class _NumberPickerState extends State { @override void initState() { super.initState(); - final initialOffset = - (widget.value - widget.minValue) ~/ widget.step * itemExtent; + final initialOffset = (widget.value - widget.minValue) ~/ widget.step * itemExtent; if (widget.infiniteLoop) { - _scrollController = - InfiniteScrollController(initialScrollOffset: initialOffset); + _scrollController = InfiniteScrollController(initialScrollOffset: initialOffset); } else { _scrollController = ScrollController(initialScrollOffset: initialOffset); } @@ -107,8 +105,7 @@ class _NumberPickerState extends State { } else { indexOfMiddleElement = indexOfMiddleElement.clamp(0, itemCount - 1); } - final intValueInTheMiddle = - _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); + final intValueInTheMiddle = _intValueFromIndex(indexOfMiddleElement + additionalItemsOnEachSide); if (widget.value != intValueInTheMiddle) { if (widget.onChanged != null) { @@ -140,8 +137,7 @@ class _NumberPickerState extends State { bool get isScrolling => _scrollController.position.isScrollingNotifier.value; - double get itemExtent => - widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; + double get itemExtent => widget.axis == Axis.vertical ? widget.itemHeight : widget.itemWidth; int get itemCount => (widget.maxValue - widget.minValue) ~/ widget.step + 1; @@ -152,12 +148,8 @@ class _NumberPickerState extends State { @override Widget build(BuildContext context) { return SizedBox( - width: widget.axis == Axis.vertical - ? widget.itemWidth - : widget.itemCount * widget.itemWidth, - height: widget.axis == Axis.vertical - ? widget.itemCount * widget.itemHeight - : widget.itemHeight, + width: widget.axis == Axis.vertical ? widget.itemWidth : widget.itemCount * widget.itemWidth, + height: widget.axis == Axis.vertical ? widget.itemCount * widget.itemHeight : widget.itemHeight, child: NotificationListener( onNotification: (not) { if (not.dragDetails?.primaryVelocity == 0) { @@ -169,9 +161,7 @@ class _NumberPickerState extends State { children: [ if (widget.infiniteLoop) InfiniteListView.builder( - physics: widget.onChanged == null - ? NeverScrollableScrollPhysics() - : BouncingScrollPhysics(), + physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(), scrollDirection: widget.axis, controller: _scrollController as InfiniteScrollController, itemExtent: itemExtent, @@ -180,9 +170,7 @@ class _NumberPickerState extends State { ) else ListView.builder( - physics: widget.onChanged == null - ? NeverScrollableScrollPhysics() - : BouncingScrollPhysics(), + physics: widget.onChanged == null ? NeverScrollableScrollPhysics() : BouncingScrollPhysics(), itemCount: listItemsCount, scrollDirection: widget.axis, controller: _scrollController, @@ -204,13 +192,11 @@ class _NumberPickerState extends State { Widget _itemBuilder(BuildContext context, int index) { final themeData = Theme.of(context); final defaultStyle = widget.textStyle ?? themeData.textTheme.bodyText2; - final selectedStyle = widget.selectedTextStyle ?? - themeData.textTheme.headline5?.copyWith(color: themeData.accentColor); + final selectedStyle = widget.selectedTextStyle ?? themeData.textTheme.headline5?.copyWith(); final value = _intValueFromIndex(index % itemCount); final isExtra = !widget.infiniteLoop && - (index < additionalItemsOnEachSide || - index >= listItemsCount - additionalItemsOnEachSide); + (index < additionalItemsOnEachSide || index >= listItemsCount - additionalItemsOnEachSide); final itemStyle = value == widget.value ? selectedStyle : defaultStyle; final child = isExtra @@ -229,9 +215,7 @@ class _NumberPickerState extends State { } String _getDisplayedValue(int value) { - final text = widget.zeroPad - ? value.toString().padLeft(widget.maxValue.toString().length, '0') - : value.toString(); + final text = widget.zeroPad ? value.toString().padLeft(widget.maxValue.toString().length, '0') : value.toString(); if (widget.textMapper != null) { return widget.textMapper!(text); } else { From b05eacb7eb427f1ac0d23f362eb10db2a5f660e3 Mon Sep 17 00:00:00 2001 From: MattyBoy Date: Sun, 14 May 2023 21:39:35 -0500 Subject: [PATCH 7/9] Updated sdk version and updated deprecated fields --- example/lib/main.dart | 9 ++++----- lib/src/numberpicker.dart | 6 +++--- pubspec.yaml | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 7cc7a72..aab1b7e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -63,7 +63,7 @@ class __IntegerExampleState extends State<_IntegerExample> { return Column( children: [ SizedBox(height: 16), - Text('Default', style: Theme.of(context).textTheme.headline6), + Text('Default', style: Theme.of(context).textTheme.titleLarge), NumberPicker( value: _currentIntValue, minValue: 0, @@ -95,7 +95,7 @@ class __IntegerExampleState extends State<_IntegerExample> { ), Divider(color: Colors.grey, height: 32), SizedBox(height: 16), - Text('Horizontal', style: Theme.of(context).textTheme.headline6), + Text('Horizontal', style: Theme.of(context).textTheme.titleLarge), NumberPicker( value: _currentHorizontalIntValue, minValue: 0, @@ -103,8 +103,7 @@ class __IntegerExampleState extends State<_IntegerExample> { step: 10, itemHeight: 100, axis: Axis.horizontal, - onChanged: (value) => - setState(() => _currentHorizontalIntValue = value), + onChanged: (value) => setState(() => _currentHorizontalIntValue = value), decoration: BoxDecoration( borderRadius: BorderRadius.circular(16), border: Border.all(color: Colors.black26), @@ -148,7 +147,7 @@ class __DecimalExampleState extends State<_DecimalExample> { return Column( children: [ SizedBox(height: 16), - Text('Decimal', style: Theme.of(context).textTheme.headline6), + Text('Decimal', style: Theme.of(context).textTheme.titleLarge), DecimalNumberPicker( value: _currentDoubleValue, minValue: 0, diff --git a/lib/src/numberpicker.dart b/lib/src/numberpicker.dart index 0d4af6e..8e42441 100644 --- a/lib/src/numberpicker.dart +++ b/lib/src/numberpicker.dart @@ -1,4 +1,3 @@ -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:infinite_listview/infinite_listview.dart'; @@ -191,8 +190,9 @@ class _NumberPickerState extends State { Widget _itemBuilder(BuildContext context, int index) { final themeData = Theme.of(context); - final defaultStyle = widget.textStyle ?? themeData.textTheme.bodyText2; - final selectedStyle = widget.selectedTextStyle ?? themeData.textTheme.headline5?.copyWith(); + final defaultStyle = widget.textStyle ?? themeData.textTheme.bodyMedium; + final selectedStyle = + widget.selectedTextStyle ?? themeData.textTheme.headlineSmall?.copyWith(color: themeData.colorScheme.secondary); final value = _intValueFromIndex(index % itemCount); final isExtra = !widget.infiniteLoop && diff --git a/pubspec.yaml b/pubspec.yaml index 8c0dfe9..02f66f2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ description: NumberPicker is a widget allowing user to choose numbers by scrolli homepage: https://github.com/MarcinusX/NumberPicker environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=2.12.0 <4.0.0" dependencies: flutter: From 8858f8b9730ed42aa1424faf468aeea809e93ee5 Mon Sep 17 00:00:00 2001 From: MattyBoy Date: Sun, 14 May 2023 21:42:14 -0500 Subject: [PATCH 8/9] Updated version --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 02f66f2..1d43988 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: numberpicker -version: 2.1.1 +version: 2.1.2 description: NumberPicker is a widget allowing user to choose numbers by scrolling spinners. homepage: https://github.com/MarcinusX/NumberPicker From 11b7beaca445623effadf6b494254fadd5e6acd5 Mon Sep 17 00:00:00 2001 From: MattyBoy Date: Sun, 18 Feb 2024 13:42:03 -0600 Subject: [PATCH 9/9] Fixed: When value was max and width was a particular value, 50, it would bounce back to one less than max. --- lib/src/numberpicker.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/src/numberpicker.dart b/lib/src/numberpicker.dart index 8e42441..ed1f65a 100644 --- a/lib/src/numberpicker.dart +++ b/lib/src/numberpicker.dart @@ -114,10 +114,12 @@ class _NumberPickerState extends State { } } } - Future.delayed( - Duration(milliseconds: 100), - () => _maybeCenterValue(), - ); + if (!isScrolling) { + Future.delayed( + Duration(milliseconds: 100), + () => _maybeCenterValue(), + ); + } } @override