![](/img/trans.png)
[英]How to call a function half way through the scroll controller in flutter
[英]Is there a way to call on difference constructors through a loop in flutter
我正在为我的母语 flutter 设计一个移动词典,每个单词及其含义都在它们的构造函数中,并且我实现了一个搜索栏,如下面的代码所示
这是我的话及其含义 class
class WordList {
final String word;
final String meaning;
const WordList({required this.word, required this.meaning});
}
这是我在变量中调用的构造函数
const dictionary = [
WordList(word: 'àbàdâ', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'bàba', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'badà', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'céndzũ̀', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'bagábà', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'àbàdâ', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'bàba', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'badà', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'céndzũ̀', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'bagábà', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'àbàdâ', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'bàba', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'badà', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'céndzũ̀', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'bagábà', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'àbàdâ', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'bàba', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'badà', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'céndzũ̀', meaning: 'Eternal, everlasting,forever'),
WordList(word: 'bagábà', meaning: 'Eternal, everlasting,forever'),
];
如上面的代码所示,很难为每个单词及其含义调用所有 WordList 构造函数,所以我想是否可以创建两个列表变量(单词和含义),或者从文件,以便在const dictionary
变量中,我将循环遍历并使用单词及其含义调用所有构造函数
这是我的应用程序的完整代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final controller = TextEditingController();
List<WordList> words = dictionary;
List<String> listWords = worda;
List<String> listMeanings = meaninga;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Nupe / English Dictionary'),
elevation: 0.0,
),
body: Container(
child: Column(
children: [
Container(
color: Colors.blue,
padding: EdgeInsets.fromLTRB(10, 0, 10, 10),
child: TextField(
controller: controller,
scrollPadding: EdgeInsets.all(50),
decoration: InputDecoration(
contentPadding: EdgeInsets.only(top: 5, left:15),
filled: true,
fillColor: Colors.white,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5),
),
hintText: 'Enter Nupe/English Word...',
suffixIcon: Icon(Icons.search)
),
onChanged: search,
),
),
Container(
//child: Text('wornd and meaning'),
child: Expanded(
child: ListView.builder(
itemCount: words.length,
itemBuilder: (BuildContext contex, int index){
final word = words[index];
final listWord = listWords[index];
final listMeaning = listMeanings[index];
return ListTile(
tileColor: Colors.white,
onTap: null,
title: Text(word.word,
style: TextStyle(color: Colors.black,
fontWeight: FontWeight.bold,
),
),
subtitle: Text(word.meaning,
style: TextStyle(color: Colors.black),
),
);
}
),
),
)
],
),
),
);
}
void search(String value) {
final suggestions = dictionary.where((word) {
final wordd = word.word.toLowerCase();
final input = value.toLowerCase();
return wordd.contains(input);
}).toList();
setState(() => words = suggestions);
}
}
避免为每个字典条目写出构造函数的简单方法是使用 map,例如
List<WordList> words = {
'àbàdâ': 'Eternal, everlasting,forever',
'bàba': 'Eternal, everlasting,forever',
'badà': 'Eternal, everlasting,forever',
}
.map((key, value) => MapEntry(key, WordList(word: key, meaning: value)))
.values
.toList();
从文件中读取将取决于您可用的格式,但如果您可以选择 go 和 json。这里的链接应该可以帮助您开始:
https://docs.flutter.dev/cookbook/persistence/reading-writing-files#4-read-data-from-the-file https://docs.flutter.dev/development/data-and-backend/json #serializing-json-manually-using-dartconvert
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.