Sukarma
Sukarma

Reputation: 55

How to Count Number of Lines in flutter

I'm new to flutter, I am Trying to create Count Number of Lines from textfield, like this https://www.tools4noobs.com/online_tools/count_lines/. but, I still have a problem with how to calculate it.

Can you give an example, I will appreciate it.

Upvotes: 2

Views: 4929

Answers (1)

So what I have found to get number of lines from a text is this flutter-how-to-get-the-number-of-text-lines

To read text you can use this Handling changes to a text field

Here's an example I did using onChanged function.. You can use a controller also

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  num lines = 0;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          body: Column(
        children: <Widget>[
          TextField(
            maxLines: null,
            onChanged: (text) {
              setState(() {
                lines = '\n'.allMatches(text).length + 1;
              });
            },
          ),
          Text(lines.toString())
        ],
      )),
    );
  }
}


Upvotes: 4

Related Questions