Flutter中仅数字TextFormField [英] Digit only TextFormField in Flutter

查看:559
本文介绍了Flutter中仅数字TextFormField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要在¥中输入价格的应用程序,因此该应用程序没有小数位. 如果使用keyboardType: TextInputType.numberWithOptions(),则可以获得数字键盘输入. 如果使用validator: (input) { },则可以检查输入是否有效,但不能阻止输入.

Im am working on an app that requires a price input in ¥ and as such has no decimal places. If we use keyboardType: TextInputType.numberWithOptions() we can get a number pad input. If we use validator: (input) { } we can check if input is valid but we cannot prevent it.

问题是我们可以保存不需要验证的草稿. 因此,最好只允许数字输入.

The problem is we can save a draft that will not need validation. So it is better for us to only allow digit input in the first place.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Digits Only',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextFormField(
              autovalidate: true,
              keyboardType: TextInputType.number,
              validator: (input) {
                final isDigitsOnly = int.tryParse(input);
                return isDigitsOnly == null
                    ? 'Input needs to be digits only'
                    : null;
              },
            ),
          ],
        ),
      ),
    );
  }
}

有没有一种方法可以防止某些文本输入并且仅允许数字输入?

Is there a way to prevent certain text input and only allow digits?

推荐答案

是的,您可以使用inputFormatters属性并添加WhitelistingTextInputFormatter.digitsOnly表达式

Yep, you can use the inputFormatters attribute and add the WhitelistingTextInputFormatter.digitsOnly expression

  import 'package:flutter/services.dart';


  TextFormField(
          ...
          inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],
        )

您可以在此处找到更多信息: https://docs.flutter.io /flutter/services/TextInputFormatter-class.html

You can find more info here: https://docs.flutter.io/flutter/services/TextInputFormatter-class.html

在Flutter 1.12之后,不推荐使用WhitelistingTextInputFormatter,而您正在运行更新的版本,请使用:

After flutter 1.12, WhitelistingTextInputFormatter was deprecated and you are running a newer version, use :

FilteringTextInputFormatter.digitsOnly

https://api.flutter.dev/flutter/services/FilteringTextInputFormatter- class.html

这篇关于Flutter中仅数字TextFormField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆