This time, the internationalization of Chinese and English will be realized first. I use vscode development, so only talk about how to achieve vscode;

Vscode installs the Flutter Intl plugin

Step 2 Configure pubspec.yaml

  flutter_localizations:
    sdk: flutter
  flutter_intl:
    enabled: true
Copy the code

Create the generated directory and I10n directory in the lib directory.

Step 4 Add the corresponding INTl_en. arb and intl_en.arb files to the I10n directory, and add the corresponding JSON files

Step 5 Make the following changes to the main.dart file

import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; // flutter_intl is the project name defined by me. Import 'package:flutter_intl/generated/l10n.dart' can be changed as required. void main() { runApp(MyApp()); } ValueChanged<Locale> localeChange; class MyApp extends StatefulWidget { @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State<MyApp> {// new sets the current Locale _locale = const Locale('en', ''); @override void initState() { super.initState(); localeChange = (locale) { setState(() { _locale = locale; }); }; } // new @override Widget build(BuildContext context) { return MaterialApp( title: 'project', theme: ThemeData.dark(), debugShowCheckedModeBanner: false, localizationsDelegates: const [ S.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate ], supportedLocales: S.delegate.supportedLocales, home: Builder( builder: (BuildContext context) { return Localizations.override( context: context, locale: _locale, child: Scaffold( appBar: AppBar( title: CenterTitle: true,), body: Center(child: Text(S.of(context).app_name), ), ), ); },),); }}Copy the code

Step 6 Check the results

Modify the following information in mian. DartAnd then restart the project

Switching languages involves data sharing, which is controlled by localeChange(Locale(‘zh’, ‘))