📅  最后修改于: 2023-12-03 14:41:15.625000             🧑  作者: Mango
Flutter TextFormField Decimal is a Flutter widget that provides an input field for decimal values, allowing users to enter and manipulate decimal numbers in your Flutter application. This widget is useful when you need to collect specific decimal input from users, such as prices, quantities, or percentages.
To use Flutter TextFormField Decimal in your Flutter application, you need to follow these steps:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
TextFormField
widget:TextFormField(
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')),
],
decoration: InputDecoration(
labelText: 'Enter a decimal value',
),
);
Customize the keyboardType
to allow decimal input and set the desired number formatting. Here, we use TextInputType.numberWithOptions(decimal: true)
.
Use inputFormatters
to define the allowed number format and precision. In this example, we only allow up to 2 digits after the decimal point using FilteringTextInputFormatter
.
Add any additional styling or validation as per your application requirements.
Here's a complete example of using Flutter TextFormField Decimal:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Decimal TextFormField Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _formKey = GlobalKey<FormState>();
final _decimalController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Decimal TextFormField Demo'),
),
body: Form(
key: _formKey,
child: Padding(
padding: EdgeInsets.all(16.0),
child: Column(
children: <Widget>[
TextFormField(
controller: _decimalController,
keyboardType: TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^\d+\.?\d{0,2}')),
],
decoration: InputDecoration(
labelText: 'Enter a decimal value',
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter a decimal value.';
}
return null;
},
),
SizedBox(height: 16.0),
RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
final enteredValue = double.parse(_decimalController.text);
// Use the entered value as needed.
}
},
child: Text('Submit'),
),
],
),
),
),
);
}
}
In this example, we create a form with a TextFormField
that allows users to enter a decimal value. The entered value is then validated and can be used for further processing.
Remember to include the necessary imports and wrap the TextFormField
in a Form
widget for proper form validation and submission handling.
By using the Flutter TextFormField Decimal widget, you can easily collect and handle decimal input from users in your Flutter application. This widget provides an intuitive and convenient way to ensure the entered decimal values meet your desired format and precision requirements.