Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a text mapper to customize number text #56

Merged
merged 2 commits into from Mar 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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