我该如何在Flutter的文本输入框中配置自动大写行为?

43

我正在Windows上进行Flutter开发尝试。我的一个简单测试应用程序中有一个输入框。我希望第一个键盘输入是大写字母,但目前看不到任何实现方法(例如使用按下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;
  }
}

你能给一个具有这种行为的Android或iOS应用程序的例子吗?或者一个我们应该通过Flutter的默认键盘包装器公开的iOS或Android API的例子?如果可以的话,我们很乐意提供!欢迎在https://github.com/flutter/flutter/issues/new提交功能请求。 - Eric Seidel
或者说iPhone 上的标准 Notes 应用程序怎么样?开始新笔记时,键盘会带着 Shift 键被按下,正如我想要的那样。 - iBob101
3个回答

82

Flutter提供了一个文本输入框的textCapitalization属性。将此属性设置为TextCapitalization.sentences或任何可用值,例如.characters或.words。如下所示:

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

6

在我们的iOS实现Flutter键盘包装器中,首字母小写是一个错误,该错误已经在今天得到修复!

我已经为此问题提交了一个bug,以使其可配置(这样您就可以禁用自动大写句子的行为),链接在此:https://github.com/flutter/flutter/issues/9363

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


0
最常见的问题是由于某种原因:

keyboardType: TextInputType.name不会将第一个字母大写,尽管你会认为“name”应该大写。

因此,一个典型的用户会添加:
textCapitalization: TextCapitalization.words,

但是,除非你删除keyboardType: TextInputType.name,否则它不起作用,一旦删除,它就会按预期工作。还有其他令人沮丧的行为组合。
删除键盘类型,但保留文本大写:TextCapitalization.words

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接