没有为类错误定义 Getter (Flutter)

时间:2021-04-16 04:26:32

标签: flutter

我有这个错误

lib/menu.dart:230:24: Error: The getter '_context' isn't defined for the class 'DetailedSetting'.
  • DetailedSetting 来自 package:my_doctor/menu.dart('lib/menu.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);
     }

     }

1 个答案:

答案 0 :(得分:0)

BuildContext 参数添加到 _changeLanguage 方法并将其作为参数传递给 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);
},