简体   繁体   English

Flutter:如何在textformfield中加下划线

[英]Flutter: How to underline in textformfield

I'm trying to make it such that the user can enter his pin, and the pin is supposed to be 4 digits long.我试图让用户可以输入他的密码,密码应该是 4 位数字长。 Now, the 4 digits that he can enter should be underscored in the text form field (even before he enters the pin), somewhat like ____ .现在,他可以输入的 4 位数字应该在文本表单字段中加上下划线(甚至在他输入 pin 之前),有点像____

So something like this:所以像这样: 在此处输入图像描述

Basically just a normal input field that has the 4 underscores.基本上只是一个带有 4 个下划线的普通输入字段。 Is there anyway I can do this in textformfield?无论如何我可以在 textformfield 中做到这一点吗?

Thanks in advance!提前致谢!

I have tried this on Android Kotlin by using TextWatcher , and I think this could be done by this link below: Here's a reference !我已经使用TextWatcher在 Android Kotlin 上尝试过这个,我认为这可以通过下面的链接来完成:这是 一个参考

Try This: https://api.flutter.dev/flutter/material/UnderlineInputBorder-class.html试试这个: https://api.flutter.dev/flutter/material/UnderlineInputBorder-class.html

Full Code:完整代码:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('TextField Input Decoration Demo'),
        ),
        body: new SafeArea(
            top: true,
            bottom: true,
            child: Column(
              children: [
                Padding(
                    padding: EdgeInsets.all(20),
                    child: Row(
                      children: [
                        Container(
                            width: 40,
                            child: TextFormField(
                              style: TextStyle(fontSize: 50),
                              textInputAction: TextInputAction.next,
                              decoration: const InputDecoration(
                                enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                  color: Colors.black,
                                  width: 10.0,
                                )),
                                focusedBorder: UnderlineInputBorder(
                                  borderSide: BorderSide(
                                    color: Colors.black,
                                    width: 10.0,
                                  ),
                                ),
                              ),
                            )),
                        SizedBox(width: 5),
                        Container(
                            width: 40,
                            child: TextFormField(
                              style: TextStyle(fontSize: 50),
                              textInputAction: TextInputAction.next,
                              decoration: const InputDecoration(
                                enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                  color: Colors.black,
                                  width: 10.0,
                                )),
                                focusedBorder: UnderlineInputBorder(
                                  borderSide: BorderSide(
                                    color: Colors.black,
                                    width: 10.0,
                                  ),
                                ),
                              ),
                            )),
                        SizedBox(width: 5),
                        Container(
                            width: 40,
                            child: TextFormField(
                              style: TextStyle(fontSize: 50),
                              textInputAction: TextInputAction.next,
                              decoration: const InputDecoration(
                                enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                  color: Colors.black,
                                  width: 10.0,
                                )),
                                focusedBorder: UnderlineInputBorder(
                                  borderSide: BorderSide(
                                    color: Colors.black,
                                    width: 10.0,
                                  ),
                                ),
                              ),
                            )),
                        SizedBox(width: 5),
                        Container(
                            width: 40,
                            child: TextFormField(
                              style: TextStyle(fontSize: 50),
                              textInputAction: TextInputAction.next,
                              decoration: const InputDecoration(
                                enabledBorder: UnderlineInputBorder(
                                    borderSide: BorderSide(
                                  color: Colors.black,
                                  width: 10.0,
                                )),
                                focusedBorder: UnderlineInputBorder(
                                  borderSide: BorderSide(
                                    color: Colors.black,
                                    width: 10.0,
                                  ),
                                ),
                              ),
                            )),
                      ],
                    )),
                new Divider(
                  color: Colors.black,
                ),
              ],
            )));
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM