颤振 用于本地化的未定义名称"上下文"



请帮助我如何传递本地化上下文在这里?错误提示:未定义名称"context"。尝试将名称更正为已定义的名称,或定义名称。

category1.dart
import 'package:flutter/material.dart';
import 'package:myproject/classes/item.dart';
import 'page_details.dart';
import 'package:myproject/classes/language_constants.dart';
List<Item> detailsList = [
Item(
name: translation(context).name_1,
image: 'assets/rivers/object1.jpg',
details: translation(context).details_1,
facts: [
translation(context).fact_1_1,
translation(context).fact_1_2,
translation(context).fact_1_3,
],
photolink: '',
author: '',
licensetype: '',
licenselink: '',
),
Item(
name: translation(context).name_2,
image: 'assets/rivers/object1.jpg',
details: translation(context).details_2,
facts: [
translation(context).fact_2_1,
translation(context).fact_2_2,
translation(context).fact_2_3,
],
photolink: '',
author: '',
licensetype: '',
licenselink: '',
),
] 

translation()定义如下:

language_constants.dart
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
AppLocalizations translation(BuildContext context) {
return AppLocalizations.of(context)!;
}

类Item定义如下:

item.dart
class Item {
final String name;
final String image;
final String details;
final String photolink;
final String author;
final String licensetype;
final String licenselink;
List<String> facts = <String>[];
Item({
required this.name,
required this.image,
required this.details,
required this.facts,
required this.photolink,
required this.author,
required this.licensetype,
required this.licenselink,
});
}

我通过detailsList[index]:

传递适当的类Item作为内容
category1.dart
ElevatedButton.icon(
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.only(left: 8, right: 8),
minimumSize: const Size(30, 30),
),
onPressed: () {
_navigateToPageDetails(context, detailsList[index]);
},
...
)

和MaterialPageRoute ():

category1.dart
_navigateToPageDetails(BuildContext context, Item item) async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PageDetails(
item: item,
),
),
);
}

最后一页:

page_details.dart
import 'package:myproject/classes/item.dart';
class PageDetails extends StatefulWidget {
final Item item;
const PageDetails({Key? key, required this.item}) : super(key: key);
@override
State<PageDetails> createState() => _PageDetailsState();
}
class _PageDetailsState extends State<PageDetails> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.item.name),
),
body: SingleChildScrollView(
child:Column(
children: [
Image.asset(widget.item.image),
...

所以,我不明白如何将上下文传递给Class Item,这样这个简单的代码就不会抛出错误:

translation(context).name_1,
-->
Error: Undefined name 'context'.

我已经尝试添加BuildContext上下文类,但它没有帮助。我试过使用widget。语境——一样,没有帮助。也许类Item应该扩展一些类?但这种尝试也无济于事。

请帮帮我。

接受答案后更新

遵循Saichi Okuma上面的完美建议,我想分享我的应用程序的代码,这样也许它会对其他人有用。

Item detailsList(BuildContext context, index) {
final List<Item> list = [];
list.addAll([
Item(
name: translation(context).name_1,
image: 'assets/rivers/object1.jpg',
details: translation(context).details_1,
facts: [
translation(context).fact_1_1,
translation(context).fact_1_2,
translation(context).fact_1_3,
],
photolink: '',
author: '',
licensetype: '',
licenselink: '',
),
Item(
name: translation(context).name_2,
image: 'assets/rivers/object2.jpg',
details: translation(context).details_2,
facts: [
translation(context).fact_2_1,
translation(context).fact_2_2,
translation(context).fact_2_3,
],
photolink: '',
author: '',
licensetype: '',
licenselink: '',
),
]);

final Item element = list[index];
return element;
}

上面的函数获取上下文并返回Item类所需的元素。

,然后不使用列表(就像在旧的初始代码中一样),我按如下方式调用该方法:

onPressed: () {
_navigateToPageDetails(context, detailsList(context, index));
}, 

当您构建列表时没有上下文,这就是您得到错误的原因。一个简单的解决方法,保持你的逻辑:


List<Item> detailsList(BuildContext context) {
final List<Item> list = [];

list.addAll([
Item(
name: translation(context).name_1,
image: 'assets/rivers/object1.jpg',
details: translation(context).details_1,
facts: [
translation(context).fact_1_1,
translation(context).fact_1_2,
translation(context).fact_1_3,
],
photolink: '',
author: '',
licensetype: '',
licenselink: '',
),
Item(
name: translation(context).name_2,
image: 'assets/rivers/object1.jpg',
details: translation(context).details_2,
facts: [
translation(context).fact_2_1,
translation(context).fact_2_2,
translation(context).fact_2_3,
],
photolink: '',
author: '',
licensetype: '',
licenselink: '',
),
]);

return list;
} 

当你请求一个列表时,传递上下文作为参数。

相关内容

  • 没有找到相关文章

最新更新