Flutter - juheekim1217/dev-note GitHub Wiki
This message means that your project already has an l10n.yaml
file, and Flutter will use the options defined in it instead of command-line arguments.
π How to Proceed?
l10n.yaml
and Generate Localization
β
Option 1: Keep If you want to keep l10n.yaml
, simply run:
flutter gen-l10n
This will generate app_localizations.dart
based on your l10n.yaml
configuration.
l10n.yaml
and Use Command-Line Arguments
β
Option 2: Remove If you want to manually control the localization generation using CLI, delete l10n.yaml
from the project root:
rm l10n.yaml
Then, regenerate the localization files using:
flutter gen-l10n --arb-dir=lib/l10n --output-dir=lib/gen/l10n
This command ensures:
.arb
files are read fromlib/l10n/
- Generated Dart localization files are placed in
lib/gen/l10n/
π Which Option Should You Choose?
- If you want consistency β Keep
l10n.yaml
and useflutter gen-l10n
. - If you prefer manual control β Delete
l10n.yaml
and use the CLI options.
π‘ Recommended Approach: Keeping l10n.yaml
is best for long-term maintenance, as it ensures a standardized localization setup across the project. π
If you don't have app_localizations.dart
, you need to generate it manually using Flutter's localization tool.
app_localizations.dart
π Steps to Generate pubspec.yaml
1οΈβ£ Check Your Make sure your pubspec.yaml
contains the required dependencies:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: 0.20.2
Also, make sure that flutter generate: true
is enabled:
flutter:
generate: true
l10n.yaml
(Localization Config File)
2οΈβ£ Create If you don't have l10n.yaml
in the root folder of your project, create one with the following content:
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
output-dir: lib/gen/l10n
synthetic-package: false
nullable-getter: false
.arb
Files)
3οΈβ£ Create Your Localization Files (Inside lib/l10n/
, create the following two .arb
files:
π lib/l10n/app_en.arb
{
"hello": "Hello",
"welcome_message": "Welcome to Jippin!"
}
π lib/l10n/app_ko.arb
{
"hello": "μλ
νμΈμ",
"welcome_message": "Jippinμ μ€μ κ²μ νμν©λλ€!"
}
4οΈβ£ Generate the Localization Files
Run the following command in the terminal:
flutter gen-l10n
This will generate the missing app_localizations.dart
inside:
/lib/gen/l10n/app_localizations.dart
5οΈβ£ Import and Use Localization in Your Flutter App
Modify main.dart
:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'gen/l10n/app_localizations.dart'; // Import generated file
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Jippin',
locale: Locale('en'), // Default language
supportedLocales: [
Locale('en'), // English
Locale('ko'), // Korean
],
localizationsDelegates: [
AppLocalizations.delegate, // Generated localization
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(AppLocalizations.of(context)!.hello)),
body: Center(child: Text(AppLocalizations.of(context)!.welcome_message)),
);
}
}
β Final Checklist
βοΈ Ensure flutter_localizations
and intl
are in pubspec.yaml
βοΈ Check flutter generate: true
is enabled
βοΈ Create l10n.yaml
with correct settings
βοΈ Create .arb
files inside lib/l10n/
βοΈ Run flutter gen-l10n
to generate app_localizations.dart
βοΈ Import and use AppLocalizations.of(context)!
in your widgets
After following these steps, app_localizations.dart
will be generated, and your app will support localization! ππ
AdaptiveNavBar
?
Why Is There a "Go Back" Arrow in When you use Navigator.pushNamed(context, '/submit')
, it pushes the new page onto the navigation stack. In Flutter, when a new page is pushed onto the stack, Flutter automatically adds a back button (β) in the AppBar if the screen has an AppBar
or uses AdaptiveNavBar
. This happens because:
Navigator.pushNamed
creates a new route on top of the current one.- Flutter automatically enables back navigation unless explicitly disabled.
π How to Navigate Without Creating a Back Button
β Solution 1: Replace the Current Page Instead of Pushing
Instead of pushing a new page on top of the current one (Navigator.pushNamed
), use Navigator.pushReplacementNamed
, which replaces the current page.
Navigator.pushReplacementNamed(context, '/submit');
πΉ What this does:
- It removes the current page from the stack and replaces it with the new one.
- This means there is no back button because the previous page no longer exists in the stack.
Navigator.popAndPushNamed
(Clears History)
β
Solution 2: Use If you want to clear all previous pages and navigate to a fresh page, use:
Navigator.popAndPushNamed(context, '/submit');
πΉ What this does:
- It first removes the current page (
pop
) and then navigates (pushNamed
). - Similar to
pushReplacementNamed
, it ensures no back button appears.
Navigator.pushNamedAndRemoveUntil
(Clear All Previous Pages)
β
Solution 3: Use If you want to completely reset the navigation stack (e.g., after login or form submission):
Navigator.pushNamedAndRemoveUntil(context, '/submit', (route) => false);
πΉ What this does:
- It removes all previous routes and only shows the
/submit
page. - There will be no back button since there's no page to go back to.
π Best Approach for Your Case
If you donβt want the back button when navigating to /submit
, the best option is:
Navigator.pushReplacementNamed(context, '/submit');
This ensures smooth navigation while avoiding unnecessary back buttons in the AdaptiveNavBar
. π
Let me know if you need further tweaks! ππ₯