Skip to content
Related Articles

Related Articles

Flutter – Working with English Words

Improve Article
Save Article
  • Last Updated : 31 Jan, 2022
Improve Article
Save Article

In Flutter, if you are wondering is there any way to work with English words or where to find any library that works with English words the search ends here. There is a library named english_words that contains at most 5000 used English words with some utility functions. It’s useful in applications like dictionaries or teaching-related apps. In this article, we will be learning about it and seeing its usage. In the future, the author of this package might add more functionalities to it.

Add the package:

First, add the english_words package to the pubspec.yaml using either of the following method shown below.

Run the following command in the terminal of IDE:

flutter pub add english_words

Or add it under the dependencies section in pubspec.yaml file and configure it using pub get- 

Dart




dependencies:
  english_words: ^4.0.0


Import the dependency:

In the main.dart file, add english_words dependency.

Dart




import 'package:english_words/english_words.dart';


Implementation:

We will be writing the whole code in main.dart file. 

Generate Words:

If we want to generate words in English, we use generateWordPairs() function, we can tell this function to how many words we want to generate. Here, we are creating a generateWords() function. To loop through each word generated we use the forEach() method and add every word generated to the words list to later display on UI.

Dart




generateWords() {
    generateWordPairs().take(5).forEach((element) {
      words.add(element.toString());
    });
  }


Generate Nouns:

Now, we want to generate nouns, we create generateNouns() functions, in which we are using the nouns method and tell it to deliver 50 nouns. Then we loop through them and add them to the nounsList to later on display on the screen. We will display items in the list using ListView.builder.

Dart




List nounsList = [];
  
  generateNouns() {
    nouns.take(50).forEach((element) {
      nounsList.add(element.toString());
    });
  }


Output:

Full Source Code:

Dart




import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
  
void main() => runApp(MyApp());
  
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'english words',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: MyHomePage(),
    );
  }
}
  
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
  
class _MyHomePageState extends State<MyHomePage> {
  List words = [];
  List nounsList = [];
  
  generateNouns() {
    nouns.take(30).forEach((element) {
      nounsList.add(element.toString());
    });
  }
  
  generateWords() {
    generateWordPairs().take(30).forEach((element) {
      words.add(element.toString());
    });
  }
  
  void initState() {
    generateWords();
    generateNouns();
    super.initState();
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("GeeksForGeeks"),
        centerTitle: true,
      ),
      body: Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        child: Row(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Container(
              width: 70,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Text(
                    "Words",
                    style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                  ),
                  Expanded(
                    child: words.isNotEmpty
                        ? ListView.builder(
                            itemCount: words.length,
                            itemBuilder: (ctx, idx) {
                              return Text(words[idx]);
                            })
                        : Text("Loading..."),
                  )
                ],
              ),
            ),
            Container(
              width: 80,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    "Nouns",
                    style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                  ),
                  Expanded(
                    child: nounsList.isNotEmpty
                        ? ListView.builder(
                            itemCount: nounsList.length,
                            itemBuilder: (ctx, idx) {
                              return Text(nounsList[idx]);
                            })
                        : Text("Loading..."),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}


Output:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!