小编典典

如何在Flutter中创建数字输入字段?

flutter

我找不到在Flutter中创建可打开
数字键盘的输入字段的方法。Flutter材质小部件可以做到这一点吗?一些
github讨论似乎表明这是受支持的功能,但是我
找不到有关它的任何文档。


阅读 298

收藏
2020-08-13

共1个答案

小编典典

您可以使用以下命令将数字指定 为
keyboardType
for the TextField using:

keyboardType: TextInputType.number

检查我的main.dart文件

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      home: new HomePage(),
      theme: new ThemeData(primarySwatch: Colors.blue),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new HomePageState();
  }
}

class HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.white,
      body: new Container(
          padding: const EdgeInsets.all(40.0),
          child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new TextField(
            decoration: new InputDecoration(labelText: "Enter your number"),
            keyboardType: TextInputType.number,
            inputFormatters: <TextInputFormatter>[
    WhitelistingTextInputFormatter.digitsOnly
], // Only numbers can be entered
          ),
        ],
      )),
    );
  }
}
2020-08-13