如何在Flutter的文本输入字段中配置自动大写行为?

iBo*_*101 6 dart flutter

我正在Windows上尝试Flutter开发。我有一个带有InputField的简单测试应用程序。我希望第一个键盘输入为大写字母,但目前看不到一种实现该功能的方法(例如,按下Shift键启动键盘)。有任何想法吗?

代码(稍微简化一下)是:

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
      theme: new ThemeData.dark(),
      home: new MainScreen()
  ));
}

class MainScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
            leading: new IconButton(
                icon: new Icon(Icons.menu),
                tooltip: 'Navigation menu',
                onPressed: null,
            ),
            title: new Text('Test'),
        ),
        body: new NewTest(),
    );
  }
}

/// Widget
class NewTest extends StatefulWidget {
  @override
  _NewTestInputState createState() => new _NewTestInputState();
}
/// State
class _NewTestInputState extends State<NewTest> {
  InputValue _currentInput;

  void _handleInputChange(InputValue input) {
    if (input != _currentInput){
      setState(() {
        _currentInput = input;
      });
    }
  }

  void _handleInputSubmitted(InputValue input) {
    setState(() {
      _currentInput = const InputValue();
    });
  }

  @override
  Widget build(BuildContext context) {
    InputField _widget = new InputField(
        value: _currentInput,
        hintText: 'Enter text',
        keyboardType: TextInputType.text,
        autofocus: true,
        onChanged: _handleInputChange,
        onSubmitted: _handleInputSubmitted,
        style: new TextStyle(fontSize: 20.0),
    );
    Container _container = new Container(
        child: _widget,
        decoration: new BoxDecoration(
            border: new Border.all(
                color: Colors.green[300],
                width: 2.0,
            ),
        ),
        padding: new EdgeInsets.all(16.0),
    );
    return _container;
  }
}
Run Code Online (Sandbox Code Playgroud)

Par*_*iya 17

这里是行为的完整列表TextInputAction

textCapitalization : TextField 提供了将用户输入的文本大写的选项。

  1. TextCapitalization.sentences: 这是最常见的大写类型,每个句子的第一个字母都转换为大写。

    TextField(
     textCapitalization: TextCapitalization.sentences,
    ),
    
    Run Code Online (Sandbox Code Playgroud)
  2. TextCapitalization.characters: 将句子中的所有字符大写。

    TextField(
     textCapitalization: TextCapitalization.characters,
    ),
    
    Run Code Online (Sandbox Code Playgroud)
  3. TextCapitalization.words: 每个单词的首字母大写。

    TextField(
     textCapitalization: TextCapitalization.words,
    ),
    
    Run Code Online (Sandbox Code Playgroud)
  4. 启用或禁用特定 TextField 的自动更正。使用自动更正字段进行设置。这也会禁用输入建议。

    TextField(? 
      autocorrect: false,
    ),?
    
    Run Code Online (Sandbox Code Playgroud)

注意:仅支持文本键盘,其他键盘类型将忽略此配置。大小写是区域设置的。


小智 16

Flutter具有用于文本字段的textCapitalization属性。将此属性设置为TextCapitalization.sentences或任何可用值,例如.characters或.words,如下所示:

TextField(
   keyboardType: TextInputType.text,
   **textCapitalization: TextCapitalization.sentences,**
   style: TextStyle(
      fontSize: 30.0,
      color: Colors.black,
      fontWeight: FontWeight.bold
   ),
)
Run Code Online (Sandbox Code Playgroud)


Eri*_*del 6

起始小写字母是 Flutter 键盘包装器的 iOS 实现中的一个错误,该错误已于今天修复!

我在这里提交了一个使其可配置的错误(以便您可以禁用自动大写句子行为): https: //github.com/flutter/flutter/issues/9363

如果这不能解决您的问题,请随时与我们联系。