📜  Flutter – 使用英语单词

📅  最后修改于: 2022-05-13 01:55:25.933000             🧑  作者: Mango

Flutter – 使用英语单词

在Flutter中,如果您想知道是否有任何方法可以使用英语单词,或者在哪里可以找到任何使用英语单词的库,搜索到此结束。有一个名为 english_words 的库,其中包含最多 5000 个使用过的英语单词以及一些实用功能。它在字典或与教学相关的应用程序等应用程序中很有用。在本文中,我们将了解它并查看它的用法。将来,此软件包的作者可能会为其添加更多功能。

添加包:

首先,使用以下任一方法将english_words包添加到pubspec.yaml

在 IDE 的终端中运行以下命令:

flutter pub add english_words

或者将其添加到pubspec.yaml文件的依赖项部分下,并使用 pub get- 进行配置

Dart
dependencies:
  english_words: ^4.0.0


Dart
import 'package:english_words/english_words.dart';


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


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


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


导入依赖:

主要。 dart文件,添加english_words 依赖项。

Dart

import 'package:english_words/english_words.dart';

执行:

我们将在 main.js 中编写整个代码。dart文件。

生成单词:

如果我们想生成英文单词,我们使用generateWordPairs ()函数,我们可以告诉这个函数我们想要生成多少个单词。在这里,我们正在创建一个generateWords ()函数。要遍历生成的每个单词,我们使用forEach () 方法并将生成的每个单词添加到单词列表中,以便稍后在 UI 上显示。

Dart

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

生成名词:

现在,我们要生成名词,我们创建generateNouns () 函数,在其中我们使用名词方法并告诉它传递 50 个名词。然后我们遍历它们并将它们添加到名词列表中,以便稍后在屏幕上显示。我们将使用 ListView.builder 在列表中显示项目。

Dart

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

输出:

完整源代码:

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

输出: