Problem in multilingualizing the app in flutter

Viewed 12

iam new in flutter.i encountered a problem in making my app multilingual. On the other pages of my app, when the code S.of(context).myText is called, the code works correctly and And the text corresponding to the language chosen by the audience is displayed, but on the one class of my app named Home, by calling this program code, regardless of the selected language, only the English text is displayed shows even if I choose Malaysia or Arabic. Interestingly, when I build the project with Chrome, there is no problem, but when I download the apk from it, it does not work on the mobile. the problem is with S.of(context).Home_drawer_mainMenu, in below code: (Sorry, my code is too much, please help me, my work is lame) my Home class:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:untitled22/SignUp.dart';
import 'package:untitled22/TabBarViewHome.dart';
import 'package:untitled22/loginFile.dart';
import './Page_1.dart';
import './Page_2.dart';
import './Page_3.dart';
import './Page_4.dart';
import 'LanguageChangeProvider.dart';
import 'package:untitled22/generated/l10n.dart';


void main() {
  runApp(const MaterialApp(
    home: HomePage(),
    debugShowCheckedModeBanner: false,
  ));
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int pageNumb = 0;
  late TabController _controller;


  String _currentLanguage = 'ar';
  bool rtl = false;

  @override
  void initState() {
    _loadCounter();
    super.initState();
  }

  _loadCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      _currentLanguage = (prefs.getString('currentLang') ?? '');
      rtl = (prefs.getBool('isRtl') ?? false);
    });
  }
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      locale: new Locale(_currentLanguage),
      localizationsDelegates: [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,
      home: BodyBody(),
      debugShowCheckedModeBanner: false,
      showSemanticsDebugger: false,
            );
          }
        }

        class BodyBody extends StatefulWidget {
          const BodyBody({Key? key}) : super(key: key);

          @override
          State<BodyBody> createState() => _BodyBodyState();
        }
    
        class _BodyBodyState extends State<BodyBody> with SingleTickerProviderStateMixin  {

          int pageNumb = 0;
          late TabController _controller;


          String _currentLanguage = '';
          bool rtl = false;

  @override
  void initState() {
    _loadCounter();
    super.initState();
    _controller = TabController(vsync: this, length: 3, initialIndex: 1);
  }

  _loadCounter() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    setState(() {
      _currentLanguage = (prefs.getString('currentLang') ?? 'ar');
      rtl = (prefs.getBool('isRtl') ?? false);
    });
  }

  @override
  Widget build(BuildContext context) {

    Size media = MediaQuery.of(context).size;
    var height = AppBar().preferredSize.height;
    TextEditingController textController = TextEditingController();

    return DefaultTabController(
      initialIndex: 1,
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          titleSpacing: 0,
          title: Text(
            S.of(context).Home_drawer_mainMenu,
         
        style: TextStyle(
          color: Color(0xFF434343),
          fontSize: rtl == true ? 16 : 14,
        ),
      ),
      backgroundColor: const Color(0xff26c6da),
      leading: Builder(
        builder: (BuildContext context) {
          return Container(
            child: IconButton(
              icon: const Icon(Icons.menu),
              onPressed: () {
                Scaffold.of(context).openDrawer();
              },
              color: const Color(0xFF434343),
            ),
          );
        },
      ),
      actions: <Widget>[
        IconButton(
          onPressed: () {
            print(height);
          },
          icon: Icon(Icons.search),
          padding: rtl == false ? const EdgeInsets.only(right: 10) : const EdgeInsets.only(left: 10),
          color: const Color(0xFF434343),
        ),
        IconButton(
          onPressed: () {},
          icon: Icon(Icons.settings),
          padding: rtl == false ? const EdgeInsets.only(right: 15) : const EdgeInsets.only(left: 15),
          color: const Color(0xFF434343),
        ),
      ],

      bottom: TabBar(
        controller: _controller,
        labelColor: Color(0xff434343),
        unselectedLabelColor: Color(0xff434343),
        indicatorColor: Color(0xff434343),
        automaticIndicatorColorAdjustment: false,
        tabs: <Widget>[
          Tab(
            icon: Icon(Icons.event_note_outlined),
          ),
          Tab(
            icon: Icon(Icons.home_rounded),
          ),
          Tab(
            icon: Icon(Icons.add_alert),
          ),
        ],
      ),
    ),
    body: TabBarView(
      controller: _controller,
      children: [
        TabBarHome(width: media.width, height: media.height),
        TabBarHome(width: media.width, height: media.height),
        TabBarHome(width: media.width, height: media.height),
      ],
    ),
    drawer: Drawer(
      child: ListView(
        children: [
          GestureDetector(
            onTap: () {
              print('hey');
            },
            child: InkWell(
              onTap: () {},
              child: Container(
                color: Colors.grey.shade100,
                height: media.height * 0.2,
                child: Center(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 0.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Container(
                              height: 70,
                              width: 90,
                              decoration: BoxDecoration(
                                  shape: BoxShape.circle,
                                  image: DecorationImage(
                                    fit: BoxFit.fitWidth,
                                    image: AssetImage("images/aga.png"),
                                  )),
                            ),
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment:
                              CrossAxisAlignment.start,
                              children: [
                                Text(
                                  "Text",
                                  style: TextStyle(
                                    fontSize: 17,
                                    color: Colors.black87,
                                    fontWeight: FontWeight.w600,
                                  ),
                                ),
                                Text(
                                  "The best leader",
                                  textAlign: TextAlign.justify,
                                  style: TextStyle(
                                    fontSize: 15,
                                    color: Colors.black87,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                        Container(
                          child: rtl == true ? Icon(Icons.keyboard_arrow_right_rounded) : Icon(Icons.keyboard_arrow_left_rounded),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ),
          ),
          Padding(
            padding: EdgeInsets.all(10),
            child: Text(
              S.of(context).Home_drawer_mainMenu,
              style: TextStyle(fontSize: 17, fontWeight: FontWeight.w600),
            ),
          ),
          ListTile(
            leading: Icon(Icons.home),
            title: Text('data'),
          ),
          ListTile(
            leading: Icon(Icons.add_call),
            title: Text('call with us'),
          ),
        ],
      ),
    ),
  ),
);
      }
    }
0 Answers
Related