From a66ec00737d253f436a9e96f00c99c6d2a8a5cab Mon Sep 17 00:00:00 2001 From: Hugo Sartori Date: Thu, 5 Sep 2019 23:44:30 -0300 Subject: [PATCH 1/2] Added a text mapper to customize number text This code introduces the TextMapper, that is a function defined by the user of this widget to map the text displayed by the widget. This way, the user can define it's own code to show a custom text instead of just a plain number. --- lib/numberpicker.dart | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/numberpicker.dart b/lib/numberpicker.dart index 285f17a..14cfe3d 100644 --- a/lib/numberpicker.dart +++ b/lib/numberpicker.dart @@ -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 @@ -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, @@ -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, @@ -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, @@ -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...] @@ -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; } // From 0f73d24da5fe34f25eb541db09114d743060658b Mon Sep 17 00:00:00 2001 From: Hugo Sartori Date: Mon, 9 Sep 2019 17:59:16 -0300 Subject: [PATCH 2/2] Added test. --- test/integer_numberpicker_test.dart | 12 ++++++++++++ test/test_utils.dart | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/test/integer_numberpicker_test.dart b/test/integer_numberpicker_test.dart index 1e461f8..c46391a 100644 --- a/test/integer_numberpicker_test.dart +++ b/test/integer_numberpicker_test.dart @@ -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']); + }); + } diff --git a/test/test_utils.dart b/test/test_utils.dart index eaddf70..1585f7f 100644 --- a/test/test_utils.dart +++ b/test/test_utils.dart @@ -22,6 +22,7 @@ Future testNumberPicker({ int initialValue, int scrollBy, int step = 1, + TextMapper textMapper, int expectedValue, bool animateToItself = false, Axis axis = Axis.vertical, @@ -40,6 +41,7 @@ Future testNumberPicker({ minValue: minValue, maxValue: maxValue, step: step, + textMapper: textMapper, infiniteLoop: infiniteLoop, decoration: decoration, highlightSelectedValue: highlightSelectedValue, @@ -50,6 +52,7 @@ Future testNumberPicker({ minValue: minValue, maxValue: maxValue, step: step, + textMapper: textMapper, decoration: decoration, highlightSelectedValue: highlightSelectedValue, onChanged: (newValue) => setState(() => value = newValue), @@ -85,6 +88,7 @@ Future testMultipleValuesInPicker({ int initialValue, int scrollBy, int step = 1, + TextMapper textMapper, bool animateToItself = false, Axis axis = Axis.vertical, bool zeroPad = false, @@ -102,6 +106,7 @@ Future testMultipleValuesInPicker({ minValue: minValue, maxValue: maxValue, step: step, + textMapper: textMapper, infiniteLoop: infiniteLoop, onChanged: (newValue) => setState(() => value = newValue), zeroPad: zeroPad, @@ -111,6 +116,7 @@ Future testMultipleValuesInPicker({ minValue: minValue, maxValue: maxValue, step: step, + textMapper: textMapper, zeroPad: zeroPad, onChanged: (newValue) => setState(() => value = newValue), );