[cleanest way] How To Restart App After change Theme or Language in a flutter

how can i refresh application current screen after changing language in flutter?

 

How can I refresh application current screen after changing language in flutter?

To restart your Flutter app you have many ways, But before that, you have to know why you want to restart the app, is it to restart the main? or just to rebuild the app after changing the language or theme, let me tell you solutions that would work with you and what the [ CLEANEST WAY ]


Method 1

phoenix_native Package

Using external packages will restart the app natively is the phoenix_native package.

Just call :

    PhoenixNative.restartApp();

restart_app Package

Also, you have another plugin that helps you to restart the whole Flutter app with a single function call by using Native APIs. called restart_app

by calling this 

onPressed: () {
  /// Fill webOrigin only when your new origin is different than the app's origin
  Restart.restartApp(webOrigin: '[your main route]');
}


Note: this is not Full App Restart for IOS.

To restart your Flutter application on iOS, due to the platform's limitations, the app will exit and send a local notification to the user. The user can then tap this notification to reopen the app. This is not a full app restart, but it's the closest workaround possible on iOS


Method 2 

RestartWidget

You can use this reusable code to restart the MaterialApp by:

1. Wrap MaterialApp with it

2. call this 

RestartWidget.restartApp(context);

NOTE:  RestateWidget not working if you have routing NavigationKey in MaterialApp


Full Code:

import 'package:flutter/material.dart';


class RestartWidget extends StatefulWidget {
  final Widget child;
  const RestartWidget({super.key, required this.child});


  static restartApp(BuildContext context) {
    final _RestartWidgetState? state =
    context.findAncestorStateOfType<_RestartWidgetState>();
    if(state != null){state.restartApp();
  }


  @override
  State<RestartWidget> createState() => _RestartWidgetState();
}

class _RestartWidgetState extends State<RestartWidget> {
  Key key =  UniqueKey();

  void restartApp() {

    setState(() {
      key =  UniqueKey();
    });
  }

  @override
  Widget build(BuildContext context) {
    return KeyedSubtree(
      key: key,
      child: widget.child,
    );
  }
}


Method 3 

The Cleanest Way

I love using this method to restart my app to the first widget in the app AppRoot().

The Thing is that this is Flutter code and no external Packages. 


1. After I change the settings with my cubit for example like this:

//1. this to make my change ex: change language
await context.read<SettingCubit>().setAppLanguage(value: 'ar');

2. Use this to go back to the first route and restart it by renavigate to it again, this do the job and keeps my code very clean

//2. Go back till no route (Restart)
Navigator.of(context).popUntil((route) => false);

// Navigate to first widget to the app **AppRoot()**  
Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => const AppRoot()),
);







Tags

#buttons=(Accept !) #days=(14)

Our website uses cookies to enhance your experience. Learn More
Accept !