如何 运行 在子部件 FLUTTER 中发挥作用
How to run function in child widget FLUTTER
在我的 main.dart 中,我有 StatefulWidget 并且 class 扩展了 State 和函数以将字符串写入和读取本地文件:
Future<void> _writeDataToFile(favs) async {
final _dirPath = await _getDirPath();
final _myFile = File('$_dirPath/fav.txt');
await _myFile.writeAsString("||${_myVariable}||");
}
Future<void> _readData() async {
final dirPath = await _getDirPath();
final myFile = File('$dirPath/fav.txt');
final data = await myFile.readAsString(encoding: utf8);
setState(() {
_content = data;
});
}
我 运行 来自分隔文件的小部件:
itemBuilder: (context, index) => allRadioList(articles[index], context, _content),
我如何 运行 _readData() 从 allRadioList 小部件更改状态。
这是我的 allRadioList(articles[index], context, _content) 代码:
Widget allRadioList(Article article, BuildContext context, String contentt) {
return Card(
child: ListTile(
title: Text(article.title),
subtitle: Text(contentt),
leading: CircleAvatar(backgroundImage: NetworkImage("https://image.tmdb.org/t/p/w300/${article.urlToImage}")),
trailing: IconButton(
icon: contentt.contains(article.id.toString()) ? Icon(Icons.star_border_outlined, color: Colors.yellow) : Icon(Icons.star_border_outlined, color: Colors.white),
tooltip: 'My Tootltip',
onPressed: () {
_writeDataToFile(article.id.toString());
_readData();
},
),
));
}
函数是对象,因此您可以将它们作为函数参数传递并在您的函数中调用它
// (){} is a anonymous function (with no name)
itemBuilder: (context, index) => allRadioList(articles[index], context, _content,onPressed: (){
_writeDataToFile(articles[index].id.toString());
_readData();
}),
和
Widget allRadioList(Article article, BuildContext context, String contentt,{Funtion onPressed}) {
return Card(
child: ListTile(
title: Text(article.title),
subtitle: Text(contentt),
leading: CircleAvatar(backgroundImage: NetworkImage("https://image.tmdb.org/t/p/w300/${article.urlToImage}")),
trailing: IconButton(
icon: contentt.contains(article.id.toString()) ? Icon(Icons.star_border_outlined, color: Colors.yellow) : Icon(Icons.star_border_outlined, color: Colors.white),
tooltip: 'My Tootltip',
onPressed: onPressed,
),
));
}
您是否尝试将 onPressed 函数设为异步并使用 await 调用这些函数?喜欢;
onPressed: () async {
await _writeDataToFile(article.id.toString());
await _readData();
},
在我的 main.dart 中,我有 StatefulWidget 并且 class 扩展了 State 和函数以将字符串写入和读取本地文件:
Future<void> _writeDataToFile(favs) async {
final _dirPath = await _getDirPath();
final _myFile = File('$_dirPath/fav.txt');
await _myFile.writeAsString("||${_myVariable}||");
}
Future<void> _readData() async {
final dirPath = await _getDirPath();
final myFile = File('$dirPath/fav.txt');
final data = await myFile.readAsString(encoding: utf8);
setState(() {
_content = data;
});
}
我 运行 来自分隔文件的小部件:
itemBuilder: (context, index) => allRadioList(articles[index], context, _content),
我如何 运行 _readData() 从 allRadioList 小部件更改状态。
这是我的 allRadioList(articles[index], context, _content) 代码:
Widget allRadioList(Article article, BuildContext context, String contentt) {
return Card(
child: ListTile(
title: Text(article.title),
subtitle: Text(contentt),
leading: CircleAvatar(backgroundImage: NetworkImage("https://image.tmdb.org/t/p/w300/${article.urlToImage}")),
trailing: IconButton(
icon: contentt.contains(article.id.toString()) ? Icon(Icons.star_border_outlined, color: Colors.yellow) : Icon(Icons.star_border_outlined, color: Colors.white),
tooltip: 'My Tootltip',
onPressed: () {
_writeDataToFile(article.id.toString());
_readData();
},
),
));
}
函数是对象,因此您可以将它们作为函数参数传递并在您的函数中调用它
// (){} is a anonymous function (with no name)
itemBuilder: (context, index) => allRadioList(articles[index], context, _content,onPressed: (){
_writeDataToFile(articles[index].id.toString());
_readData();
}),
和
Widget allRadioList(Article article, BuildContext context, String contentt,{Funtion onPressed}) {
return Card(
child: ListTile(
title: Text(article.title),
subtitle: Text(contentt),
leading: CircleAvatar(backgroundImage: NetworkImage("https://image.tmdb.org/t/p/w300/${article.urlToImage}")),
trailing: IconButton(
icon: contentt.contains(article.id.toString()) ? Icon(Icons.star_border_outlined, color: Colors.yellow) : Icon(Icons.star_border_outlined, color: Colors.white),
tooltip: 'My Tootltip',
onPressed: onPressed,
),
));
}
您是否尝试将 onPressed 函数设为异步并使用 await 调用这些函数?喜欢;
onPressed: () async {
await _writeDataToFile(article.id.toString());
await _readData();
},