简体   繁体   English

如何去除Flutter中TextField/TextFormField中的空行?

[英]How to remove empty lines in TextField/TextFormField in Flutter?

is it possible to remove empty lines in TextField/TextFormField?是否可以删除 TextField/TextFormField 中的空行? After typing some input I type shift+enter to create some new empty lines, and then my controller takes all these empty lines.键入一些输入后,我键入 shift+enter 以创建一些新的空行,然后我的 controller 获取所有这些空行。

Get the string from textfield.从文本字段中获取字符串。 Let's call this string s and then just do s.trimRight() to automatically delete all empty spaces from the right side.让我们将此字符串称为s ,然后只需执行s.trimRight()即可自动删除右侧的所有空格。 Similarly you can do s.trimLeft() to delete empty spaces from the left.同样,您可以执行s.trimLeft()来删除左侧的空格。

There are two places you would want to deny whitespaces.您可能希望在两个地方拒绝空格。

1.While Input is entered. 1.输入时。

TextFormField(
     validator: (value),
     inputFormatters: [
          FilteringTextInputFormatter.deny(new RegExp(r"\s\b|\b\s"))
        ],
     )

2.After Input is entered. 2.输入后。

TextFormField(
      controller: _controller,
      ....
)

Use: _controller.trim() this will trim all the whitespace in the textformfield使用: _controller.trim()这将修剪 textformfield 中的所有空白

border: InputBorder.none, in decoration:const InputDecoration() border: InputBorder.none,在装饰中:const InputDecoration()

Try this code:试试这个代码:

import 'package:flutter/material.dart';

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

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

class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController _textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
           TextFormField(
                controller: _textController,
                style: const TextStyle(
                  fontSize: 14,
                  fontFamily: 'Work_Sans',
                  fontWeight: FontWeight.w500,
                ),
                decoration:const InputDecoration(
                  hintText: 'Type Here',
                  border: InputBorder.none,
                  fillColor: Colors.white,
                  hintStyle:  TextStyle(
                    fontSize: 14,
                    color: Colors.grey,
                    fontFamily: 'Work_Sans',
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }
}

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

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