如何在颤振中删除TextField/TextFormField中的空行?



是否可以在TextField/TextFormField中删除空行?在键入一些输入后,我按shift+enter创建一些新的空行,然后我的控制器接收所有这些空行。

有两个地方需要拒绝使用空格。

1。当输入Input时

TextFormField(
validator: (value),
inputFormatters: [
FilteringTextInputFormatter.deny(new RegExp(r"sb|bs"))
],
)

2。输入Input

TextFormField(
controller: _controller,
....
)

使用:_controller.trim()这将修剪所有的空白在textformfield

从textfield中获取字符串。我们称这个字符串为s,然后用s.trimRight()来自动删除右边所有的空格。类似地,您可以执行s.trimLeft()来删除左侧的空格。

border: InputBorder.none,in decoration:const InputDecoration()

试试这个代码:

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,
),
),
),
],
),
),
);
}
}

最新更新