Providing a user-friendly numeric input widget is often necessary when developing a Flutter application. Whether you're building a finance app that requires precise value adjustments or a game that allows users to set their character's attributes, a well-implemented numeric input widget can significantly enhance the user experience.
In this blog, we'll delve into implementing a Flutter SpinBox widget that allows users to input numbers conveniently.
A SpinBox in Flutter is a numeric input widget allowing users to make value adjustments using spin buttons. These buttons typically appear on either side of the input field, enabling users to increment or decrement the current value.
The widget replaces the need for a plain text field, providing a more intuitive interface for numerical input, especially when the user is expected to enter a specific value within a certain range.
To begin with, ensure that you have the necessary package imported into your Dart code. The flutter_spinbox package is a popular choice that provides a Material or iOS-style SpinBox. To import this package, add the following line to your pubspec.yaml file:
1dependencies: 2 flutter_spinbox: ^0.13.1 3
After adding the dependency, run the flutter packages get the command to fetch the package.
Here's a simple example of implementing a SpinBox in your Flutter application. This code snippet demonstrates the creation of a numeric input widget with default value adjustments using spin buttons.
1import 'package:flutter/material.dart'; 2import 'package:flutter_spinbox/flutter_spinbox.dart'; 3 4void main() => runApp(MyApp()); 5 6class MyApp extends StatelessWidget { 7 8 Widget build(BuildContext context) { 9 return MaterialApp( 10 home: Scaffold( 11 appBar: AppBar(title: Text('SpinBox Example')), 12 body: Center( 13 child: SpinBox( 14 min: 0, 15 max: 100, 16 value: 50, 17 onChanged: (value) => print(value), 18 ), 19 ), 20 ), 21 ); 22 } 23} 24
In the above Dart code, the SpinBox widget is placed within the build method of a stateless widget. The min and max properties define the range of the numeric input widget, while the value property sets the initial value. The onChanged function triggers the new value whenever the user adjusts the spin buttons.
To provide more accurate control over value adjustments, you can customize the behavior of the spin buttons. For instance, you can set a specific value increment or decrement step or even implement custom logic for value adjustments based on the previous value.
1SpinBox( 2 min: 0, 3 max: 100, 4 value: 50, 5 increment: 5, 6 decoration: InputDecoration(labelText: 'Select quantity'), 7 onChanged: (newValue) { 8 // Custom logic can be added here 9 }, 10) 11
In this snippet, the increment property is set to 5, meaning that each tap on the spin buttons will adjust the value by 5. The decoration property adds a label to the input field, providing a textual description to the user.
For applications that aim to mimic the iOS style, the flutter_spinbox package offers an iOS-style numeric input widget. This widget inherits the look and feel of iOS UI elements, providing a familiar interface for users of Apple devices.
1import 'package:flutter_spinbox/cupertino.dart'; // or flutter_spinbox.dart for both 2CupertinoSpinBox( 3min: 1, 4max: 100, 5value: 50, 6onChanged: (value) => print(value), 7)
While the above code does not explicitly set an iOS style, the SpinBox widget will adapt its appearance based on the platform. If you're running your app on an iOS device, it will automatically render the iOS-style spin buttons.
The SpinBox widget also supports advanced features such as custom spin button icons and debugging diagnostics. The icons can be customized to match the overall design of your application, while the diagnostics help identify issues during development.
1SpinBox( 2 min: 0, 3 max: 100, 4 value: 50, 5 iconSize: 24.0, 6 iconActiveColor: Colors.green, 7 iconDisabledColor: Colors.grey, 8 onChanged: (newValue) { 9 // Handle the new value 10 }, 11) 12
In this example, the iconSize, iconActiveColor, and iconDisabledColor properties are used to customize the appearance of the spin buttons' icons.
For debugging purposes, the SpinBox widget provides a debugFillProperties method that adds information to the DiagnosticsNode objects describing the widget's properties. This is part of the Flutter framework's debugging tools, which can help you understand the tree of widgets and their properties.
When dealing with numeric input widgets, handling user input accurately and providing validation is crucial. The SpinBox also allows users to type a specific value directly into the input field. You can use the validator function to ensure that the input is within the permitted range and is a valid number.
1SpinBox( 2 min: 0, 3 max: 100, 4 value: 50, 5 onChanged: (newValue) { 6 // Handle the new value 7 }, 8 validator: (value) { 9 if (value == null || value.isEmpty) { 10 return 'Please enter a number'; 11 } 12 final numValue = num.tryParse(value); 13 if (numValue == null || numValue < 0 || numValue > 100) { 14 return 'Please enter a number between 0 and 100'; 15 } 16 return null; 17 }, 18) 19
In this Dart code snippet, the validator function checks if the input is empty, a number, and falls within the specified range. If the input doesn't meet these conditions, it returns an error message that will be displayed to the user.
Incorporating a SpinBox as a numeric input widget in your Flutter application can greatly improve the user experience by providing convenient value adjustments. Using spin buttons, users can quickly increment or decrement the value or enter a specific value directly into the input field. Customizing the appearance and behavior of the SpinBox to match your app's design and functionality is straightforward, thanks to the flexible properties provided by the widget.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.