29

I'm experimenting with Flutter development on Windows. I have a simple test app with an InputField. I would like the first keyboard entry to be a capital letter but can't see a way of achieving that (e.g. launching the keyboard with shift pressed) that at the moment. Any ideas?

Code (a bit simplified) is:

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;
  }
}
4

3 回答 3

56

Flutter 为文本字段提供了textCapitalization属性。将此属性设置为TextCapitalization.sentences或任何可用值,例如.characters 或 .words像这样:

TextField(
   keyboardType: TextInputType.text,
   **textCapitalization: TextCapitalization.sentences,**
   style: TextStyle(
      fontSize: 30.0,
      color: Colors.black,
      fontWeight: FontWeight.bold
   ),
)
于 2019-01-22T14:16:21.240 回答
24

这是TextInputAction类行为的完整列表

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

  1. TextCapitalization.sentences: 这是最常见的大写形式,每个句子的首字母都转换为大写。

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

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

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

    TextField(
 
      autocorrect: false,
    ),

    

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

于 2020-11-04T03:44:42.880 回答
4

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

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

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

于 2017-04-13T04:28:57.820 回答