-1

I have this error

lib/menu.dart:230:24: Error: The getter '_context' isn't defined for the class 'DetailedSetting'.
  • DetailedSetting is from package:my_doctor/menu.dart('lib/menu.dart'). Try correcting the name to the name of an existing getter, or defining a getter or field named '_context'. MyDoctor.setLocale(_context, _temp);

with the code which is:

     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);
     }

     }
Recondit
  • 114
  • 9

1 Answers1

0

Add a BuildContext param to _changeLanguage method and pass this to the MyDoctor.setLocale as argument:

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);
}

Then, on your onChanged callback, pass the current context

onChanged: (Language lang) {
  _changeLanguage(lang, context);
},
Ignacior
  • 897
  • 2
  • 15