📜  flutter textformfield decimal - Dart (1)

📅  最后修改于: 2023-12-03 14:41:15.625000             🧑  作者: Mango

Flutter TextFormField Decimal - Dart

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.

Features
  • Allows users to enter decimal numbers with specified precision and scale.
  • Handles decimal input validation and error messages.
  • Supports customization of text formatting and input constraints.
  • Provides a convenient way to retrieve entered decimal values.
Usage

To use Flutter TextFormField Decimal in your Flutter application, you need to follow these steps:

  1. Import the necessary packages:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
  1. Create a form widget or a screen with a 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',
  ),
);
  1. Customize the keyboardType to allow decimal input and set the desired number formatting. Here, we use TextInputType.numberWithOptions(decimal: true).

  2. 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.

  3. Add any additional styling or validation as per your application requirements.

Example

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.

Conclusion

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.