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

0 android textfield dart flutter

我需要知道用户输入的文本字段中有多少行。有没有这样做的语法?这是我的代码。出现“只能在初始化程序中访问静态成员”

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


Run Code Online (Sandbox Code Playgroud)

Pra*_*ndo 5

您可以在此处找到答案 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))),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}



Run Code Online (Sandbox Code Playgroud)