Home > Net >  Flutter - How to redirect to Homepage after user choose app's language?
Flutter - How to redirect to Homepage after user choose app's language?

Time:08-11

I'm building an app with Flutter source code. There is a menu name Change Language to let users can change app's language.

After they pressed menu they will see a list of available languages then choosing a language. Nothing happen after choosing one nothing happen => they still see language list screen.

Now I want to users be redirected to homepage after choosing a language. How can I do that?

Here is the code to control language choosing.

Thank you very much!!!

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:myproject/constant/ps_dimens.dart';
import 'package:myproject/constant/route_paths.dart';
import 'package:myproject/provider/language/language_provider.dart';
import 'package:myproject/repository/language_repository.dart';
import 'package:myproject/ui/common/ps_admob_banner_widget.dart';
import 'package:myproject/ui/common/ps_dropdown_base_widget.dart';
import 'package:myproject/utils/utils.dart';
import 'package:myproject/viewobject/common/language.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:provider/provider.dart';

class LanguageSettingView extends StatefulWidget {
  const LanguageSettingView(
      {Key? key,
      required this.animationController,
      required this.languageIsChanged})
      : super(key: key);
  final AnimationController? animationController;
  final Function languageIsChanged;
  @override
  _LanguageSettingViewState createState() => _LanguageSettingViewState();
}

class _LanguageSettingViewState extends State<LanguageSettingView> {
  String currentLang = '';
  LanguageRepository? repo1;

  bool isConnectedToInternet = false;
  bool isSuccessfullyLoaded = true;

  void checkConnection() {
    Utils.checkInternetConnectivity().then((bool onValue) {
      isConnectedToInternet = onValue;
      if (isConnectedToInternet && true) {
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    if (!isConnectedToInternet && true) {
      print('loading ads....');
      checkConnection();
    }
    final Animation<double> animation = Tween<double>(begin: 0.0, end: 1.0)
        .animate(CurvedAnimation(
            parent: widget.animationController!,
            curve: const Interval(0.5 * 1, 1.0, curve: Curves.fastOutSlowIn)));
    widget.animationController!.forward();
    // final dynamic data = EasyLocalizationProvider.of(context).data;
    final LanguageRepository repo1 = Provider.of<LanguageRepository>(context);
    // return EasyLocalizationProvider(
    //   data: data,
    //   child:
    return ChangeNotifierProvider<LanguageProvider>(
      lazy: false,
      create: (BuildContext context) {
        final LanguageProvider provider = LanguageProvider(repo: repo1);
      //  provider.getLanguageList();
        return provider;
      },
      child: Consumer<LanguageProvider>(builder:
          (BuildContext context, LanguageProvider provider, Widget? child) {
        return AnimatedBuilder(
            animation: widget.animationController!,
            child: SingleChildScrollView(
                child: Container(
              padding: const EdgeInsets.all(PsDimens.space8),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                mainAxisSize: MainAxisSize.max,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  PsDropdownBaseWidget(
                      title: Utils.getString(
                          context, 'language_selection__select'),
                      selectedText: provider.getLanguage().name,
                      onTap: () async {
                        //provider.getLanguageList(); 
                        final dynamic result = await Navigator.pushNamed(
                            context, RoutePaths.languageList);
                        if (result != null && result is Language) {
                          // EasyLocalization.of(context).set
                          // await data.changeLocale(result.toLocale());

                          // EasyLocalization.of(context).

                         await provider.addLanguage(result);
                         EasyLocalization.of(context)?.setLocale( Locale(result.languageCode!, result.countryCode));
                        }
                        Utils.psPrint(result.toString());
                      }),
                  const PsAdMobBannerWidget(
                    admobSize: AdSize.mediumRectangle,
                    // admobBannerSize: AdmobBannerSize.MEDIUM_RECTANGLE,
                  ),
                ],
              ),
            )),
            builder: (BuildContext context, Widget? child) {
              return FadeTransition(
                  opacity: animation,
                  child: Transform(
                      transform: Matrix4.translationValues(
                          0.0, 100 * (1.0 - animation.value), 0.0),
                      child: child));
            });
      }),
      // ),
    );
  }
}

CodePudding user response:

It is very simple use Navigator for routing follow these steps

  1. from home page push the language page.

    onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => LanguageSettingView()), ); }

  2. Once the user select the language call navigator pop method with chosen language Something like

    Navigator.pop(context, 'English');

How to use routing using navigator check flutter official documentation

https://docs.flutter.dev/cookbook/navigation/navigation-basics#2-navigate-to-the-second-route-using-navigatorpush

All the best !!!

  • Related