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?

βœ… Option 1: Keep l10n.yaml and Generate Localization

If you want to keep l10n.yaml, simply run:

flutter gen-l10n

This will generate app_localizations.dart based on your l10n.yaml configuration.


βœ… Option 2: Remove l10n.yaml and Use Command-Line Arguments

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 from lib/l10n/
  • Generated Dart localization files are placed in lib/gen/l10n/

πŸš€ Which Option Should You Choose?

  • If you want consistency β†’ Keep l10n.yaml and use flutter 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.


πŸ›  Steps to Generate app_localizations.dart

1️⃣ Check Your pubspec.yaml

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

2️⃣ Create l10n.yaml (Localization Config File)

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

3️⃣ Create Your Localization Files (.arb 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! πŸš€πŸŽ‰

Why Is There a "Go Back" Arrow in AdaptiveNavBar?

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.

βœ… Solution 2: Use Navigator.popAndPushNamed (Clears History)

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.

βœ… Solution 3: Use Navigator.pushNamedAndRemoveUntil (Clear All Previous Pages)

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! 😊πŸ”₯