From 2cdc709618eea253784cb99c40bbecb7bf857b97 Mon Sep 17 00:00:00 2001 From: alirn76 Date: Sun, 5 Apr 2020 05:04:06 +0430 Subject: [PATCH 1/8] add color for selected value as selectedValueColor --- lib/numberpicker.dart | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index 14cfe3d..5c0f938 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -34,6 +34,7 @@ class NumberPicker extends StatelessWidget { this.zeroPad = false, this.highlightSelectedValue = true, this.decoration, + this.selectedValueColor, }) : assert(initialValue != null), assert(minValue != null), assert(maxValue != null), @@ -69,6 +70,7 @@ class NumberPicker extends StatelessWidget { this.zeroPad = false, this.highlightSelectedValue = true, this.decoration, + this.selectedValueColor, }) : assert(initialValue != null), assert(minValue != null), assert(maxValue != null), @@ -106,6 +108,7 @@ class NumberPicker extends StatelessWidget { this.listViewWidth = kDefaultListViewCrossAxisSize, this.highlightSelectedValue = true, this.decoration, + this.selectedValueColor, }) : assert(initialValue != null), assert(minValue != null), assert(maxValue != null), @@ -176,6 +179,9 @@ class NumberPicker extends StatelessWidget { ///Decoration to apply to central box where the selected value is placed final Decoration decoration; + ///Color Of Selected Number + final Color selectedValueColor; + ///Step between elements. Only for integer datePicker ///Examples: /// if step is 100 the following elements may be 100, 200, 300... @@ -252,7 +258,7 @@ class NumberPicker extends StatelessWidget { Widget _integerListView(ThemeData themeData) { TextStyle defaultStyle = themeData.textTheme.body1; TextStyle selectedStyle = - themeData.textTheme.headline.copyWith(color: themeData.accentColor); + themeData.textTheme.headline.copyWith(color: selectedValueColor); var listItemCount = integerItemCount + 2; @@ -312,7 +318,7 @@ class NumberPicker extends StatelessWidget { Widget _decimalListView(ThemeData themeData) { TextStyle defaultStyle = themeData.textTheme.body1; TextStyle selectedStyle = - themeData.textTheme.headline.copyWith(color: themeData.accentColor); + themeData.textTheme.headline.copyWith(color: selectedValueColor); int decimalItemCount = selectedIntValue == maxValue ? 3 : math.pow(10, decimalPlaces) + 2; @@ -370,7 +376,7 @@ class NumberPicker extends StatelessWidget { Widget _integerInfiniteListView(ThemeData themeData) { TextStyle defaultStyle = themeData.textTheme.body1; TextStyle selectedStyle = - themeData.textTheme.headline.copyWith(color: themeData.accentColor); + themeData.textTheme.headline.copyWith(color: selectedValueColor); return Listener( onPointerUp: (ev) { From a0479080878dccad57347870aebc41b61fb24fc4 Mon Sep 17 00:00:00 2001 From: AliRn76 Date: Tue, 28 Jul 2020 20:36:34 +0430 Subject: [PATCH 2/8] Set the textStyle and selectedTextStyle for default numbers and selected numbers --- lib/numberpicker.dart | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index 5c0f938..661087e 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -34,7 +34,8 @@ class NumberPicker extends StatelessWidget { this.zeroPad = false, this.highlightSelectedValue = true, this.decoration, - this.selectedValueColor, + this.textStyle, + this.selectedTextStyle, }) : assert(initialValue != null), assert(minValue != null), assert(maxValue != null), @@ -70,7 +71,8 @@ class NumberPicker extends StatelessWidget { this.zeroPad = false, this.highlightSelectedValue = true, this.decoration, - this.selectedValueColor, + this.textStyle, + this.selectedTextStyle, }) : assert(initialValue != null), assert(minValue != null), assert(maxValue != null), @@ -108,7 +110,8 @@ class NumberPicker extends StatelessWidget { this.listViewWidth = kDefaultListViewCrossAxisSize, this.highlightSelectedValue = true, this.decoration, - this.selectedValueColor, + this.textStyle, + this.selectedTextStyle, }) : assert(initialValue != null), assert(minValue != null), assert(maxValue != null), @@ -179,8 +182,11 @@ class NumberPicker extends StatelessWidget { ///Decoration to apply to central box where the selected value is placed final Decoration decoration; - ///Color Of Selected Number - final Color selectedValueColor; + ///Default Style Of Numbers + final TextStyle textStyle; + + ///Style Of Selected Number + final TextStyle selectedTextStyle; ///Step between elements. Only for integer datePicker ///Examples: @@ -256,10 +262,6 @@ class NumberPicker extends StatelessWidget { } Widget _integerListView(ThemeData themeData) { - TextStyle defaultStyle = themeData.textTheme.body1; - TextStyle selectedStyle = - themeData.textTheme.headline.copyWith(color: selectedValueColor); - var listItemCount = integerItemCount + 2; return Listener( @@ -287,8 +289,8 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = value == selectedIntValue && highlightSelectedValue - ? selectedStyle - : defaultStyle; + ? selectedTextStyle + : textStyle; bool isExtra = index == 0 || index == listItemCount - 1; @@ -316,10 +318,6 @@ class NumberPicker extends StatelessWidget { } Widget _decimalListView(ThemeData themeData) { - TextStyle defaultStyle = themeData.textTheme.body1; - TextStyle selectedStyle = - themeData.textTheme.headline.copyWith(color: selectedValueColor); - int decimalItemCount = selectedIntValue == maxValue ? 3 : math.pow(10, decimalPlaces) + 2; @@ -346,8 +344,8 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = value == selectedDecimalValue && highlightSelectedValue - ? selectedStyle - : defaultStyle; + ? selectedTextStyle + : textStyle; bool isExtra = index == 0 || index == decimalItemCount - 1; @@ -374,10 +372,6 @@ class NumberPicker extends StatelessWidget { } Widget _integerInfiniteListView(ThemeData themeData) { - TextStyle defaultStyle = themeData.textTheme.body1; - TextStyle selectedStyle = - themeData.textTheme.headline.copyWith(color: selectedValueColor); - return Listener( onPointerUp: (ev) { ///used to detect that user stopped scrolling @@ -400,8 +394,8 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = value == selectedIntValue && highlightSelectedValue - ? selectedStyle - : defaultStyle; + ? selectedTextStyle + : textStyle; return new Center( child: new Text( @@ -728,3 +722,4 @@ class _NumberPickerDialogControllerState extends State { ); } } + From b1a15da88da3e38d0b279855bf8660e9164e8060 Mon Sep 17 00:00:00 2001 From: AliRn76 Date: Tue, 28 Jul 2020 20:45:18 +0430 Subject: [PATCH 3/8] made it ready for merge :) --- lib/numberpicker.dart | 204 ++++++++++++++++++++++++------------------ 1 file changed, 116 insertions(+), 88 deletions(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index 661087e..08233d6 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -3,6 +3,7 @@ import 'dart:math' as math; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; import 'package:infinite_listview/infinite_listview.dart'; /// Created by Marcin SzaƂek @@ -34,6 +35,7 @@ class NumberPicker extends StatelessWidget { this.zeroPad = false, this.highlightSelectedValue = true, this.decoration, + this.haptics = false, this.textStyle, this.selectedTextStyle, }) : assert(initialValue != null), @@ -45,7 +47,7 @@ class NumberPicker extends StatelessWidget { selectedIntValue = initialValue, selectedDecimalValue = -1, decimalPlaces = 0, - intScrollController = new ScrollController( + intScrollController = ScrollController( initialScrollOffset: (initialValue - minValue) ~/ step * itemExtent, ), scrollDirection = Axis.horizontal, @@ -71,6 +73,7 @@ class NumberPicker extends StatelessWidget { this.zeroPad = false, this.highlightSelectedValue = true, this.decoration, + this.haptics = false, this.textStyle, this.selectedTextStyle, }) : assert(initialValue != null), @@ -84,14 +87,14 @@ class NumberPicker extends StatelessWidget { selectedDecimalValue = -1, decimalPlaces = 0, intScrollController = infiniteLoop - ? new InfiniteScrollController( - initialScrollOffset: - (initialValue - minValue) ~/ step * itemExtent, - ) - : new ScrollController( - initialScrollOffset: - (initialValue - minValue) ~/ step * itemExtent, - ), + ? InfiniteScrollController( + initialScrollOffset: + (initialValue - minValue) ~/ step * itemExtent, + ) + : ScrollController( + initialScrollOffset: + (initialValue - minValue) ~/ step * itemExtent, + ), decimalScrollController = null, listViewHeight = 3 * itemExtent, integerItemCount = (maxValue - minValue) ~/ step + 1, @@ -110,6 +113,7 @@ class NumberPicker extends StatelessWidget { this.listViewWidth = kDefaultListViewCrossAxisSize, this.highlightSelectedValue = true, this.decoration, + this.haptics = false, this.textStyle, this.selectedTextStyle, }) : assert(initialValue != null), @@ -120,15 +124,15 @@ class NumberPicker extends StatelessWidget { assert(initialValue >= minValue && initialValue <= maxValue), selectedIntValue = initialValue.floor(), selectedDecimalValue = ((initialValue - initialValue.floorToDouble()) * - math.pow(10, decimalPlaces)) + math.pow(10, decimalPlaces)) .round(), - intScrollController = new ScrollController( + intScrollController = ScrollController( initialScrollOffset: (initialValue.floor() - minValue) * itemExtent, ), - decimalScrollController = new ScrollController( + decimalScrollController = ScrollController( initialScrollOffset: ((initialValue - initialValue.floorToDouble()) * - math.pow(10, decimalPlaces)) - .roundToDouble() * + math.pow(10, decimalPlaces)) + .roundToDouble() * itemExtent, ), listViewHeight = 3 * itemExtent, @@ -147,7 +151,7 @@ class NumberPicker extends StatelessWidget { ///max value user can pick final int maxValue; - + ///build the text of each item on the picker final TextMapper textMapper; @@ -182,12 +186,6 @@ class NumberPicker extends StatelessWidget { ///Decoration to apply to central box where the selected value is placed final Decoration decoration; - ///Default Style Of Numbers - final TextStyle textStyle; - - ///Style Of Selected Number - final TextStyle selectedTextStyle; - ///Step between elements. Only for integer datePicker ///Examples: /// if step is 100 the following elements may be 100, 200, 300... @@ -207,6 +205,15 @@ class NumberPicker extends StatelessWidget { ///Amount of items final int integerItemCount; + ///Whether to trigger haptic pulses or not + final bool haptics; + + ///Default Style Of Numbers + final TextStyle textStyle; + + ///Style Of Selected Number + final TextStyle selectedTextStyle; + // //----------------------------- PUBLIC ------------------------------ // @@ -232,7 +239,7 @@ class NumberPicker extends StatelessWidget { void animateDecimalAndInteger(double valueToSelect) { animateInt(valueToSelect.floor()); animateDecimal(((valueToSelect - valueToSelect.floorToDouble()) * - math.pow(10, decimalPlaces)) + math.pow(10, decimalPlaces)) .round()); } @@ -251,7 +258,7 @@ class NumberPicker extends StatelessWidget { if (decimalPlaces == 0) { return _integerListView(themeData); } else { - return new Row( + return Row( children: [ _integerListView(themeData), _decimalListView(themeData), @@ -271,13 +278,13 @@ class NumberPicker extends StatelessWidget { animateInt(selectedIntValue); } }, - child: new NotificationListener( - child: new Container( + child: NotificationListener( + child: Container( height: listViewHeight, width: listViewWidth, child: Stack( children: [ - new ListView.builder( + ListView.builder( scrollDirection: scrollDirection, controller: intScrollController, itemExtent: itemExtent, @@ -288,20 +295,20 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = - value == selectedIntValue && highlightSelectedValue - ? selectedTextStyle - : textStyle; + value == selectedIntValue && highlightSelectedValue + ? selectedTextStyle + : textStyle; bool isExtra = index == 0 || index == listItemCount - 1; return isExtra - ? new Container() //empty first and last element - : new Center( - child: new Text( - getDisplayedValue(value), - style: itemStyle, - ), - ); + ? Container() //empty first and last element + : Center( + child: Text( + getDisplayedValue(value), + style: itemStyle, + ), + ); }, ), _NumberPickerSelectedItemDecoration( @@ -319,7 +326,7 @@ class NumberPicker extends StatelessWidget { Widget _decimalListView(ThemeData themeData) { int decimalItemCount = - selectedIntValue == maxValue ? 3 : math.pow(10, decimalPlaces) + 2; + selectedIntValue == maxValue ? 3 : math.pow(10, decimalPlaces) + 2; return Listener( onPointerUp: (ev) { @@ -328,13 +335,13 @@ class NumberPicker extends StatelessWidget { animateDecimal(selectedDecimalValue); } }, - child: new NotificationListener( - child: new Container( + child: NotificationListener( + child: Container( height: listViewHeight, width: listViewWidth, child: Stack( children: [ - new ListView.builder( + ListView.builder( controller: decimalScrollController, itemExtent: itemExtent, itemCount: decimalItemCount, @@ -343,19 +350,20 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = - value == selectedDecimalValue && highlightSelectedValue - ? selectedTextStyle - : textStyle; + value == selectedDecimalValue && highlightSelectedValue + ? selectedTextStyle + : textStyle; bool isExtra = index == 0 || index == decimalItemCount - 1; return isExtra - ? new Container() //empty first and last element - : new Center( - child: new Text( - value.toString().padLeft(decimalPlaces, '0'), - style: itemStyle), - ); + ? Container() //empty first and last element + : Center( + child: Text( + value.toString().padLeft(decimalPlaces, '0'), + style: itemStyle, + ), + ); }, ), _NumberPickerSelectedItemDecoration( @@ -379,8 +387,8 @@ class NumberPicker extends StatelessWidget { _animateIntWhenUserStoppedScrolling(selectedIntValue); } }, - child: new NotificationListener( - child: new Container( + child: NotificationListener( + child: Container( height: listViewHeight, width: listViewWidth, child: Stack( @@ -393,12 +401,12 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = - value == selectedIntValue && highlightSelectedValue - ? selectedTextStyle - : textStyle; + value == selectedIntValue && highlightSelectedValue + ? selectedTextStyle + : textStyle; - return new Center( - child: new Text( + return Center( + child: Text( getDisplayedValue(value), style: itemStyle, ), @@ -439,7 +447,7 @@ class NumberPicker extends StatelessWidget { if (notification is ScrollNotification) { //calculate int intIndexOfMiddleElement = - (notification.metrics.pixels / itemExtent).round(); + (notification.metrics.pixels / itemExtent).round(); if (!infiniteLoop) { intIndexOfMiddleElement = intIndexOfMiddleElement.clamp(0, integerItemCount - 1); @@ -469,6 +477,9 @@ class NumberPicker extends StatelessWidget { newValue = ((intValueInTheMiddle + decimalPart).toDouble()); } } + if (haptics) { + HapticFeedback.selectionClick(); + } onChanged(newValue); } } @@ -494,6 +505,9 @@ class NumberPicker extends StatelessWidget { decimalValueInTheMiddle != selectedDecimalValue) { double decimalPart = _toDecimal(decimalValueInTheMiddle); double newValue = ((selectedIntValue + decimalPart).toDouble()); + if (haptics) { + HapticFeedback.selectionClick(); + } onChanged(newValue); } } @@ -531,9 +545,9 @@ class NumberPicker extends StatelessWidget { ///indicates if user has stopped scrolling so we can center value in the middle bool _userStoppedScrolling( - Notification notification, - ScrollController scrollController, - ) { + Notification notification, + ScrollController scrollController, + ) { return notification is UserScrollNotification && notification.direction == ScrollDirection.idle && scrollController.position.activity is! HoldScrollActivity; @@ -543,7 +557,8 @@ class NumberPicker extends StatelessWidget { /// Use it only when user manually stops scrolling in infinite loop void _animateIntWhenUserStoppedScrolling(int valueToSelect) { // estimated index of currently selected element based on offset and item extent - int currentlySelectedElementIndex = intScrollController.offset ~/ itemExtent; + int currentlySelectedElementIndex = + intScrollController.offset ~/ itemExtent; // when more(less) than half of the top(bottom) element is hidden // then we should increment(decrement) index in case of positive(negative) offset @@ -568,8 +583,11 @@ class NumberPicker extends StatelessWidget { ///scroll to selected value _animate(ScrollController scrollController, double value) { - scrollController.animateTo(value, - duration: new Duration(seconds: 1), curve: new ElasticOutCurve()); + scrollController.animateTo( + value, + duration: Duration(seconds: 1), + curve: ElasticOutCurve(), + ); } } @@ -580,16 +598,16 @@ class _NumberPickerSelectedItemDecoration extends StatelessWidget { const _NumberPickerSelectedItemDecoration( {Key key, - @required this.axis, - @required this.itemExtent, - @required this.decoration}) + @required this.axis, + @required this.itemExtent, + @required this.decoration}) : super(key: key); @override Widget build(BuildContext context) { - return new Center( - child: new IgnorePointer( - child: new Container( + return Center( + child: IgnorePointer( + child: Container( width: isVertical ? double.infinity : itemExtent, height: isVertical ? itemExtent : double.infinity, decoration: decoration, @@ -617,6 +635,8 @@ class NumberPickerDialog extends StatefulWidget { final bool zeroPad; final bool highlightSelectedValue; final Decoration decoration; + final TextMapper textMapper; + final bool haptics; ///constructor for integer values NumberPickerDialog.integer({ @@ -630,10 +650,12 @@ class NumberPickerDialog extends StatefulWidget { this.zeroPad = false, this.highlightSelectedValue = true, this.decoration, + this.textMapper, + this.haptics = false, Widget confirmWidget, Widget cancelWidget, - }) : confirmWidget = confirmWidget ?? new Text("OK"), - cancelWidget = cancelWidget ?? new Text("CANCEL"), + }) : confirmWidget = confirmWidget ?? Text("OK"), + cancelWidget = cancelWidget ?? Text("CANCEL"), decimalPlaces = 0, initialDoubleValue = -1.0; @@ -647,19 +669,20 @@ class NumberPickerDialog extends StatefulWidget { this.titlePadding, this.highlightSelectedValue = true, this.decoration, + this.textMapper, + this.haptics = false, Widget confirmWidget, Widget cancelWidget, - }) : confirmWidget = confirmWidget ?? new Text("OK"), - cancelWidget = cancelWidget ?? new Text("CANCEL"), + }) : confirmWidget = confirmWidget ?? Text("OK"), + cancelWidget = cancelWidget ?? Text("CANCEL"), initialIntegerValue = -1, step = 1, infiniteLoop = false, zeroPad = false; @override - State createState() => - new _NumberPickerDialogControllerState( - initialIntegerValue, initialDoubleValue); + State createState() => _NumberPickerDialogControllerState( + initialIntegerValue, initialDoubleValue); } class _NumberPickerDialogControllerState extends State { @@ -679,16 +702,19 @@ class _NumberPickerDialogControllerState extends State { NumberPicker _buildNumberPicker() { if (widget.decimalPlaces > 0) { - return new NumberPicker.decimal( - initialValue: selectedDoubleValue, - minValue: widget.minValue, - maxValue: widget.maxValue, - decimalPlaces: widget.decimalPlaces, - highlightSelectedValue: widget.highlightSelectedValue, - decoration: widget.decoration, - onChanged: _handleValueChanged); + return NumberPicker.decimal( + initialValue: selectedDoubleValue, + minValue: widget.minValue, + maxValue: widget.maxValue, + decimalPlaces: widget.decimalPlaces, + highlightSelectedValue: widget.highlightSelectedValue, + decoration: widget.decoration, + onChanged: _handleValueChanged, + textMapper: widget.textMapper, + haptics: widget.haptics, + ); } else { - return new NumberPicker.integer( + return NumberPicker.integer( initialValue: selectedIntValue, minValue: widget.minValue, maxValue: widget.maxValue, @@ -698,22 +724,24 @@ class _NumberPickerDialogControllerState extends State { highlightSelectedValue: widget.highlightSelectedValue, decoration: widget.decoration, onChanged: _handleValueChanged, + textMapper: widget.textMapper, + haptics: widget.haptics, ); } } @override Widget build(BuildContext context) { - return new AlertDialog( + return AlertDialog( title: widget.title, titlePadding: widget.titlePadding, content: _buildNumberPicker(), actions: [ - new FlatButton( + FlatButton( onPressed: () => Navigator.of(context).pop(), child: widget.cancelWidget, ), - new FlatButton( + FlatButton( onPressed: () => Navigator.of(context).pop(widget.decimalPlaces > 0 ? selectedDoubleValue : selectedIntValue), From 4baf99ada055cc463d870a100c9065503d19e199 Mon Sep 17 00:00:00 2001 From: AliRn76 Date: Tue, 28 Jul 2020 22:20:51 +0430 Subject: [PATCH 4/8] fix the format --- lib/numberpicker.dart | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index 4d6c838..c589fcc 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -124,15 +124,15 @@ class NumberPicker extends StatelessWidget { assert(initialValue >= minValue && initialValue <= maxValue), selectedIntValue = initialValue.floor(), selectedDecimalValue = ((initialValue - initialValue.floorToDouble()) * - math.pow(10, decimalPlaces)) + math.pow(10, decimalPlaces)) .round(), intScrollController = ScrollController( initialScrollOffset: (initialValue.floor() - minValue) * itemExtent, ), decimalScrollController = ScrollController( initialScrollOffset: ((initialValue - initialValue.floorToDouble()) * - math.pow(10, decimalPlaces)) - .roundToDouble() * + math.pow(10, decimalPlaces)) + .roundToDouble() * itemExtent, ), listViewHeight = 3 * itemExtent, @@ -239,7 +239,7 @@ class NumberPicker extends StatelessWidget { void animateDecimalAndInteger(double valueToSelect) { animateInt(valueToSelect.floor()); animateDecimal(((valueToSelect - valueToSelect.floorToDouble()) * - math.pow(10, decimalPlaces)) + math.pow(10, decimalPlaces)) .round()); } @@ -269,6 +269,10 @@ class NumberPicker extends StatelessWidget { } Widget _integerListView(ThemeData themeData) { + TextStyle defaultStyle = textStyle ?? themeData.textTheme.body1; + TextStyle selectedStyle = + selectedTextStyle ?? themeData.textTheme.headline.copyWith(color: themeData.accentColor); + var listItemCount = integerItemCount + 2; return Listener( From ec22370e877dd0762c80269ec6ca5804ac52db4b Mon Sep 17 00:00:00 2001 From: AliRn76 Date: Tue, 28 Jul 2020 22:25:40 +0430 Subject: [PATCH 5/8] fix format and styles --- lib/numberpicker.dart | 60 ++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index c589fcc..40797dc 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -88,13 +88,13 @@ class NumberPicker extends StatelessWidget { decimalPlaces = 0, intScrollController = infiniteLoop ? InfiniteScrollController( - initialScrollOffset: - (initialValue - minValue) ~/ step * itemExtent, - ) + initialScrollOffset: + (initialValue - minValue) ~/ step * itemExtent, + ) : ScrollController( - initialScrollOffset: - (initialValue - minValue) ~/ step * itemExtent, - ), + initialScrollOffset: + (initialValue - minValue) ~/ step * itemExtent, + ), decimalScrollController = null, listViewHeight = 3 * itemExtent, integerItemCount = (maxValue - minValue) ~/ step + 1, @@ -124,15 +124,15 @@ class NumberPicker extends StatelessWidget { assert(initialValue >= minValue && initialValue <= maxValue), selectedIntValue = initialValue.floor(), selectedDecimalValue = ((initialValue - initialValue.floorToDouble()) * - math.pow(10, decimalPlaces)) + math.pow(10, decimalPlaces)) .round(), intScrollController = ScrollController( initialScrollOffset: (initialValue.floor() - minValue) * itemExtent, ), decimalScrollController = ScrollController( initialScrollOffset: ((initialValue - initialValue.floorToDouble()) * - math.pow(10, decimalPlaces)) - .roundToDouble() * + math.pow(10, decimalPlaces)) + .roundToDouble() * itemExtent, ), listViewHeight = 3 * itemExtent, @@ -239,7 +239,7 @@ class NumberPicker extends StatelessWidget { void animateDecimalAndInteger(double valueToSelect) { animateInt(valueToSelect.floor()); animateDecimal(((valueToSelect - valueToSelect.floorToDouble()) * - math.pow(10, decimalPlaces)) + math.pow(10, decimalPlaces)) .round()); } @@ -300,19 +300,19 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = value == selectedIntValue && highlightSelectedValue - ? selectedTextStyle - : textStyle; + ? selectedStyle + : defaultStyle; bool isExtra = index == 0 || index == listItemCount - 1; return isExtra ? Container() //empty first and last element : Center( - child: Text( - getDisplayedValue(value), - style: itemStyle, - ), - ); + child: Text( + getDisplayedValue(value), + style: itemStyle, + ), + ); }, ), _NumberPickerSelectedItemDecoration( @@ -329,6 +329,10 @@ class NumberPicker extends StatelessWidget { } Widget _decimalListView(ThemeData themeData) { + TextStyle defaultStyle = textStyle ?? themeData.textTheme.body1; + TextStyle selectedStyle = + selectedTextStyle ?? themeData.textTheme.headline.copyWith(color: themeData.accentColor); + int decimalItemCount = selectedIntValue == maxValue ? 3 : math.pow(10, decimalPlaces) + 2; @@ -355,19 +359,19 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = value == selectedDecimalValue && highlightSelectedValue - ? selectedTextStyle - : textStyle; + ? selectedStyle + : defaultStyle; bool isExtra = index == 0 || index == decimalItemCount - 1; return isExtra ? Container() //empty first and last element : Center( - child: Text( - value.toString().padLeft(decimalPlaces, '0'), - style: itemStyle, - ), - ); + child: Text( + value.toString().padLeft(decimalPlaces, '0'), + style: itemStyle, + ), + ); }, ), _NumberPickerSelectedItemDecoration( @@ -384,6 +388,10 @@ class NumberPicker extends StatelessWidget { } Widget _integerInfiniteListView(ThemeData themeData) { + TextStyle defaultStyle = textStyle ?? themeData.textTheme.body1; + TextStyle selectedStyle = + selectedTextStyle ?? themeData.textTheme.headline.copyWith(color: themeData.accentColor); + return Listener( onPointerUp: (ev) { ///used to detect that user stopped scrolling @@ -406,8 +414,8 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = value == selectedIntValue && highlightSelectedValue - ? selectedTextStyle - : textStyle; + ? selectedStyle + : defaultStyle; return Center( child: Text( From 9de8e34b5e6807faa41dc68f1fa9803723d81f6f Mon Sep 17 00:00:00 2001 From: AliRn76 Date: Tue, 28 Jul 2020 22:38:19 +0430 Subject: [PATCH 6/8] fkk android studio for re formatting :\ --- lib/numberpicker.dart | 74 +++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index 40797dc..2af011a 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -88,13 +88,13 @@ class NumberPicker extends StatelessWidget { decimalPlaces = 0, intScrollController = infiniteLoop ? InfiniteScrollController( - initialScrollOffset: - (initialValue - minValue) ~/ step * itemExtent, - ) + initialScrollOffset: + (initialValue - minValue) ~/ step * itemExtent, + ) : ScrollController( - initialScrollOffset: - (initialValue - minValue) ~/ step * itemExtent, - ), + initialScrollOffset: + (initialValue - minValue) ~/ step * itemExtent, + ), decimalScrollController = null, listViewHeight = 3 * itemExtent, integerItemCount = (maxValue - minValue) ~/ step + 1, @@ -124,15 +124,15 @@ class NumberPicker extends StatelessWidget { assert(initialValue >= minValue && initialValue <= maxValue), selectedIntValue = initialValue.floor(), selectedDecimalValue = ((initialValue - initialValue.floorToDouble()) * - math.pow(10, decimalPlaces)) + math.pow(10, decimalPlaces)) .round(), intScrollController = ScrollController( initialScrollOffset: (initialValue.floor() - minValue) * itemExtent, ), decimalScrollController = ScrollController( initialScrollOffset: ((initialValue - initialValue.floorToDouble()) * - math.pow(10, decimalPlaces)) - .roundToDouble() * + math.pow(10, decimalPlaces)) + .roundToDouble() * itemExtent, ), listViewHeight = 3 * itemExtent, @@ -239,7 +239,7 @@ class NumberPicker extends StatelessWidget { void animateDecimalAndInteger(double valueToSelect) { animateInt(valueToSelect.floor()); animateDecimal(((valueToSelect - valueToSelect.floorToDouble()) * - math.pow(10, decimalPlaces)) + math.pow(10, decimalPlaces)) .round()); } @@ -299,20 +299,20 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = - value == selectedIntValue && highlightSelectedValue - ? selectedStyle - : defaultStyle; + value == selectedIntValue && highlightSelectedValue + ? selectedStyle + : defaultStyle; bool isExtra = index == 0 || index == listItemCount - 1; return isExtra ? Container() //empty first and last element : Center( - child: Text( - getDisplayedValue(value), - style: itemStyle, - ), - ); + child: Text( + getDisplayedValue(value), + style: itemStyle, + ), + ); }, ), _NumberPickerSelectedItemDecoration( @@ -334,7 +334,7 @@ class NumberPicker extends StatelessWidget { selectedTextStyle ?? themeData.textTheme.headline.copyWith(color: themeData.accentColor); int decimalItemCount = - selectedIntValue == maxValue ? 3 : math.pow(10, decimalPlaces) + 2; + selectedIntValue == maxValue ? 3 : math.pow(10, decimalPlaces) + 2; return Listener( onPointerUp: (ev) { @@ -358,20 +358,20 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = - value == selectedDecimalValue && highlightSelectedValue - ? selectedStyle - : defaultStyle; + value == selectedDecimalValue && highlightSelectedValue + ? selectedStyle + : defaultStyle; bool isExtra = index == 0 || index == decimalItemCount - 1; return isExtra ? Container() //empty first and last element : Center( - child: Text( - value.toString().padLeft(decimalPlaces, '0'), - style: itemStyle, - ), - ); + child: Text( + value.toString().padLeft(decimalPlaces, '0'), + style: itemStyle, + ), + ); }, ), _NumberPickerSelectedItemDecoration( @@ -413,9 +413,9 @@ class NumberPicker extends StatelessWidget { //define special style for selected (middle) element final TextStyle itemStyle = - value == selectedIntValue && highlightSelectedValue - ? selectedStyle - : defaultStyle; + value == selectedIntValue && highlightSelectedValue + ? selectedStyle + : defaultStyle; return Center( child: Text( @@ -459,7 +459,7 @@ class NumberPicker extends StatelessWidget { if (notification is ScrollNotification) { //calculate int intIndexOfMiddleElement = - (notification.metrics.pixels / itemExtent).round(); + (notification.metrics.pixels / itemExtent).round(); if (!infiniteLoop) { intIndexOfMiddleElement = intIndexOfMiddleElement.clamp(0, integerItemCount - 1); @@ -557,9 +557,9 @@ class NumberPicker extends StatelessWidget { ///indicates if user has stopped scrolling so we can center value in the middle bool _userStoppedScrolling( - Notification notification, - ScrollController scrollController, - ) { + Notification notification, + ScrollController scrollController, + ) { return notification is UserScrollNotification && notification.direction == ScrollDirection.idle && scrollController.position.activity is! HoldScrollActivity; @@ -610,9 +610,9 @@ class _NumberPickerSelectedItemDecoration extends StatelessWidget { const _NumberPickerSelectedItemDecoration( {Key key, - @required this.axis, - @required this.itemExtent, - @required this.decoration}) + @required this.axis, + @required this.itemExtent, + @required this.decoration}) : super(key: key); @override From 153f93a3c606e86fd4a1753b8a9a0cb102b8d294 Mon Sep 17 00:00:00 2001 From: Ali <40032682+AliRn76@users.noreply.github.com> Date: Tue, 1 Sep 2020 14:55:59 +0430 Subject: [PATCH 7/8] Fix for when max and min was the same in list (both were 0) --- lib/numberpicker.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index 2af011a..6bc9c90 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -41,7 +41,7 @@ class NumberPicker extends StatelessWidget { }) : assert(initialValue != null), assert(minValue != null), assert(maxValue != null), - assert(maxValue > minValue), + assert(maxValue >= minValue), assert(initialValue >= minValue && initialValue <= maxValue), assert(step > 0), selectedIntValue = initialValue, From 2ab2cea96f157e052ed10b9a923c294b8f7d7cae Mon Sep 17 00:00:00 2001 From: Ali <40032682+AliRn76@users.noreply.github.com> Date: Tue, 1 Sep 2020 15:03:46 +0430 Subject: [PATCH 8/8] fix another 2 more max >= min --- lib/numberpicker.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index 6bc9c90..244d402 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -79,7 +79,7 @@ class NumberPicker extends StatelessWidget { }) : assert(initialValue != null), assert(minValue != null), assert(maxValue != null), - assert(maxValue > minValue), + assert(maxValue >= minValue), assert(initialValue >= minValue && initialValue <= maxValue), assert(step > 0), assert(scrollDirection != null), @@ -120,7 +120,7 @@ class NumberPicker extends StatelessWidget { assert(minValue != null), assert(maxValue != null), assert(decimalPlaces != null && decimalPlaces > 0), - assert(maxValue > minValue), + assert(maxValue >= minValue), assert(initialValue >= minValue && initialValue <= maxValue), selectedIntValue = initialValue.floor(), selectedDecimalValue = ((initialValue - initialValue.floorToDouble()) *