Skip to content

Commit

Permalink
Merge pull request #56 from HugoSart/patch-1
Browse files Browse the repository at this point in the history
Added a text mapper to customize number text
  • Loading branch information
MarcinusX committed Mar 5, 2020
2 parents edc933b + 0f73d24 commit bc80954
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/numberpicker.dart
Expand Up @@ -7,6 +7,9 @@ import 'package:infinite_listview/infinite_listview.dart';

/// Created by Marcin Szałek
///Define a text mapper to transform the text displayed by the picker
typedef String TextMapper(String numberText);

///NumberPicker is a widget designed to pick a number between #minValue and #maxValue
class NumberPicker extends StatelessWidget {
///height of every list element for normal number picker
Expand All @@ -24,6 +27,7 @@ class NumberPicker extends StatelessWidget {
@required this.minValue,
@required this.maxValue,
@required this.onChanged,
this.textMapper,
this.itemExtent = kDefaultItemExtent,
this.listViewHeight = kDefaultListViewCrossAxisSize,
this.step = 1,
Expand Down Expand Up @@ -56,6 +60,7 @@ class NumberPicker extends StatelessWidget {
@required this.minValue,
@required this.maxValue,
@required this.onChanged,
this.textMapper,
this.itemExtent = kDefaultItemExtent,
this.listViewWidth = kDefaultListViewCrossAxisSize,
this.step = 1,
Expand Down Expand Up @@ -95,6 +100,7 @@ class NumberPicker extends StatelessWidget {
@required this.minValue,
@required this.maxValue,
@required this.onChanged,
this.textMapper,
this.decimalPlaces = 1,
this.itemExtent = kDefaultItemExtent,
this.listViewWidth = kDefaultListViewCrossAxisSize,
Expand Down Expand Up @@ -135,6 +141,9 @@ class NumberPicker extends StatelessWidget {

///max value user can pick
final int maxValue;

///build the text of each item on the picker
final TextMapper textMapper;

///inidcates how many decimal places to show
/// e.g. 0=>[1,2,3...], 1=>[1.0, 1.1, 1.2...] 2=>[1.00, 1.01, 1.02...]
Expand Down Expand Up @@ -410,9 +419,10 @@ class NumberPicker extends StatelessWidget {
}

String getDisplayedValue(int value) {
return zeroPad
final text = zeroPad
? value.toString().padLeft(maxValue.toString().length, '0')
: value.toString();
return textMapper != null ? textMapper(text) : text;
}

//
Expand Down
12 changes: 12 additions & 0 deletions test/integer_numberpicker_test.dart
Expand Up @@ -113,4 +113,16 @@ void main() {
decoration: decoration,
);
});

testWidgets('Text mapper works', (WidgetTester tester) async {
await testMultipleValuesInPicker(
tester: tester,
minValue: 0,
maxValue: 10,
initialValue: 2,
scrollBy: 1,
textMapper: (text) => '$text days',
expectedDisplayValues: ['2 days', '3 days', '4 days']);
});

}
6 changes: 6 additions & 0 deletions test/test_utils.dart
Expand Up @@ -22,6 +22,7 @@ Future<NumberPicker> testNumberPicker({
int initialValue,
int scrollBy,
int step = 1,
TextMapper textMapper,
int expectedValue,
bool animateToItself = false,
Axis axis = Axis.vertical,
Expand All @@ -40,6 +41,7 @@ Future<NumberPicker> testNumberPicker({
minValue: minValue,
maxValue: maxValue,
step: step,
textMapper: textMapper,
infiniteLoop: infiniteLoop,
decoration: decoration,
highlightSelectedValue: highlightSelectedValue,
Expand All @@ -50,6 +52,7 @@ Future<NumberPicker> testNumberPicker({
minValue: minValue,
maxValue: maxValue,
step: step,
textMapper: textMapper,
decoration: decoration,
highlightSelectedValue: highlightSelectedValue,
onChanged: (newValue) => setState(() => value = newValue),
Expand Down Expand Up @@ -85,6 +88,7 @@ Future<NumberPicker> testMultipleValuesInPicker({
int initialValue,
int scrollBy,
int step = 1,
TextMapper textMapper,
bool animateToItself = false,
Axis axis = Axis.vertical,
bool zeroPad = false,
Expand All @@ -102,6 +106,7 @@ Future<NumberPicker> testMultipleValuesInPicker({
minValue: minValue,
maxValue: maxValue,
step: step,
textMapper: textMapper,
infiniteLoop: infiniteLoop,
onChanged: (newValue) => setState(() => value = newValue),
zeroPad: zeroPad,
Expand All @@ -111,6 +116,7 @@ Future<NumberPicker> testMultipleValuesInPicker({
minValue: minValue,
maxValue: maxValue,
step: step,
textMapper: textMapper,
zeroPad: zeroPad,
onChanged: (newValue) => setState(() => value = newValue),
);
Expand Down

0 comments on commit bc80954

Please sign in to comment.