User should be able to select a number from a range of odd numbers.
User should be able to adjust selected number by tapping on the increment and decrement buttons.
Prerequisites
Install Flutter and Dart on your machine. Create a new Flutter project.
Create a new dart file number_picker.dart in the lib directory.
In this file add a simple boilerplate for a stateful widget.
And update main.dart to use the NumberPicker widget.
Now we are set for success :)
Show range of odd number horizontally
Idea for my NumberPicker widget is to show a range of odd numbers horizontally and let user scroll it to select a number.
To achieve that we will use SingleChildScrollView with Row widget.
We will generate a list of numbers using List.generate and add them to the parent Row widget.
In this step we will focus on implementing code for body of the Scaffold widget in the NumberPicker widget.
Follow inline comments to understand the code.
Try to run your app and see if you can see a range of odd numbers showed horizontally, where numbers are white on blue background.
Add onTap logic to select a number
Now we will add onTap logic to the GestureDetector widget.
When user taps on a number we will change the background color of the selected number to green.
Let’s run the app and see if we can select a number by tapping on it and if it changes its color to green.
Note: I guess you noticed, I messed with colors, and switched them. So Blue is selected number and Green is not selected.
Add increment and decrement buttons to adjust selected number
In this step we will add 3 new visual elements to our widget:
Decrement button
Text widget to show selected number
Increment button
I used a lazy way to create buttons, do not use it in production code.
Follow inline comments to understand the code.
Save your changes and refresh the app to see the new UI.
As you can see now we have horizontal range of odd numbers, user can select a number by tapping on it and adjust it by using increment and decrement buttons.
Next two steps are optional, and you can implement them if you want to have some fun.
Add visual widgets for even numbers
I would like to add instead of SizedBox widget for even numbers a visual representation of even numbers.
User won’t be able to tap on them, but they will be able to see them.
Also, these widgets will change color based on selected number.
Save, refresh, test :)
As you can see, when you change number by clicking Increment/Decrement buttons, even numbers change their color based on selected number.
My goal was to decrease confusion of end user, so they can quickly select number see which number is selected.
Add animation for horizontal scroll on number select event
In this step we will add a simple animation to scroll to the selected number when user taps on it, or changes it by using Increment/Decrement buttons.
For this we will use ScrollController and animateTo method.
One custom logic is added to calculate the offset based on the selected number. This offset includes size of the odd number widget plus even number widget to correctly translate position of horizontal scroll and show selected number on the screen.
After saving these changes you should be able to see the animation when you tap on a number or use Increment/Decrement buttons.
Note: This is a simple animation, you can improve it by adding more complex animations. And implement it in a more efficient way.
Add colors gradient from red to green
In this final section, I will show you how to add a gradient color to each number (odd/even) widget.
My idea is to use red-ish color for small numbers and with each number increase to change color to green.
The higher number the better.
To create a gradient color we will use Color.lerp, not the best, can create some ugly blending colors, but it’s fast.
Now let’s save your changes and refresh the app to see the final result.
Note: Try to change this approach, and instead of making background of different color, change the border color of the number widget.
Conclusion
In this post we learned how to build a number picker in Flutter without using any external packages.
We started with a simple case of showing a range of odd numbers horizontally, then added onTap logic to select a number, and increment/decrement buttons to adjust the selected number.
We added visual representation of even numbers, and animated horizontal scroll to selected number.
Finally, we added a gradient color from red to green for each number widget.
Code is not perfect, and can be improved in many ways, but it’s a good starting point for building your own number picker widget.
Thank you for reading, and I hope you learned something new today 🧁