如何在 Flutter 中将文本大写

Muk*_*kha 10 text flutter

我试图找到如何在 Flutter 中将文本大写,但我找不到它。

我的代码:

Center(
    heightFactor: 2,
    child: Text(
      'Strengthening the bond of owners and pets, more than ever...',
      textAlign: TextAlign.center,
      style: TextStyle(
        fontSize: 20.0,
        fontStyle: FontStyle.italic,
        fontWeight: FontWeight.bold,
        color: Colors.cyanAccent[700],
        wordSpacing: 8,
      ),
    )),
Run Code Online (Sandbox Code Playgroud)

Ben*_*sal 21

我不知道是否有办法通过Text小部件来做到这一点,但您可以使用string.toUppercase()大写单词:

Center(
heightFactor: 2,
child: Text(
  'Strengthening the bond of owners and pets, more than ever...'.toUpperCase(),
  textAlign: TextAlign.center,
  style: TextStyle(
    fontSize: 20.0,
    fontStyle: FontStyle.italic,
    fontWeight: FontWeight.bold,
    color: Colors.cyanAccent[700],
    wordSpacing: 8,
  ),
)),
Run Code Online (Sandbox Code Playgroud)

更新:

要仅将字符串的第一个字母大写,您可以简单地添加extension如下所示:

extension StringExtension on String {
  String capitalize() {
     return 
       "${this[0].toUpperCase()}${substring(1).toLowerCase()}";
  }
}
Run Code Online (Sandbox Code Playgroud)

并在任何地方调用它,如下所示:

'string'.capitalize()
Run Code Online (Sandbox Code Playgroud)

  • @bensel - 这不是大写,而是大写 (3认同)

tfm*_*gue 15

大写和大写是不同的:

  • 大写:“加强……”
  • 大写:“加强……”

大写

要在所有语言环境中将字符串大写:

import 'package:intl/intl.dart';

toBeginningOfSentenceCase('strengthening...');
Run Code Online (Sandbox Code Playgroud)

要将 en-US 等语言环境的字符串大写:

String text = 'strengthening...';
text = text[0].toUpperCase() + text.substring(1).toLowerCase();
Run Code Online (Sandbox Code Playgroud)

您还可以让虚拟键盘在句子开头自动切换为大写:

TextField(
  textCapitalization: TextCapitalization.sentences
)
Run Code Online (Sandbox Code Playgroud)

https://api.flutter.dev/flutter/services/TextCapitalization.html

大写

要将字符串大写:

'strengthening...'.toUpperCase()
Run Code Online (Sandbox Code Playgroud)

您还可以让虚拟键盘自动将每个字符切换为大写

TextField(
  textCapitalization: TextCapitalization.characters
)
Run Code Online (Sandbox Code Playgroud)

https://api.flutter.dev/flutter/services/TextCapitalization.html