Getter 不是为类错误定义的(颤振)



我有这个错误

lib/menu.dart:230:24: Error: The getter '_context' isn't defined for the class 'DetailedSetting'.
  • DetailedSetting来自package:my_doctor/menu.dart('lib/manue.dart'(。请尝试将名称更正为现有getter的名称,或定义名为"_context"的getter或字段。MyDoctor.setLocale(_context, _temp);

代码为:

class MyDoctor extends StatefulWidget {
static void setLocale(BuildContext _context,Locale locale){
_MyDoctorState state = _context.findAncestorStateOfType<_MyDoctorState>();
state.setLocale(locale);
}
@override
_MyDoctorState createState() => _MyDoctorState();
}
class _MyDoctorState extends State<MyDoctor> {
Locale _locale;
void setLocale(Locale locale){
setState(() {
_locale = locale;
});
}
class DetailedSetting extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Text(
AppLocalization.of(context).translate('Settings'),
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
fontStyle: FontStyle.italic,
color: Colors.blue),
),
centerTitle: true,
actions: [
IconButton(
icon: Icon(Icons.list),
onPressed: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) => Menu()));
}),
]),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
GestureDetector(
child: Container(
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(40))),
color: Colors.blue,
),
child: ListTile(
title: Text(
AppLocalization.of(context).translate('profile_editing'),
style: TextStyle(fontSize: 20, color: Colors.white)),
leading: Icon(
Icons.person,
color: Colors.black,
size: 35,
),
),
),
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => ProfileForm()));
},
),
Container(
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(40))),
color: Colors.blue,
),
child: Row(mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Icon(
Icons.language,
color: Colors.black,
size: 35,
),
DropdownButton(
underline: SizedBox(),
hint: Text(AppLocalization.of(context).translate('change_lan'),
style: TextStyle(fontSize: 20, color: Colors.white)),
items: Language.languageList()
.map<DropdownMenuItem<Language>>((e) => DropdownMenuItem(
value: e,
child: Row(
children: [
Text(e.name),
Text(e.flag),
],
)))
.toList(),
onChanged: (Language lang) {
_changeLanguage(lang);
},
),
]),
),
GestureDetector(
child: Container(
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(40))),
color: Colors.blue,
),
child: ListTile(
title: Text(
AppLocalization.of(context).translate('change_pass'),
style: TextStyle(fontSize: 20, color: Colors.white)),
leading: Icon(
Icons.admin_panel_settings,
color: Colors.black,
size: 35,
),
),
),
onTap: () {},
),
],
),
);
}
void _changeLanguage(Language language) {
Locale _temp;
switch (language.languageCode) {
case "en":
_temp = Locale(language.languageCode, 'US');
break;
case "ar":
_temp = Locale(language.languageCode, 'EG');
break;
default:
_temp = Locale('en', 'ar');
}
MyDoctor.setLocale(_context, _temp);
}
}

_changeLanguage方法添加BuildContext参数,并将其作为参数传递给MyDoctor.setLocale

void _changeLanguage(Language language, BuildContext context) {
Locale _temp;
switch (language.languageCode) {
case "en":
_temp = Locale(language.languageCode, 'US');
break;
case "ar":
_temp = Locale(language.languageCode, 'EG');
break;
default:
_temp = Locale('en', 'ar');
}
MyDoctor.setLocale(context, _temp);
}

然后,在onChanged回调中,传递当前上下文

onChanged: (Language lang) {
_changeLanguage(lang, context);
},

最新更新