Yoseph Mario Wibowo
Yoseph Mario Wibowo

Reputation: 3

How to count how many lines in a textfield?

I need to know how many lines in textfield that have been input by the user. Is there any syntax for doing it? Here is my code. Appears "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))),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}


Upvotes: 0

Views: 6839

Answers (1)

You can find your answer here flutter-how-to-get-the-number-of-text-lines

Next time please make sure you do some search on previous questions before your create duplicates. Thank You

EDIT

Seems you are new to flutter. You can't use the function there

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



Upvotes: 9

Related Questions