Home > database >  Generate widgets in-between texts in flutter
Generate widgets in-between texts in flutter

Time:02-28

I am trying to generate a list of pages numbers after every 300 words but can't. please does anybody know how I can implement this?.

class Try extends StatelessWidget {
  const Try({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final String text4 = lorem(paragraphs: 8, words: 2000);
    return Scaffold(
      body: SingleChildScrollView(
        child: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(9),
            child: Column(
              children: [
                Text(text4),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

I tried adding the below code to it but...

for (int i = 0; i < text4.length;300 * i  )
                    //if (text4.length == 300)
                    Padding(
                      padding: const EdgeInsets.all(14),
                      child: Column(
                        children: [
                          const Text('page ${a  = 1}'),
                          Divider(
                            color: Theme.of(context).primaryColor,
                          )
                        ],
                      ),
                    ),
Text(text4),

CodePudding user response:

You can use substring to extract text and page number logic will be i / 300 1. It will provide 300 letters. For word case you need to convert text to list by splitting on space.

final wordList = text4.split(" ").toList();
 String text4 =
      "I am trying to generate a list of pages numbers after every 300 words but can't. please does anybody know how I can implement this?.I am trying to generate a list of pages numbers after every 300 words but can't. please does anybody know how I can implement this?.I am trying to generate a list of pages numbers after every 300 words but can't. please does anybody know how I can implement this?.I am trying to generate a list of pages numbers after every 300 words but can't. please does anybody know how I can implement this?.I am trying to generate a list of pages numbers after every 300 words but can't. please does anybody know how I can implement this?.I am trying to generate a list of pages numbers after every 300 words but can't. please does anybody know how I can implement this?.";
  late final wordList = text4.split(" ").toList();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        padding: EdgeInsets.all(16.0),
        child: Column(
          children: [
            // for (int i = 0; i < text4.length; i  = 300)
            for (int i = 0; i < wordList.length; i  = 300)
              Padding(
                padding: const EdgeInsets.all(14),
                child: Column(
                  children: [
                    Text('page ${i / 300   1}'),
                    Divider(
                      color: Theme.of(context).primaryColor,
                    ),

                    /// for letters
                    // Text(text4.substring(
                    //   i,
                    //   i   300 > text4.length ? text4.length : i   300,
                    // )),

                    () {
                      final textX = wordList
                          .sublist(
                              i,
                              i   300 > wordList.length
                                  ? wordList.length
                                  : i   300)
                          .toString();
                      return Text(textX.substring(1, textX.length - 1));
                    }()
                  ],
                ),
              ),
          ],
        ),
      ),
    );
  }

enter image description here

[] depends on original text, for substring it is handled Text(textX.substring(1, textX.length - 1));.

  • Related