如何计算文本字段中的行数?

How to count how many lines in a textfield?

我需要知道用户在文本字段中输入了多少行。有什么语法可以做到吗?这是我的代码。出现 "only static members can be accessed in initializer"

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))),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}


你可以在这里找到你的答案

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

编辑

看来您是 Flutter 新手。你不能使用那里的功能

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))),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}