Flutter 在文本字段中换行文本,而不创建换行符

Flutter 在文本字段中换行文本,而不创建换行符,flutter,textfield,flutter-desktop,Flutter,Textfield,Flutter Desktop,使用maxLines=null属性,我可以将文本换行到TextField。但是,这也使用Enter创建换行符 我不想这样-我想保留Enter键,用于onSubmitted()函数。如何做到这一点?您可以通过文本字段的聚焦节点(WidgetOne)的onKey或RawKeyboardListener(widgetwo)实现这一点: 导入“包装:颤振/材料.省道”; 导入“包:flifter/services.dart”; 进口“包装:颤振钩/颤振钩.省道”; void main(){ runApp

使用
maxLines=null
属性,我可以将文本换行到
TextField
。但是,这也使用
Enter
创建换行符


我不想这样-我想保留
Enter
键,用于
onSubmitted()
函数。如何做到这一点?

您可以通过
文本字段
聚焦节点
WidgetOne
)的
onKey
RawKeyboardListener
widgetwo
)实现这一点:

导入“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
进口“包装:颤振钩/颤振钩.省道”;
void main(){
runApp(
材料聚丙烯(
标题:“包装单行文本字段”,
主页:_Page(),
),
);
}
类_页面扩展小部件{
常数页({
关键点,
}):super(key:key);
@凌驾
小部件构建(构建上下文){
最终结果=使用状态(“”);
返回脚手架(
主体:填充物(
填充:常数边集全部(32.0),
子:列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
_WidgetOne(提交时:(值)=>result.value=value),
_WidgetTwo(提交时:(值)=>result.value=value),
容器(
边距:所有常数边集(15.0),
填充:常数边集全部(3.0),
装饰:
BoxEdition(边框:border.all(颜色:Colors.black)),
子项:文本('Result:${Result.value}'),
],
),
),
);
}
}
类_WidgetOne扩展了无状态小部件{
提交时更改的最终值;
const_WidgetOne({Key-Key,this.onSubmitted}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
最终_controller=TextEditingController();
final _focusNode=focusNode(onKey:(节点,事件){
if(event.isKeyPressed(LogicalKeyboardKey.enter)){
已提交(_controller.text);
node.unfocus();
返回true;
}
返回false;
});
返回文本字段(
装饰:输入装饰(hintText:“带FocusNode onKey”),
控制器:_控制器,
focusNode:_focusNode,
maxLines:null,
);
}
}
类_widgetwo扩展了无状态小部件{
提交时更改的最终值;
const_widgetwo({Key-Key,this.onSubmitted}):super(Key:Key);
@凌驾
小部件构建(构建上下文){
最终_controller=TextEditingController();
final _focusNode=focusNode(onKey:(节点,事件){
if(event.isKeyPressed(LogicalKeyboardKey.enter)){
已提交(_controller.text);
node.unfocus();
返回true;
}
返回false;
});
返回键盘侦听器(
focusNode:_focusNode,
onKey:(事件){
if(event.isKeyPressed(LogicalKeyboardKey.enter)){
已提交(_controller.text);
}
},
孩子:TextField(
装饰:输入装饰(hintText:“带RawKeyboardListener onKey”),
控制器:_控制器,
maxLines:null,
),
);
}
}

感谢您给出了非常好的答案,这也是一个非常有用的好例子!不客气!:-)
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Wrapping Single Line Text Fields',
      home: _Page(),
    ),
  );
}

class _Page extends HookWidget {
  const _Page({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final result = useState('');
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.all(32.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            _WidgetOne(onSubmitted: (value) => result.value = value),
            _WidgetTwo(onSubmitted: (value) => result.value = value),
            Container(
                margin: const EdgeInsets.all(15.0),
                padding: const EdgeInsets.all(3.0),
                decoration:
                    BoxDecoration(border: Border.all(color: Colors.black)),
                child: Text('Result: ${result.value}')),
          ],
        ),
      ),
    );
  }
}

class _WidgetOne extends StatelessWidget {
  final ValueChanged<String> onSubmitted;

  const _WidgetOne({Key key, this.onSubmitted}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _controller = TextEditingController();
    final _focusNode = FocusNode(onKey: (node, event) {
      if (event.isKeyPressed(LogicalKeyboardKey.enter)) {
        onSubmitted(_controller.text);
        node.unfocus();
        return true;
      }
      return false;
    });
    return TextField(
      decoration: InputDecoration(hintText: 'With FocusNode onKey'),
      controller: _controller,
      focusNode: _focusNode,
      maxLines: null,
    );
  }
}

class _WidgetTwo extends StatelessWidget {
  final ValueChanged<String> onSubmitted;

  const _WidgetTwo({Key key, this.onSubmitted}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final _controller = TextEditingController();
    final _focusNode = FocusNode(onKey: (node, event) {
      if (event.isKeyPressed(LogicalKeyboardKey.enter)) {
        onSubmitted(_controller.text);
        node.unfocus();
        return true;
      }
      return false;
    });
    return RawKeyboardListener(
      focusNode: _focusNode,
      onKey: (event) {
        if (event.isKeyPressed(LogicalKeyboardKey.enter)) {
          onSubmitted(_controller.text);
        }
      },
      child: TextField(
        decoration: InputDecoration(hintText: 'With RawKeyboardListener onKey'),
        controller: _controller,
        maxLines: null,
      ),
    );
  }
}