<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mouhaned Akermi</title>
    <description>The latest articles on DEV Community by Mouhaned Akermi (@mouhaned_akermi).</description>
    <link>https://dev.to/mouhaned_akermi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1553320%2F245f83d6-4dbc-4121-956b-bbe7c4c5955d.jpg</url>
      <title>DEV Community: Mouhaned Akermi</title>
      <link>https://dev.to/mouhaned_akermi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mouhaned_akermi"/>
    <language>en</language>
    <item>
      <title>Custom Render Boxes and Painting in Flutter</title>
      <dc:creator>Mouhaned Akermi</dc:creator>
      <pubDate>Wed, 07 Aug 2024 09:51:13 +0000</pubDate>
      <link>https://dev.to/mouhaned_akermi/custom-render-boxes-and-painting-in-flutter-33l9</link>
      <guid>https://dev.to/mouhaned_akermi/custom-render-boxes-and-painting-in-flutter-33l9</guid>
      <description>&lt;p&gt;Flutter is known for its flexibility and performance in building user interfaces. While its widget system covers most needs, there are scenarios where you may want to go beyond standard widgets to achieve highly customized and performant UIs. In such cases, you can use custom render boxes and painting. This article explores how to create custom render boxes and painting classes in Flutter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Understanding Flutter’s Rendering Pipeline&lt;/strong&gt;&lt;br&gt;
Flutter’s rendering pipeline consists of several layers, with the RenderObject layer being the most fundamental. Widgets describe the configuration of the UI, Elements manage the lifecycle, and RenderObjects handle the actual layout and painting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use Custom Render Boxes&lt;/strong&gt;&lt;br&gt;
Custom render boxes are useful when you need:&lt;/p&gt;

&lt;p&gt;Highly optimized performance for complex layouts.&lt;br&gt;
Precise control over layout and painting.&lt;br&gt;
To create custom widgets that aren’t achievable with the existing Flutter widgets.&lt;br&gt;
&lt;strong&gt;Creating a Custom Render Box&lt;/strong&gt;&lt;br&gt;
To create a custom render box, you extend the RenderBox class and override its methods. Here's an example of a simple custom render box that draws a colored rectangle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Define the Custom Render Box&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/rendering.dart';

class ColoredBox extends RenderBox {
  Color color;

  ColoredBox({required this.color});

  @override
  void paint(PaintingContext context, Offset offset) {
    final paint = Paint()..color = color;
    context.canvas.drawRect(offset &amp;amp; size, paint);
  }

  @override
  void performLayout() {
    size = constraints.biggest;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Create a Custom Widget&lt;/strong&gt;&lt;br&gt;
Next, create a widget that uses the custom render box.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class ColoredBoxWidget extends LeafRenderObjectWidget {
  final Color color;

  ColoredBoxWidget({required this.color});

  @override
  RenderObject createRenderObject(BuildContext context) {
    return ColoredBox(color: color);
  }

  @override
  void updateRenderObject(BuildContext context, RenderObject renderObject) {
    (renderObject as ColoredBox).color = color;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Use the Custom Widget&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Custom Render Box')),
        body: Center(
          child: ColoredBoxWidget(color: Colors.blue),
        ),
      ),
    );
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Custom Painting with Render Boxes&lt;/strong&gt;&lt;br&gt;
Custom painting involves using the Canvas class to draw graphics. This can include shapes, text, images, and more. In the ColoredBox example, we used the Canvas.drawRect method to paint a rectangle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Painting Example&lt;/strong&gt;&lt;br&gt;
Let’s create a custom render box that draws a gradient-filled circle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Define the Custom Render Box&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/rendering.dart';
import 'dart:ui' as ui;

class GradientCircleBox extends RenderBox {
  final ui.Gradient gradient;

  GradientCircleBox({required this.gradient});

  @override
  void paint(PaintingContext context, Offset offset) {
    final paint = Paint()..shader = gradient.createShader(offset &amp;amp; size);
    context.canvas.drawCircle(offset + Offset(size.width / 2, size.height / 2), size.shortestSide / 2, paint);
  }

  @override
  void performLayout() {
    size = constraints.biggest;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Create a Custom Widget&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class GradientCircleWidget extends LeafRenderObjectWidget {
  final Gradient gradient;

  GradientCircleWidget({required this.gradient});

  @override
  RenderObject createRenderObject(BuildContext context) {
    return GradientCircleBox(gradient: gradient);
  }

  @override
  void updateRenderObject(BuildContext context, RenderObject renderObject) {
    (renderObject as GradientCircleBox).gradient = gradient;
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Use the Custom Widget&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Custom Gradient Circle')),
        body: Center(
          child: GradientCircleWidget(
            gradient: LinearGradient(
              colors: [Colors.blue, Colors.green],
            ),
          ),
        ),
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;**Optimizing Custom Render Boxes&lt;br&gt;
**When creating custom render boxes, consider the following optimization tips:&lt;/p&gt;

&lt;p&gt;1-&lt;em&gt;Minimize Layout Passes&lt;/em&gt;: Ensure your layout logic is efficient. Avoid unnecessary calls to markNeedsLayout.&lt;br&gt;
2-&lt;em&gt;Efficient Painting&lt;/em&gt;: Use the Canvas class efficiently. Cache reusable resources, like Paint objects, to avoid creating them repeatedly.&lt;br&gt;
3-&lt;em&gt;Handle Constraints Properly&lt;/em&gt;: Ensure your render box handles constraints correctly to avoid layout issues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Custom render boxes and painting provide powerful tools for creating highly customized and performant Flutter UIs. By understanding and leveraging Flutter’s rendering pipeline, you can achieve effects and optimizations not possible with standard widgets. Whether you’re creating a unique UI component or optimizing performance, custom render boxes and painting are essential skills for advanced Flutter development.&lt;/p&gt;

&lt;p&gt;Happy coding, and enjoy the flexibility and power of Flutter’s rendering system!&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>softwaredevelopment</category>
      <category>android</category>
      <category>mobileappdevelopment</category>
    </item>
    <item>
      <title>Flutter Understanding Internationalization and Localization in Flutter</title>
      <dc:creator>Mouhaned Akermi</dc:creator>
      <pubDate>Fri, 02 Aug 2024 07:21:14 +0000</pubDate>
      <link>https://dev.to/mouhaned_akermi/flutter-understanding-internationalization-and-localization-in-flutter-4d04</link>
      <guid>https://dev.to/mouhaned_akermi/flutter-understanding-internationalization-and-localization-in-flutter-4d04</guid>
      <description>&lt;p&gt;In the increasingly globalized world, it’s essential for mobile applications to cater to diverse audiences by supporting multiple languages and regional preferences. This process involves two key concepts: Internationalization (i18n) and Localization (l10n). In this article, we’ll explore what these terms mean, their importance, and how to implement them in a Flutter application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Internationalization (i18n)?&lt;/strong&gt;&lt;br&gt;
Internationalization refers to the process of designing an application to be easily adaptable to various languages and regions without requiring changes to the codebase. It involves:&lt;/p&gt;

&lt;p&gt;Separating Content from Code: All user-facing text, dates, numbers, and other locale-specific data are externalized, typically stored in resource files. This separation allows developers to add or update languages without altering the core application logic.&lt;br&gt;
Supporting Multiple Languages: The application must support switching between different languages and formats. This is typically done by loading different sets of resources based on the user’s locale.&lt;br&gt;
Locale-Specific Formatting: Properly handling formats for dates, numbers, currencies, etc., according to local customs and conventions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Localization (l10n)?&lt;/strong&gt;&lt;br&gt;
Localization is the process of adapting an application for a specific language or region. It includes:&lt;/p&gt;

&lt;p&gt;Translating Text: Converting all user-visible text into the target language(s). This process involves creating and maintaining translation files that contain all the necessary translations.&lt;br&gt;
Adapting Layouts: Adjusting UI elements, such as text fields, buttons, and icons, to accommodate different text lengths and reading directions (e.g., left-to-right or right-to-left).&lt;br&gt;
Cultural Customization: Making culturally appropriate adjustments, such as changing imagery, colors, or symbols, to better resonate with the target audience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Are Internationalization and Localization Important?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Broader Reach&lt;/strong&gt;&lt;br&gt;
By supporting multiple languages and regions, your app can reach a global audience. This not only expands your user base but also enhances user satisfaction by providing a personalized experience.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Legal and Cultural Compliance&lt;/strong&gt;&lt;br&gt;
Different regions have different laws, norms, and cultural sensitivities. Localizing your app ensures that it complies with these local standards and meets the expectations of your users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Competitive Advantage&lt;/strong&gt;&lt;br&gt;
Offering a localized experience can set your app apart from competitors who may not support multiple languages. This can be a key differentiator, especially in regions where users expect apps to be available in their native language.&lt;/p&gt;

&lt;p&gt;Implementing Internationalization and Localization in Flutter&lt;br&gt;
Flutter, Google’s UI toolkit for building natively compiled applications, provides robust support for internationalization and localization. Here’s a step-by-step guide to implementing these features in a Flutter app:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Adding Dependencies&lt;/strong&gt;&lt;br&gt;
First, add the necessary dependencies to your pubspec.yaml file. The most commonly used package for internationalization in Flutter is intl.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: ^0.17.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Defining Supported Locales&lt;/strong&gt;&lt;br&gt;
In the MaterialApp widget, specify the supported locales and the localization delegates. The delegates are responsible for loading the appropriate translations and formatting rules based on the current locale.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'l10n/app_localizations.dart';

void main() =&amp;gt; runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: [
        Locale('en', ''), // English
        Locale('es', ''), // Spanish
      ],
      localizationsDelegates: [
        AppLocalizations.delegate, // Custom localization delegate
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      home: MyHomePage(),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Creating Translation Files&lt;/strong&gt;&lt;br&gt;
For each supported language, create an ARB (Application Resource Bundle) file. These files contain key-value pairs, where the keys are the identifiers used in the code, and the values are the corresponding translations.&lt;/p&gt;

&lt;p&gt;Example: intl_en.arb (English)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "appTitle": "My App",
  "helloWorld": "Hello, World!"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example: intl_es.arb (Spanish)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "appTitle": "Mi Aplicación",
  "helloWorld": "¡Hola, Mundo!"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Loading and Using Translations
Create a custom localization class that extends LocalizationsDelegate and provides methods to load and access the translations. Use this class in your widgets to display localized text.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class AppLocalizations {
  AppLocalizations(this.locale);

  final Locale locale;

  static AppLocalizations of(BuildContext context) {
    return Localizations.of&amp;lt;AppLocalizations&amp;gt;(context, AppLocalizations);
  }

  static const LocalizationsDelegate&amp;lt;AppLocalizations&amp;gt; delegate = _AppLocalizationsDelegate();

  Map&amp;lt;String, String&amp;gt; _localizedStrings;

  Future&amp;lt;bool&amp;gt; load() async {
    String jsonString = await rootBundle.loadString('lib/l10n/intl_${locale.languageCode}.arb');
    Map&amp;lt;String, dynamic&amp;gt; jsonMap = json.decode(jsonString);

    _localizedStrings = jsonMap.map((key, value) {
      return MapEntry(key, value.toString());
    });

    return true;
  }

  String translate(String key) {
    return _localizedStrings[key];
  }
}

class _AppLocalizationsDelegate extends LocalizationsDelegate&amp;lt;AppLocalizations&amp;gt; {
  const _AppLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) {
    return ['en', 'es'].contains(locale.languageCode);
  }

  @override
  Future&amp;lt;AppLocalizations&amp;gt; load(Locale locale) async {
    AppLocalizations localizations = new AppLocalizations(locale);
    await localizations.load();
    return localizations;
  }

  @override
  bool shouldReload(_AppLocalizationsDelegate old) =&amp;gt; false;
}`
To use the translations in your widgets:

`@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      title: Text(AppLocalizations.of(context).translate('appTitle')),
    ),
    body: Center(
      child: Text(AppLocalizations.of(context).translate('helloWorld')),
    ),
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Testing and Deployment&lt;/strong&gt;&lt;br&gt;
Ensure that your app is thoroughly tested for each supported language and region. Pay attention to text length, layout issues, and cultural nuances. Once everything is in place, you can deploy your localized app to the respective app stores.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Internationalization and localization are crucial for making your Flutter app accessible and appealing to a global audience. By following best practices and using Flutter’s built-in tools, you can create a seamless and culturally relevant experience for users around the world.&lt;/p&gt;

&lt;p&gt;Incorporating i18n and l10n may require an initial investment of time and effort, but the benefits of reaching a broader audience and providing a better user experience make it well worth it. So, take the plunge and make your Flutter app a truly global product!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thank you ❤️&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>mobile</category>
      <category>softwaredevelopment</category>
      <category>localization</category>
    </item>
  </channel>
</rss>
