【问题标题】:How to count how many lines in a textfield?如何计算文本字段中有多少行?
【发布时间】:2019-06-04 08:47:49
【问题描述】:

我需要知道用户在文本字段中输入了多少行。有什么语法可以做到吗?这是我的代码。出现“在初始化器中只能访问静态成员”

class MyCustomForm extends StatefulWidget {
  @override
  Page1 createState() => Page1();
}

class Page1 extends State<MyCustomForm> {

  final TextEditingController myController = TextEditingController();
  String change = '';

  final numLines = '\n'.allMatches(change).length + 1; //appears Only static members can be accessed in initializer

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ListView(
        children: <Widget>[
          new Container(
            padding: new EdgeInsets.all(10.0),
            child: new Column(
              children: <Widget>[
                new TextField(
                  keyboardType: TextInputType.multiline,
                  maxLines: 26,
                  controller: myController,
                  onChanged: (String e){
                    setState(() {
                      change = e;
                    });
                  },
                  decoration: new InputDecoration(
                      border: new OutlineInputBorder(
                          borderRadius: new BorderRadius.circular(20.0))),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}


【问题讨论】:

  • 添加一些你已经完成的代码
  • 这是我的代码

标签: android flutter dart textfield


【解决方案1】:

您可以在这里找到答案 flutter-how-to-get-the-number-of-text-lines

下次请确保在创建重复之前对以前的问题进行一些搜索。谢谢你

编辑

看来你是新来的颤振。你不能使用那里的功能

class MyCustomForm extends StatefulWidget {
  @override
  Page1 createState() => Page1();
}

class Page1 extends State<MyCustomForm> {

  final TextEditingController myController = TextEditingController();
  int numLines = 0;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new ListView(
        children: <Widget>[
          new Container(
            padding: new EdgeInsets.all(10.0),
            child: new Column(
              children: <Widget>[
                new TextField(
                  keyboardType: TextInputType.multiline,
                  maxLines: 26,
                  controller: myController,
                  onChanged: (String e){
                     setState((){
                         numLines = '\n'.allMatches(e).length + 1;
                     })
                  },
                  decoration: new InputDecoration(
                      border: new OutlineInputBorder(
                          borderRadius: new BorderRadius.circular(20.0))),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}



【讨论】:

  • 好吧,我已经检查过那个,但仍然没有解决我的问题。
  • 你的问题是什么?您可以从我提到的来源获取行数。
  • 当我把最后的 numLines = '\n'.allMatches(yourText).length + 1;似乎“只有静态成员可以在初始化程序中访问”我不知道应该修复什么。
  • 用代码更新您的问题。我认为您在错误的地方输入了代码。
  • @Praneeth Dhanushka Fernando 非常感谢您的帮助,我现在明白了。是的,我 1 周前才开始颤抖。我得到了 numLine,现在我想把它放在文本上。 new Text(numLine), 但还是没弄好。
猜你喜欢
  • 1970-01-01
  • 2013-04-27
  • 2011-10-06
  • 2011-05-24
  • 2023-02-21
  • 1970-01-01
  • 2012-10-01
  • 2019-02-16
  • 2014-10-17
相关资源
最近更新 更多