Skip to content

Commit

Permalink
Merge ec22370 into dd040ad
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRn76 committed Jul 28, 2020
2 parents dd040ad + ec22370 commit 8d95979
Showing 1 changed file with 56 additions and 43 deletions.
99 changes: 56 additions & 43 deletions lib/numberpicker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class NumberPicker extends StatelessWidget {
this.highlightSelectedValue = true,
this.decoration,
this.haptics = false,
this.textStyle,
this.selectedTextStyle,
}) : assert(initialValue != null),
assert(minValue != null),
assert(maxValue != null),
Expand Down Expand Up @@ -72,6 +74,8 @@ class NumberPicker extends StatelessWidget {
this.highlightSelectedValue = true,
this.decoration,
this.haptics = false,
this.textStyle,
this.selectedTextStyle,
}) : assert(initialValue != null),
assert(minValue != null),
assert(maxValue != null),
Expand All @@ -84,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,
Expand All @@ -110,6 +114,8 @@ class NumberPicker extends StatelessWidget {
this.highlightSelectedValue = true,
this.decoration,
this.haptics = false,
this.textStyle,
this.selectedTextStyle,
}) : assert(initialValue != null),
assert(minValue != null),
assert(maxValue != null),
Expand All @@ -118,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,
Expand Down Expand Up @@ -202,6 +208,12 @@ class NumberPicker extends StatelessWidget {
///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 ------------------------------
//
Expand All @@ -227,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());
}

Expand Down Expand Up @@ -257,9 +269,9 @@ class NumberPicker extends StatelessWidget {
}

Widget _integerListView(ThemeData themeData) {
TextStyle defaultStyle = themeData.textTheme.body1;
TextStyle defaultStyle = textStyle ?? themeData.textTheme.body1;
TextStyle selectedStyle =
themeData.textTheme.headline.copyWith(color: themeData.accentColor);
selectedTextStyle ?? themeData.textTheme.headline.copyWith(color: themeData.accentColor);

var listItemCount = integerItemCount + 2;

Expand Down Expand Up @@ -287,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(
Expand All @@ -317,12 +329,12 @@ class NumberPicker extends StatelessWidget {
}

Widget _decimalListView(ThemeData themeData) {
TextStyle defaultStyle = themeData.textTheme.body1;
TextStyle defaultStyle = textStyle ?? themeData.textTheme.body1;
TextStyle selectedStyle =
themeData.textTheme.headline.copyWith(color: themeData.accentColor);
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) {
Expand All @@ -346,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(
Expand All @@ -376,9 +388,9 @@ class NumberPicker extends StatelessWidget {
}

Widget _integerInfiniteListView(ThemeData themeData) {
TextStyle defaultStyle = themeData.textTheme.body1;
TextStyle defaultStyle = textStyle ?? themeData.textTheme.body1;
TextStyle selectedStyle =
themeData.textTheme.headline.copyWith(color: themeData.accentColor);
selectedTextStyle ?? themeData.textTheme.headline.copyWith(color: themeData.accentColor);

return Listener(
onPointerUp: (ev) {
Expand All @@ -401,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(
Expand Down Expand Up @@ -447,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);
Expand Down Expand Up @@ -545,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;
Expand Down Expand Up @@ -598,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
Expand Down Expand Up @@ -750,3 +762,4 @@ class _NumberPickerDialogControllerState extends State<NumberPickerDialog> {
);
}
}

0 comments on commit 8d95979

Please sign in to comment.