Google search engine
Home Blog Page 10

Records In Dart

0

Dart Records is a thrilling new feature presented in Dart language version 3.0. They give an elegant method for characterizing anonymous, changeless, and composed total types.

Records permit you to proficiently and briefly make anonymous composite values from any current data. At the end of the day, you can return numerous values from a function without making a model/temporary class for it.

This article will explore the Records In Dart. We will execute a demo program and how to use dart records in your applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.

Table Of Contents::

What are Records?

Syntax of Records

Retrieve Record Values in Dart

Code Implement

Conclusion



What are Records?

Records are a kind of total data structure in Dart that permits you to package various items together into a solitary, fixed-size, and composed element. Not at all like other assortment types, for example, lists or maps, records are changeless and heterogeneous, meaning they can contain fields of various kinds. This makes them especially helpful for addressing organized information in your Dart programs.

Syntax of Records:

Record syntax structure in Dart is clear. Record articulations are comma-delimited arrangements of named or positional fields, encased in brackets. Here is a model for better comprehension:

var record = ('first', a: 2, b: true, 'last');

In this code bit, we make and characterize a variable record utilizing a blend of positional and named fields. Then again, Dart gives record-type explanations to characterize return types and parameter types. These explanations are additionally comma-delimited arrangements of types encased in brackets. Take in the accompanying model:

(int, int) swap((int, int) record) {
 var (a, b) = record;
 return (b, a);
}

Here, the swap function acknowledges a record made out of two integer values and returns a record with these values exchanged.

Retrieve Record Values in Dart

The proficiency of Dart records focuses light on how we can retrieve record values. After we have put stored values in our Dart records, we can recover these stored record values utilizing worked-in getters. In Dart records, each field in the record can uncover its getter, furnishing us with a helpful method for getting to the substance inside our records.

Here is an illustrative model featuring how to accomplish this:

void main() {
  var record = ('first', a: 2, b: true, 'last');

  print(record.$1); // Prints 'first'
  print(record.a); // Prints 2
  print(record.b); // Prints true
  print(record.$2); // Prints 'last'
}

We show the utilization of getters to retrieve record values. For this situation, $1 gets to the main positional field, investigating a and b gets to the value of the named fields, and $2 gets the worth of the following positional field skirting the named fields.

Code Implement:

Using Records is quite simple. You can return name and age together from our function as follows:

(String name, String age) getData() {
return ('Tester'.toUpperCase(),'28'.toUpperCase());
}
void main(List<String> args) {

//Using Records
final (name,age)=getData();
print('Name: $name$age');

}

Records simplify our life. We don’t need to make model classes with 2-3 variables. We can just involve Records for this reason.

Conclusion:

In the article, I have explained the Records In Dart; you can modify this code according to your choice. This was a small introduction to the Records In Dart User Interaction from my side, and it’s working using Flutter.

Dart Records are a strong element that empowers you to address organized information in a concise and type-safe way. By understanding their syntax structure and semantics, you can use Records to work on the clarity and dependability of your Dart code.

I hope this blog will provide you with basic information on Trying the Records In Dart of your projects. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Debugging Flutter Apps: Tips and Tricks for Efficient Problem-Solving

0

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.

Introduction

Debugging is a crucial skill in a developer’s toolkit, ensuring applications perform seamlessly and deliver exceptional user experiences. Flutter — a leading framework for building expressive UIs across platforms — has powerful debugging tools and practices to resolve issues effectively. This guide explores technical tips and advanced strategies to debug Flutter apps, tackling UI glitches, performance bottlenecks, network errors, and more.


Table of Contents

Introduction

Debugging in Flutter

Building a Comprehensive Debugging Toolkit

Essential Debugging Tools for Flutter

Structured Logging and Error Reporting with Advanced Tools

Debugging UI and Rendering Issues

Advanced Debugging Techniques

Debugging on Specific Platforms

Future-Ready Debugging

Conclusion


Debugging in Flutter

What is Debugging?

Debugging is identifying, analyzing, and resolving errors in an application to ensure it functions as intended. In Flutter, bugs can take many forms, including:

  • Runtime Errors: Crashes or exceptions during app execution.
  • UI Issues: Misaligned widgets, overflow errors, or rendering problems.
  • Performance Bottlenecks: Frame drops, slow animations, or high CPU usage.
  • Logic Errors: Faulty algorithms or incorrect data processing.
  • State Management Errors: Inconsistent state transitions or unexpected behaviors.
  • Network and API Issues: Incorrect responses, slow data fetching, or failed calls.
Why Debugging Matters

Effective debugging ensures:

  • Enhanced User Experience: Smooth functionality, fewer crashes, and visually appealing UIs.
  • Optimized Performance: Efficient resource utilization and fast load times.
  • Reduced Development Time: Quick identification and resolution of problems.

Building a Comprehensive Debugging Toolkit

IDE Setup and Debugger Tools

Leverage powerful IDEs like Visual Studio Code or Android Studio with integrated debugging support.

Features:

  • Breakpoint support for stopping execution at specific lines.
  • Step-by-step execution for detailed analysis.
  • Real-time variable inspection.

Advanced Tips:

  • Use Conditional Breakpoints: Add conditions for stopping execution only when certain criteria are met.
  • Debug asynchronous code with Dart’s Future Call Stack, which traces async methods effectively.

Example: Setting a Breakpoint in VS Code

  1. Open your Dart file.
  2. Click on the gutter next to the line number to set a breakpoint.
  3. Start debugging with flutter run --debug.
  4. Inspect the variable values at runtime when the breakpoint is hit.

Essential Debugging Tools for Flutter

Flutter DevTools is indispensable for analyzing UI layouts, inspecting widget trees, and profiling performance.

Key Features and Usage

Flutter DevTools

Flutter DevTools is a comprehensive debugging suite that provides insights into your app’s behavior.

Key Features:

  1. Widget Inspector:
  • Explore and modify widget properties in real-time.
  • Diagnose layout issues and constraints problems.

2. Performance Profiler:

  • Analyze frame rendering performance and identify slow operations.
  • Detect excessive widget rebuilds or rendering layers.

3. Memory Analyzer:

  • Monitor memory usage and identify leaks.
  • Debug memory-intensive operations using Dart garbage collection.

4. Network Tab (Using Plugins):

  • Inspect API requests, responses, and timings using tools like flutter_flipperkit.

Usage Tip: Open DevTools from the terminal by running:

flutter pub global activate devtools
flutter pub global run devtools

Pro Tip: Use the Repaint Rainbow in DevTools to visualize excessive repaints, a common cause of performance drops:

MaterialApp(
debugShowCheckedModeBanner: false,
debugShowMaterialGrid: true,
home: MyApp(),
);

Example: Identifying UI Bugs with DevTools

If a widget’s layout is incorrect, use the Widget Inspector to:

  • Pinpoint misaligned widgets.
  • Adjust their properties in real time and validate the changes.

Structured Logging and Error Reporting with Advanced Tools

Logging is the backbone of effective debugging. Move beyond basic print() statements to more robust solutions:

Using the logger Package:

import 'package:logger/logger.dart';

final logger = Logger(
printer: PrettyPrinter(),
);

void fetchData() {
try {
logger.i('Fetching data...');
throw Exception('Network error');
} catch (e) {
logger.e('An error occurred', e);
}
}

Best Practices for Logging:

  • Use log levels (debug, info, error) for better organization.
  • Structure logs for automated analysis tools like Firebase Crashlytics.
  • Minimize logging in production builds using compile-time flags.

Crash Reporting and Analysis

Firebase Crashlytics:

  • Monitor crash logs and user traces in real time.
  • Add custom keys for contextual debugging:
FirebaseCrashlytics.instance.setCustomKey('screen', 'Login');

Sentry:

  • Track issues with detailed stack traces and session context.
  • Integrate breadcrumbs for user actions leading to errors.

 Debugging UI and Rendering Issues

Widget Debugging Techniques

1. Diagnosing Misalignments:

  • Enable layout overlays with debugPaintSizeEnabled = true;.
  • Use debugPrintMarkNeedsLayoutStacks to identify layout invalidations.

2. Debugging Overflow Errors:

  • Leverage the Layout Explorer in DevTools.
  • Identify and fix constraints issues with tools like Expanded, Flexible, and FittedBox.

The Debugging Power of Hot Reload and Hot Restart

  • Hot Reload: For UI changes and minor logic updates without losing app state.
  • Hot Restart: To refresh the entire application state.

Pro Tip: Use state restoration APIs to preserve app state during restarts for complex state management.

Debugging Performance Bottlenecks

Identifying Frame Drops

  • Enable the Performance Overlay by setting:
MaterialApp(
showPerformanceOverlay: true,
home: MyApp(),
);

Use DevTools’ Frame Chart to detect slow frames and optimize widget rebuilds.

Reducing Widget Rebuilds

  • Optimize Stateless Widgets: Use const constructors for widgets that don’t change.
  • Memoize Widgets: Use Provider or Bloc to minimize unnecessary widget rebuilding.

Profile Rendering Performance

  • Analyze rendering layers using the Render Tree Inspector.
  • Debug shader compilation and GPU usage with flutter build profile.

 Advanced Debugging Techniques

Debugging Asynchronous Code

Flutter heavily relies on asynchronous programming with Future and Stream. Debugging async operations can be tricky.

Tips:

  • Use debugger steps like step into and step over to trace async calls.
  • Inspect the Future Call Stack to trace where an exception was thrown.

Example: Handling async exceptions with Zone:

runZonedGuarded(() {
runApp(MyApp());
}, (error, stackTrace) {
print('Caught error: $error');
});

Debugging Network and API Calls

  1. Network Logging:
  • Use the Dio package with interceptors for monitoring API calls
dio.interceptors.add(LogInterceptor(responseBody: true));

2. Mock API Responses:

  • Simulate responses for offline debugging using tools like Mockoon or mockito.

3. Error Handling:

  • Use try-catch blocks for handling exceptions.
  • Implement global error handlers for unhandled API errors.

Debugging State Management

State management solutions like Provider, Bloc, and Riverpod can introduce complexity. Use these tools to debug state transitions:

  1. Provider:
  • Wrap widgets with Consumer for real-time state updates.
  • Use debugPrint to log state changes.

2. For apps using BLoC:

  • Implement a BlocObserver for tracking events and transitions:
class AppBlocObserver extends BlocObserver {
@override
void onEvent(Bloc bloc, Object? event) {
super.onEvent(bloc, event);
print('Bloc Event: $event');
}
}
  • Debug stream events using the RxDart Stream Inspector.

Debugging Widget Tree

  • Widget Constraints: Debug layout errors using debugPrintConstraints().
  • Debug Overflow Issues:
  • Enable debug flags like debugPaintSizeEnabled = true;.
  • Use the Layout Explorer in DevTools to visualize widget constraints and boundaries.

Debugging on Specific Platforms

Flutter Web

  • Use browser DevTools alongside Flutter Inspector.
  • Profile network calls with the Network tab in Chrome DevTools.

Flutter Desktop

  • Debug platform-specific issues using native tools like Instruments for macOS or Windows Event Viewer.

Flutter Embedded

  • Attach debuggers to the embedded device using the flutter attach command.

 Future-Ready Debugging

Embracing AI-Powered Debugging

  • Use AI tools like CodeWhisperer or Copilot to analyze and suggest fixes for error-prone code.
  • Explore AI-driven tools for log analysis to identify patterns in app crashes.

Debugging in Flutter Web and Desktop

  • Debug web performance using browser DevTools alongside Flutter’s Inspector.
  • Use platform-specific profiling tools like Instruments (macOS) or Windows Performance Recorder.

Automating Debugging with Testing

  • Write comprehensive test cases:
  • Unit Tests for logic validation.
  • Widget Tests for UI behavior.
  • Integration Tests for complete app flows.
  • Integrate CI/CD pipelines for automated issue tracking using GitHub Actions or Bitrise.

Conclusion

Debugging in Flutter is a multi-faceted process that evolves with advancements in tools and techniques. By leveraging Flutter DevTools, advanced logging, profiling, and state management tools, you can tackle issues effectively and ensure your app delivers a stellar user experience. Always stay updated with the latest debugging innovations and refine your debugging practices as Flutter evolves.

By mastering these tips and tricks, you’ll become a better developer and ensure your Flutter apps remain performant, reliable, and user-friendly in a competitive app ecosystem. Happy debugging! 🚀

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Customizing the Flutter Engine for Specific Use Cases

0

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table of Contents:

Introduction

Understanding the Flutter Engine Architecture

Why Customize the Flutter Engine?

Setting Up the Flutter Engine for Customization

Examples of Customization

Testing and Debugging Custom Engines

Deployment Considerations

Real-World Use Cases of Custom Engines

Challenges and Best Practices

Conclusion

References


Introduction:

The Flutter engine is a powerful piece of software that underpins the Flutter framework, enabling it to deliver cross-platform, high-performance applications. While the default engine is sufficient for most projects, there are scenarios where customization can unlock even more potential — whether for optimizing performance, adding new features, or targeting unconventional platforms. This blog explores the intricacies of customizing the Flutter engine, covering the reasons, process, use cases, and challenges involved.


Understanding the Flutter Engine Architecture

The Flutter engine acts as the foundation of the Flutter framework, providing the runtime environment and rendering capabilities that make Flutter apps possible. Here’s a breakdown of its key components:

  • Dart VM: The Dart virtual machine executes Dart code, providing features like Just-In-Time (JIT) compilation during development and Ahead-Of-Time (AOT) compilation for production builds. The Dart VM ensures that Flutter apps can be both performant and dynamic.
  • Skia: Skia is a 2D graphics library the Flutter engine uses to render UI components. It supports hardware-accelerated graphics and handles everything from simple shapes to complex animations.
  • Platform-Specific Embeddings: These are the layers that connect the Flutter framework to native operating system APIs, such as Android’s Activity or iOS’s UIViewController. These embeddings allow Flutter to run seamlessly across platforms.
  • Text and Layout Engines: The engine includes components to manage layout and typography, ensuring consistent rendering across devices and screen sizes.
  • Event Handling System: Handles user interactions like touch gestures, mouse inputs, and keyboard events.
  • Asset Management: Efficiently handles application assets such as images, fonts, and other resources.

The engine’s modular architecture and flexibility make it suitable for various applications, from mobile apps to desktops and even embedded systems. Customizing the engine involves modifying these components to meet specific requirements.


Why Customize the Flutter Engine?

There are several scenarios where customizing the Flutter engine can be advantageous:

  1. Optimizing Performance: Tailoring the engine by removing unused components can reduce memory usage and improve performance. For instance, you might optimize rendering for devices with specific GPUs..
  2. Adding Features: Some applications require access to hardware or APIs not natively supported by Flutter. For example, you might want to add support for a thermal imaging sensor or a unique communication protocol.
  3. Targeting Specific Platforms: Flutter’s engine can be adapted to run on non-standard devices or custom operating systems, such as embedded systems in industrial or automotive applications.
  4. Experimentation: Developers and researchers might explore alternative rendering engines, event systems, or new ways to handle inputs to push the boundaries of what Flutter can do.
  5. Enhancing Graphics and Rendering: Modify the rendering pipeline to include custom shaders, advanced visual effects, or support for 3D rendering for AR/VR applications.
  6. Security and Compliance: Customizing the engine can allow for integrating security features or compliance mechanisms tailored to sensitive applications, such as healthcare or finance.

Setting Up the Flutter Engine for Customization

Customizing the Flutter engine starts with setting up a local development environment and building the engine from source. This section walks through the process step by step.

Step 1: Cloning the Flutter Engine

To begin, you need the source code of the Flutter engine. Clone it from GitHub using the following commands:

git clone https://github.com/flutter/engine.git
cd engine

This repository contains the complete source code, including the engine, third-party dependencies, and tools necessary to build and customize the engine. Familiarize yourself with its structure:

  • shell/: Contains the runtime components of the engine.
  • third_party/: Houses dependencies like Skia.
  • flutter/: Includes the framework-related code.
  • common/: Shared components and utilities.

Step 2: Installing Required Tools

You’ll need specific tools to build and customize the engine:

  • GN (Generate Ninja): The meta-build system used by the Flutter engine.
  • Ninja: A high-performance build tool.
  • C++ Compiler: Compatible with your target platform.
  • Python: Required for build scripts.

Install these tools and ensure they are accessible from your terminal.

Step 3: Building the Engine

Flutter’s build system supports multiple platforms. Here’s an example command to build the engine for Android:

python ./tools/gn --android
ninja -C out/android_debug_unopt

This builds a debug version of the engine, suitable for testing your modifications. You can also build for iOS, desktop, or embedded platforms by specifying the appropriate build flags.

Step 4: Running a Flutter App with Your Custom Engine

To test your changes, configure the Flutter tool to use your custom engine by pointing it to the built artifacts:

flutter run --local-engine-src-path=path/to/engine/src --local-engine=android_debug_unopt

This allows you to use the modified engine with any Flutter application.


Examples of Customization

Let’s explore some practical use cases for customizing the Flutter engine:

1. Modifying the Rendering Pipeline

The Flutter engine’s rendering pipeline is highly optimized but can be tailored further. For example:

  • Integrating a Custom Rendering Engine: Replace Skia with a different rendering engine (e.g., Vulkan or Metal) for platforms that benefit from alternative graphics APIs.
  • Optimizing for Specific Hardware: Adjust rendering settings to maximize performance on low-powered devices.

2. Adding Platform-Specific Features

Some hardware features may not be supported out of the box. By customizing the engine, you can:

  • Add support for specialized sensors or peripherals.
  • Introduce APIs for accessing unique device capabilities.

For example, you could integrate a thermal imaging sensor with a Flutter app by writing custom code in the engine and exposing it through platform channels.

3. Optimizing Resource Usage

In resource-constrained environments, you can:

  • Trim Unused Features: Remove unused fonts, codecs, or asset loading mechanisms.
  • Improve Memory Management: Optimize how resources are loaded and unloaded during app lifecycle events.

4. Changing the Event Loop

For real-time applications, customizing how the engine processes input events (e.g., touch, mouse) can reduce latency. Modifications to the event loop can also make Flutter suitable for scenarios like robotics or live-streaming interfaces.


Testing and Debugging Custom Engines

Testing is crucial when working with custom engines to ensure stability and performance.

Setting Up Test Apps

Develop small test applications to validate engine changes. For example:

  • A simple UI app for testing rendering changes.
  • An app that interacts with new hardware features added to the engine.

Using Debugging Tools

Flutter provides robust debugging tools:

  • Dart DevTools: Analyze performance and diagnose issues.
  • Flutter Observatory: Debug Dart code running on your custom engine.

Avoiding Common Pitfalls

  • Ensure compatibility with the Flutter framework.
  • Regularly merge changes from the main Flutter repository to keep your custom engine updated.
  • Thoroughly test for regressions after each modification.

Deployment Considerations

Once your custom engine is stable, consider how to deploy it:

Packaging the Engine

Create distributable binaries for your target platforms. For example:

  • .so files for Android.
  • .framework bundles for iOS.
  • Executables for desktop or embedded devices.

Integrating with Flutter Apps

Modify flutter_tools to use your custom engine. This may involve:

  • Updating the flutter.yaml configuration.
  • Ensuring compatibility with specific Flutter versions.

Maintaining Your Engine

  • Document all customizations for future reference.
  • Stay updated with upstream changes to avoid conflicts.

Real-World Use Cases of Custom Engines

Here are a few scenarios where custom Flutter engines shine:

  1. Lightweight Apps for Embedded Systems:
  • Running Flutter on single-board computers like Raspberry Pi.
  • Optimizing for devices with limited RAM and processing power.

2. Gaming Frameworks:

  • Enhancing rendering capabilities for real-time graphics.
  • Integrating game controllers or VR hardware.

3. Augmented Reality Applications:

  • Customizing the engine to support AR libraries and optimize performance.

4. Industrial IoT Dashboards:

  • Adding support for specialized sensors or industrial protocols.

Challenges and Best Practices

Challenges:

  • Complexity: Modifying the engine requires advanced knowledge of C++ and Flutter’s architecture.
  • Maintenance Overhead: Keeping your engine up-to-date with the main Flutter repository can be time-consuming.
  • Compatibility Issues: Ensuring that changes don’t break the framework or third-party plugins.

Best Practices:

  • Start small and test frequently.
  • Keep your modifications modular to simplify updates.
  • Use version control to track changes and facilitate collaboration.
  • Contribute back to the Flutter community if your changes are generalizable.

Conclusion

Customizing the Flutter engine opens up a world of possibilities for developers looking to push the boundaries of what Flutter can achieve. Whether optimizing performance, adding new features, or targeting unconventional platforms, a tailored engine can be a game-changer.

While the process involves a steep learning curve and careful maintenance, the benefits can far outweigh the challenges for specific use cases. So, roll up your sleeves, dive into the engine’s source code, and experiment with customizations that make your Flutter apps truly unique.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


References:

Writing custom platform-specific code
Learn how to write custom platform-specific code in your app.docs.flutter.dev

Flutter architectural overview
A high-level overview of the architecture of Flutter, including the core principles and concepts that form its design.docs.flutter.dev

Streamlining the App with the Flutter Engine
The Flutter Engine is an essential element of the Flutter framework. Learn about the Flutter Engine, its role in the…www.dhiwise.com


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Shallow & Deep Copy a Map In Dart

0

Dart supports Map information type. You can copy a Map into another one utilizing different strategies. There are two sorts of copy, shallow copy, and Deep copy. Understanding the distinction between those two is important before you pick how to copy a map.

This article will explore the Shallow & Deep Copy a Map In Dart. We will execute a demo program and I might want to tell you how to copy a Map in Dart and explain the difference between shallow copy and deep copy in your applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.

Table Of Contents::

Shallow Copy

Using Map.of

Using Map.from

Using Map.unmodifiable

Deep Copy

Conclusion



Shallow Copy:

A shallow copy means the main object which is the Map is copied, however, the internal objects (the components) are not. An object can be mutable or changeless.

If the component is immutable and you change the value, Dart can not adjust it and another object with an alternate memory area will be made.

Subsequently, utilizing shallow copy ought to be protected if the component sort of the Map is immutable, like Stringboolint, or bool. The following are instances of shallow copy utilizing Dart’s strategies.

Using Map.of:

The Map.of factory technique can be utilized to copy a Map into a another one.

factory Map.of(Map<K, V> other)

Demo:

var values = <int, String>{
1: 'red',
2: 'green',
3: 'blue',
};
var newValues = Map.of(values);

Using Map.from:

This technique is likeMap.of, however, you can utilize it to produce another Map whose key as well as component type is a subtype of the source key or component type.

  factory Map.from(Map other)

Making another Map with additional exact types is utilized. You need to ensure that all the keys and values have the more exact types.

Demo:

var values = <num, String>{
1: 'red',
2: 'green',
3: 'blue',
};
Map<int, String> newValues = Map.from(values);

Using Map.unmodifiable:

If you want to copy into a Map that cannot be modified, you can use Map.umodifiable factory method.

  factory Map.unmodifiable(Map<dynamic, dynamic> other)

Demo:

var types = Map.unmodifiable({
1: 'red',
2: 'green',
3: 'blue',
});

Deep Copy:

While shallow copy works for a Map with immutable components, you might have to involve a deep copy for a Map with mutable components. Utilizing the models above, assuming that the component is mutable, Dart will just copy the reference to the object.

The actual object isn’t copied. Consequently, assuming that you alter a component of the source, it additionally influences the element of the new Map. On the off chance that you don’t need that way of behaving, you need to clone every component by making another object.

For demo, we have a Map whose component type is Item. To make another Item object from a current one, we can make a factory technique clone.

class Item {
String name;

Item({
required this.name,
});

factory Item.clone(Item source) {
return Item(
name: source.name,
);
}
}

From that point forward, use map the technique to map every component. You want to return a MapEntry and utilize the cloning strategy above to copy the mutable object.

  var item1 = Item(name: 'red');
var item2 = Item(name: 'green');
Map<int, Item> source = {1: item1, 2: item2};
var clone = source.map((key, value) => MapEntry(key, Item.clone(value)));
item1.name = 'green';
print(source[1]?.name); // Green
print(clone[1]?.name); // Red

Conclusion:

In the article, I have explained the Shallow & Deep Copy a Map In Dart; you can modify this code according to your choice. This was a small introduction to the Shallow & Deep Copy a Map In Dart User Interaction from my side, and it’s working using Flutter.

That is how to copy a Map in Dart. If the Map just holds back immutable key and component values, shallow values ought to be sufficient. If either the key or value has mutable components, you might need to perform a deep copy.

I hope this blog will provide you with sufficient information on Trying the Shallow & Deep Copy a Map In Dart of your projects. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Explore Animated Loader In Flutter

0

The loader is utilized when the application is loading while information is coming from the Application programming interface or DataBase. The loader helps show loading when the application is in a loading state.

This article will Explore an Animated Loader In Flutter. We will perceive how to execute a demo program and we are going to learn about how we can create an animated loader using the loading_animation_widget package in your Flutter applications.

For Loading Animation:

loading_animation_widget | Flutter Package
Installation Add loading_animation_widget: to your pubspec.yaml dependencies then run flutter pub get dependencies…pub.dev

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table Of Contents::

Introduction

Implementation

Code Implement

Code File

Conclusion



Introduction:

This demo video shows how to create an animated loader in Flutter and how an animated loader will work using the loading_animation_widget package in your Flutter applications. We will show users different types of loaders. Users can choose one of the loaders for our projects. It will be shown on your device.

Demo Module::


Implementation:

Step 1: Add the dependencies

Add dependencies to pubspec — yaml file.

dependencies:
flutter:
sdk: flutter
loading_animation_widget: ^latest version

Step 2: Import

import 'package:loading_animation_widget/loading_animation_widget.dart';

Step 3: Run flutter packages get in the root directory of your app.

How to implement code in dart file :

You need to implement it in your code respectively:

Create a new dart file called main.dart inside the lib folder.

We should adjust our main. dart file. We want to new class AnimatedLoaderDemo(). In this class first, we will add a variable _pageController.

late PageController _pageController;

We will create an integer variable _pageNo is equal to zero.

int _pageNo = 0;

Now, we will add the initState() method. In this method, we will add a _pageController equal to the PageController(initialPage: _pageNo).

@override
void initState() {
super.initState();
_pageController = PageController(initialPage: _pageNo);
}

We will add the code to the build method. Below we will define _forPreviousPage_forNextPage, and animationList with the code.

@override
Widget build(BuildContext context) {
return PageView(
physics: const NeverScrollableScrollPhysics(),
controller: _pageController,
children: animationList
.map(
(appBody) => Scaffold(
backgroundColor: Colors.cyan.shade200,
appBar: (AppBar(
automaticallyImplyLeading: false,
title: const Text("Flutter Animated Loader Demo"),
centerTitle: true,
backgroundColor: Colors.cyan,
)),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: MediaQuery.of(context).size.width / 6,
),
Text(
"Page : $_pageNo",
style: const TextStyle(fontSize: 20),
),
Expanded(
child: Center(
child: appBody.widget,
),
),
],
),
bottomNavigationBar: SafeArea(
top: false,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: const Icon(
Icons.chevron_left_rounded,
),
onPressed: _forPreviousPage,
),
IconButton(
icon: const Icon(
Icons.chevron_right_rounded,
),
onPressed: _forNextPage,
),
],
),
),
),
),
)
.toList(),
);
}

Now we will add the _forNextPage() method. In this method, which is helpful to go to the next page.

void _forNextPage() {
if (_pageNo + 1 < animationList.length) {
_pageController.jumpToPage(_pageNo + 1);
setState(() {
_pageNo++;
});
} else {
_pageController.animateToPage(
0,
duration: const Duration(milliseconds: 800),
curve: Curves.ease,
);
setState(() {
_pageNo = 0;
});
}
}

Now we will add the _forPreviousPage() method. In this method, which is helpful to go to the previous page.

void _forPreviousPage() {
if (_pageNo == 0) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'Not any Page form Previous',
),
),
);
} else {
_pageController.jumpToPage(_pageNo - 1);
setState(() {
_pageNo--;
});
}
}

Now, we will create a new class that was AppBody and the code is below.

class AppBody {
final String title;
final Widget widget;

AppBody(
this.title,
this.widget,
);
}

Now we will add the animationList. We need to create a List whose type name will be same as above class name.

final animationList = <AppBody>[
AppBody(
'Loader',
const Text(
'Total animations: 5',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
),
),
),
AppBody(
'inkDropLoader',
LoadingAnimationWidget.waveDots(
color: Colors.redAccent,
size: 70,
),
),
AppBody(
'twistingDotsLoader',
LoadingAnimationWidget.twistingDots(
leftDotColor: const Color(0xFF1A1A3F),
rightDotColor: const Color(0xFFEA3799),
size: 70,
),
),
AppBody(
'threeRotatingDotsLoader',
LoadingAnimationWidget.threeRotatingDots(
color: Colors.white,
size: 70,
),
),
AppBody(
'staggeredDotsWaveLoader',
LoadingAnimationWidget.staggeredDotsWave(
color: Colors.white,
size: 70,
),
),
AppBody(
'fourRotatingDotsLoader',
LoadingAnimationWidget.fourRotatingDots(
color: Colors.white,
size: 70,
),
),
];

When we run the application, we ought to get the screen’s output like the underneath screen capture.


Code File:

import 'package:flutter/material.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';

class AnimatedLoaderDemo extends StatefulWidget {
const AnimatedLoaderDemo({Key? key}) : super(key: key);

@override
State<AnimatedLoaderDemo> createState() => _AnimatedLoaderDemoState();
}

class _AnimatedLoaderDemoState extends State<AnimatedLoaderDemo> {
late PageController _pageController;
int _pageNo = 0;

@override
void initState() {
super.initState();
_pageController = PageController(initialPage: _pageNo);
}

@override
Widget build(BuildContext context) {
return PageView(
physics: const NeverScrollableScrollPhysics(),
controller: _pageController,
children: animationList
.map(
(appBody) => Scaffold(
backgroundColor: Colors.cyan.shade200,
appBar: (AppBar(
automaticallyImplyLeading: false,
title: const Text("Flutter Animated Loader Demo"),
centerTitle: true,
backgroundColor: Colors.cyan,
)),
body: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
height: MediaQuery.of(context).size.width / 6,
),
Text(
"Page : $_pageNo",
style: const TextStyle(fontSize: 20),
),
Expanded(
child: Center(
child: appBody.widget,
),
),
],
),
bottomNavigationBar: SafeArea(
top: false,
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: const Icon(
Icons.chevron_left_rounded,
),
onPressed: _forPreviousPage,
),
IconButton(
icon: const Icon(
Icons.chevron_right_rounded,
),
onPressed: _forNextPage,
),
],
),
),
),
),
)
.toList(),
);
}

void _forNextPage() {
if (_pageNo + 1 < animationList.length) {
_pageController.jumpToPage(_pageNo + 1);
setState(() {
_pageNo++;
});
} else {
_pageController.animateToPage(
0,
duration: const Duration(milliseconds: 800),
curve: Curves.ease,
);
setState(() {
_pageNo = 0;
});
}
}

void _forPreviousPage() {
if (_pageNo == 0) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'Not any Page form Previous',
),
),
);
} else {
_pageController.jumpToPage(_pageNo - 1);
setState(() {
_pageNo--;
});
}
}
}

class AppBody {
final String title;
final Widget widget;

AppBody(
this.title,
this.widget,
);
}

final animationList = <AppBody>[
AppBody(
'Loader',
const Text(
'Total animations: 5',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
),
),
),
AppBody(
'inkDropLoader',
LoadingAnimationWidget.waveDots(
color: Colors.redAccent,
size: 70,
),
),
AppBody(
'twistingDotsLoader',
LoadingAnimationWidget.twistingDots(
leftDotColor: const Color(0xFF1A1A3F),
rightDotColor: const Color(0xFFEA3799),
size: 70,
),
),
AppBody(
'threeRotatingDotsLoader',
LoadingAnimationWidget.threeRotatingDots(
color: Colors.white,
size: 70,
),
),
AppBody(
'staggeredDotsWaveLoader',
LoadingAnimationWidget.staggeredDotsWave(
color: Colors.white,
size: 70,
),
),
AppBody(
'fourRotatingDotsLoader',
LoadingAnimationWidget.fourRotatingDots(
color: Colors.white,
size: 70,
),
),
];

Conclusion:

In the article, I have explained the Animated Loader in a flutter; you can modify this code according to your choice. This was a small introduction to Animated Loader On User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying the Animated Loader in your Flutter projectsWe will show you what the Introduction is. Make a demo program for working on Animated Loader Using the loading_animation_widget package in your Flutter applications. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Sorting List of Objects/Maps By Date In Flutter

0

This article will explore the Sorting List of Objects/Maps By Date In Flutter. We will perceive how to execute a demo program and we are going to learn about how we can use it in your applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table Of Contents::

Introduction

Sorting a List of Objects by Date

Sorting a List of Maps by Date

Conclusion



Introduction:

The point is to utilize the sort() technique for the List class and pass a custom correlation capability that utilizes the compareTo() strategy for the DateTime class:

myList.sort ((a, b) {
// for ascending order
return a.date.compareTo (b.date);

// for descending order
// return b.date.compareTo (a.date);
});

Sorting a List of Objects by Date:

Suppose you have a list of users. The data of every user is stored away in an object, and your supervisor requests that you reorder these users by their join date. The model underneath will show you to tackle this:

The code:

class User {
final String name;
final String email;
final DateTime joinDate;

User({required this.name, required this.email, required this.joinDate});

@override
String toString() {
return 'User{name: $name, email: $email, joinDate: $joinDate}';
}
}

void main() {
// list of users
List<User> users = [
User(
name: 'Rahul',
email: 'raul@gmail.com',
joinDate: DateTime(2022, 10, 3)),
User(
name: 'John',
email: 'john@gmail.com',
joinDate: DateTime(2019, 5, 19)),
User(
name: 'Sam',
email: 'sam@gmail.com',
joinDate: DateTime(2021, 5, 12)),
User(
name: 'RSorted users by ascending joinDate:
[{name: John, email: john@gmail.com, joinDate: 2019-05-19}, {name: Sam, email: sam@gmail.com, joinDate: 2021-05-12}, {name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03}, {name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08}]
Sorted users by descending joinDate:
[{name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08}, {name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03}, {name: Sam, email: sam@gmail.com, joinDate: 2021-05-12}, {name: John, email: john@gmail.com, joinDate: 2019-05-19}]

Process finished with exit code 0uchi',
email: 'ruchi@gmail.com',
joinDate: DateTime(2023, 2, 8))
];

// sort by joinDate: ascending
final sortedUsersAsc = users.map((user) => user).toList()
..sort((a, b) => a.joinDate.compareTo(b.joinDate));

// sort by joinDate: descending
final sortedUsersDesc = users.map((user) => user).toList()
..sort((a, b) => b.joinDate.compareTo(a.joinDate));

print('Sorted by joinDate: ascending');
sortedUsersAsc.forEach((user) => print(user.toString()));

print('Sorted by joinDate: descending');
sortedUsersDesc.forEach((user) => print(user.toString()));
}

When we run the application, we ought to get the screen’s output like the underneath screen Console Output.

Sorted by joinDate: ascending
User{name: John, email: john@gmail.com, joinDate: 2019-05-19 00:00:00.000}
User{name: Sam, email: sam@gmail.com, joinDate: 2021-05-12 00:00:00.000}
User{name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03 00:00:00.000}
User{name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08 00:00:00.000}
Sorted by joinDate: descending
User{name: ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08 00:00:00.000}
User{name: rahul, email: raul@gmail.com, joinDate: 2022-10-03 00:00:00.000}
User{name: Sam, email: sam@gmail.com, joinDate: 2021-05-12 00:00:00.000}
User{name: John, email: john@gmail.com, joinDate: 2019-05-19 00:00:00.000}

Process finished with exit code 0

Sorting a List of Maps by Date:

In this model, the data about a user is stored away on a map rather than an object. Other than that, the join date of a user is a string, not a DateTime object. Hence, we should parse it into a DateTime object first utilizing the DateTime.parse() or DateTime.tryParse() strategy before playing out the comparison.

The code:

void main() {
final List<Map<String, dynamic>> users = [
{
"name": "Rahul",
"email": "raul@gmail.com",
"joinDate": "2022-10-03",
},
{
"name": "John",
"email": "john@gmail.com",
"joinDate": "2019-05-19",
},
{
"name": "Sam",
"email": "sam@gmail.com",
"joinDate": "2021-05-12",
},
{
"name": "Ruchi",
"email": "ruchi@gmail.com",
"joinDate": "2023-02-08",
}
];

// sort users by joinDate: ascending
final sortedUsersAsc = users.map((user) => user).toList()
..sort((a, b) =>
DateTime.parse(a["joinDate"]).compareTo(DateTime.parse(b["joinDate"])));

// sort users by joinDate: descending
final sortedUsersDesc = users.map((user) => user).toList()
..sort((a, b) =>
DateTime.parse(b["joinDate"]).compareTo(DateTime.parse(a["joinDate"])));

print("Sorted users by ascending joinDate:");
print(sortedUsersAsc);

print("Sorted users by descending joinDate:");
print(sortedUsersDesc);
}

When we run the application, we ought to get the screen’s output like the underneath screen Console Output.

Sorted users by ascending joinDate:
[
{name: John, email: john@gmail.com, joinDate: 2019-05-19},
{name: Sam, email: sam@gmail.com, joinDate: 2021-05-12},
{name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03},
{name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08}
]
Sorted users by descending joinDate:
[
{name: Ruchi, email: ruchi@gmail.com, joinDate: 2023-02-08},
{name: Rahul, email: raul@gmail.com, joinDate: 2022-10-03},
{name: Sam, email: sam@gmail.com, joinDate: 2021-05-12},
{name: John, email: john@gmail.com, joinDate: 2019-05-19}
]

Process finished with exit code 0

Conclusion:

In the article, I have explained the sorting list of Objects/Maps By Date In Flutter; you can modify this code according to your choice. This was a small introduction to Sorting a List of Objects/Maps By Date In Flutter User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on how to try the Sorting List of Objects/Maps by the date of your flutter projects. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Explore Speed Dial In Flutter

0

In Flutter, Speed Dial is a well-known floating action button (FAB) variation generally used to give users admittance to different actions or choices from a single button. At the point when the FAB is tapped, it extends to rapidly uncover a list of extra more modest buttons or actions, empowering users to perform related errands.

This article will Explore Speed Dial In Flutter. We will implement a demo program and I would like to show how to use speed dial in Flutter using the flutter_speed_dial package in your Flutter applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.

For Flutter Speed Dial:

flutter_speed_dial | Flutter package
Flutter plugin to implement a beautiful and dynamic Material Design Speed Dial with labels, animated icons…pub.dev


Table Of Contents::

Introduction

Implementation

Code Implement

Code File

Conclusion



Introduction:

A Speed Dial in Flutter is a proficient and easy-to-use method for dealing with various actions in a single Floating Action Button. It works on UI/UX plan while further developing availability to key elements.

This demo video shows how to use speed dial in Flutter and how a speed dial will work using the flutter_speed_dial package in your Flutter applications. We will show a user a fab button on a screen when the user presses the button it expands to reveal a list of additional smaller buttons or actions by using the flutter_speed_dial package. It will be shown on your device.

Demo Module::


Implementation:

Step 1: Add the dependencies

Add dependencies to pubspec — yaml file.

dependencies:
  flutter:
    sdk: flutter
  flutter_lints: ^3.0.0

Step 2: Import

import 'package:flutter_speed_dial/flutter_speed_dial.dart';

Step 3: Run flutter packages get in the root directory of your app.

Code Implement:

You need to implement it in your code respectively:

Create a new dart file called main.dart inside the lib folder.

In the main.dart file, we will create a new class was SpeedDialDemo(). In this new class, we will add the body. In the body we will add the center widget. Inside the widget, we will add an Image with height and width and text is ‘PLease tap the FAB button’.

Center(
child: Column(
children: [
Image.asset(
"assets/logo.png",
height: MediaQuery.of(context).size.height * 0.45,
width: MediaQuery.of(context).size.width * 0.7,
),
const Text(
'PLease tap the FAB button',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
),
],
)),

Then, now we will add the floatingActionButton. In this button we will add SpeedDial() function. Inside this function, we will add animatedIcon, backgroundColor, foregroundColor, overlayColor, overlayOpacity, etc.

floatingActionButton: SpeedDial(
  animatedIcon: AnimatedIcons.menu_close,
  backgroundColor: Colors.teal.shade100,
  foregroundColor: Colors.black,
  overlayColor: Colors.white,
  overlayOpacity: 0.5,
  spacing: 12.0,
  spaceBetweenChildren: 9.0,
  direction: SpeedDialDirection.up,
  children: [
    SpeedDialChild(
      child: const Icon(Icons.message),
      label: 'Chat',
      onTap: () => print('Chat'),
    ),
    SpeedDialChild(
      child: const Icon(Icons.call),
      label: 'Call',
      onTap: () => print('Call'),
    ),
    SpeedDialChild(
      child: const Icon(Icons.camera_alt),
      label: 'Camera',
      onTap: () => print('Camera'),
    ),
  ],
),

Its children, we will add three SpeedDialChild() function. In this function, we will three icons on the child, label and onTap(). When user tap the the button is expands and show icons.

When we run the application, we ought to get the screen’s output like the underneath screen capture.

Code File:

import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:flutter_speed_dial_demo/splash_screen.dart';

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

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
debugShowCheckedModeBanner: false,
home: const Splash(),
);
}
}

class SpeedDialDemo extends StatefulWidget {
const SpeedDialDemo({super.key});

@override
State<SpeedDialDemo> createState() => _SpeedDialDemoState();
}

class _SpeedDialDemoState extends State<SpeedDialDemo> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: false,
centerTitle: true,
title: const Text('Flutter Speed Dial Demo'),
backgroundColor: Colors.teal.shade100,
),
body: Center(
child: Column(
children: [
Image.asset(
"assets/logo.png",
height: MediaQuery.of(context).size.height * 0.45,
width: MediaQuery.of(context).size.width * 0.7,
),
const Text(
'PLease tap the FAB button',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
),
],
)),
floatingActionButton: SpeedDial(
animatedIcon: AnimatedIcons.menu_close,
backgroundColor: Colors.teal.shade100,
foregroundColor: Colors.black,
overlayColor: Colors.white,
overlayOpacity: 0.5,
spacing: 12.0,
spaceBetweenChildren: 9.0,
direction: SpeedDialDirection.up,
children: [
SpeedDialChild(
child: const Icon(Icons.message),
label: 'Chat',
onTap: () => print('Chat'),
),
SpeedDialChild(
child: const Icon(Icons.call),
label: 'Call',
onTap: () => print('Call'),
),
SpeedDialChild(
child: const Icon(Icons.camera_alt),
label: 'Camera',
onTap: () => print('Camera'),
),
],
),
);
}
}

Conclusion:

In the article, I have explained the spead dial in a flutter; you can modify this code according to your choice. This was a small introduction to speed dial On User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying the Speed Dial in your flutter projectsWe will show you what the Introduction is. Make a demo program for working on speed dial Using the flutter_speed_dial package in your flutter applications. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on FacebookGitHubTwitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Flutter’s Architecture: Understanding the Internals

0

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table of Contents

Overview of Flutter’s Architecture

Flutter Engine: The Heart of Rendering

The Flutter Rendering Pipeline: How the UI is Drawn

Layers

Skia: The Graphics Engine Behind Flutter

Dart and Skia: How They Communicate

Deep Dive into Flutter’s Rendering Pipeline

Layout Phase: Determining Size and Position

Paint Phase: Rendering Visuals to the Screen

Compositing: Combining Layers for Efficient Rendering

Conclusion

References


Introduction

Flutter has quickly become one of the most popular frameworks for building cross-platform mobile apps. With its rich widget ecosystem, beautiful UIs, and seamless performance across iOS and Android, Flutter is an attractive choice for developers. However, behind the scenes, Flutter’s architecture is based on a highly optimized and custom rendering pipeline that ensures it runs efficiently and provides developers with great flexibility.

In this post, we will examine Flutter’s internal architecture, focusing on its enginerendering pipeline, and widget drawing on the screen. We will also discuss how Dart interacts with Skia, Flutter’s graphics library, and the custom rendering system that powers its high performance.


Overview of Flutter’s Architecture

Flutter’s architecture comprises several layers that work together to provide an efficient, high-performance development experience. Let’s start with a quick overview of the key components:

  • Dart App: It composes widgets into the desired user interface (UI) and implements the business logic necessary for the application. This involves arranging the widgets in a way that meets the design requirements and ensuring the app’s functionality is driven by the appropriate business rules and processes.
  • Flutter Framework (Dart-based): This is the top layer of Flutter where developers work directly. It includes all the widgets, libraries, and tools to create the app’s UI and business logic. The framework itself is written in Dart, which provides an expressive, easy-to-learn programming environment.
  • Flutter Engine: This is the core engine written in C++ that powers Flutter’s rendering capabilities. It handles low-level operations such as rendering, input handling, text layout, and more. The engine communicates with the underlying platform (iOS/Android) through platform channels to access native features.
  • Platform Layer: This layer provides access to the device’s platform-specific features such as sensors, cameras, geolocation, etc. Flutter communicates with this layer using platform channels, which allow the Dart code to interact with native code (Java/Kotlin for Android and Swift/Objective-C for iOS).
  • Embedder: The system coordinates with the underlying operating system to gain access to essential services, such as rendering surfaces, accessibility, and input management. It also takes responsibility for managing the event loop, ensuring that events are processed efficiently. Additionally, the system exposes a platform-specific API that allows the Embedder to be integrated seamlessly into applications, enabling smooth interaction with the operating system’s services and resources.
  • Runner: It composes the components exposed by the platform-specific API of the Embedder into an app package that is runnable on the target platform. This process is an integral part of the app template generated by the flutter create command, ensuring that the necessary resources and configurations are in place for the app to function properly on the specified platform.

Flutter Engine: The Heart of Rendering

The Flutter Engine is the core of Flutter’s performance and rendering. It is written in C++ and is responsible for managing the entire lifecycle of a Flutter app. The engine provides several essential components, including:

  • Dart VM: Flutter runs Dart code using the Dart Virtual Machine (VM). This VM is highly optimized for Flutter and enables efficient execution of business logic. During development, Flutter uses Just-In-Time (JIT) compilation to speed up the development cycle. In production, Ahead-Of-Time (AOT) compilation is used for optimized performance.
  • Skia Graphics Library: Skia is a 2D graphics library used by Flutter to draw everything from simple shapes, text, and images to complex animations. Skia provides hardware-accelerated rendering, making it ideal for Flutter’s performance-oriented needs.
  • Text Layout: Flutter uses Skia’s text layout engine to render text across various fonts, languages, and styles. It includes features such as font rasterization and text shaping.
  • Platform Channels: Flutter’s engine also manages communication with native platform code through platform channels, allowing Flutter apps to access native features like camera, geolocation, and more.

The Flutter Rendering Pipeline: How the UI is Drawn

One of the most interesting aspects of Flutter’s architecture is its custom rendering pipeline. Unlike many mobile UI frameworks, Flutter does not rely on the native platform’s rendering system. Instead, it builds its UI using its own set of widgets, which it then renders via Skia. Let’s walk through the rendering pipeline step by step:

1. Widget Creation

In Flutter, everything is a widget. Widgets are the building blocks of the UI, and they describe what should appear on the screen. However, widgets themselves do not draw anything. They are immutable and declarative, meaning that when a widget is updated, it results in a new widget tree being created.

Text("Hello, Flutter!", style: TextStyle(fontSize: 24, color: Colors.blue));

This widget does not actually paint anything on the screen. It simply describes that there should be text with the specified style. Flutter uses a widget tree to manage the app’s UI, where each widget represents a part of the UI (like text, images, buttons, etc.).

2. Element Tree

Widgets are lightweight and immutable. When a widget changes, it doesn’t modify the existing widget directly; instead, it creates a new widget. The Flutter framework converts this widget tree into an Element tree.

An Element is a runtime representation of a widget and holds references to the widget’s state. This allows Flutter to keep track of the UI and the state of each widget efficiently.

3. RenderObject Tree

Once the element tree is established, Flutter transforms it into a RenderObject tree. RenderObjects are the low-level objects responsible for laying out and painting widgets on the screen. They handle the layout of widgets in terms of size and position.

The RenderObject tree can be thought of as a more detailed version of the widget tree, where each object has the responsibility for rendering itself. For instance, a Container widget might be represented as a RenderBox object.

Container(
padding: EdgeInsets.all(8.0),
child: Text("Hello, Flutter!"),
)

In this example, the Container widget would have a corresponding RenderBox that handles the layout and painting of the text and the padding inside.

4. Layout Phase

The layout phase is responsible for determining the size and position of every widget. It starts at the root of the RenderObject tree and moves downward, recursively calculating the layout of each widget.

In the layout phase, each RenderObject must decide its size (width and height) and its position relative to its parent and children. Flutter’s layout system is highly flexible, allowing widgets to behave in a variety of ways (e.g., wrapping, expanding, or aligning).

Here’s an example of a Column widget containing two children

Column(
children: [
Text("First Item"),
Text("Second Item"),
],
)

During the layout phase, the Column will calculate the size and position of the two Text widgets, aligning them vertically.

5. Paint Phase

Once the layout phase is completed, Flutter enters the paint phase. This is when the actual drawing happens. In this phase, each RenderObject will paint itself onto the screen using the Canvas API.

For example, the Text widget will use Skia’s text rendering capabilities to draw the text, while a Container might use a rectangle to render its background.

renderBox.paint(canvas, offset);

In this code, renderBox is the RenderObject associated with the widget, and paint() is responsible for drawing the widget onto the screen.

6. Compositing

Once the widgets are painted, Flutter may need to combine the rendered layers into a single image that can be displayed on the screen. This process is called compositing.

Flutter uses a layered compositing system to combine multiple layers of UI elements. This allows Flutter to optimize the rendering process, ensuring that only the parts of the screen that have changed need to be redrawn.

For example, if a Container widget changes, only that widget’s layer will be re-rendered, not the entire screen.


Layers

Layers are an important concept of the Flutter framework, which are grouped into multiple categories in terms of complexity and arranged in the top-down approach. The topmost layer is the UI of the application, which is specific to the Android and iOS platforms. The second topmost layer contains all the Flutter native widgets. The next layer is the rendering layer, which renders everything in the Flutter app. Then, the layers go down to Gestures, foundation library, engine, and finally, core platform-specific code. The following diagram specifies the layers in Flutter app development.


Skia: The Graphics Engine Behind Flutter

One of the central pillars of Flutter’s performance and rendering capabilities is Skia, an open-source 2D graphics library that powers much of Flutter’s graphical rendering. Skia provides an efficient and flexible framework for drawing complex visual elements such as text, images, shapes, and animations on the screen. It is a low-level graphics engine used by both Android and Chrome, but Flutter leverages it as its primary means of rendering UI content. Skia’s high-performance rendering system is one of the main reasons Flutter apps can run so smoothly across platforms.

Key Features of Skia in Flutter

Here are the most crucial features of Skia, which are leveraged by Flutter to render high-performance graphics:

  • Cross-Platform Rendering: Skia is platform-agnostic, meaning it can be used across various platforms, including Android, iOS, macOS, Linux, and Windows. Since Flutter relies on Skia for rendering, it can ensure consistent visual fidelity across all platforms.
  • Low-Level Graphics API: Skia provides a low-level API to interact with 2D drawing primitives like paths, text, and bitmaps. Flutter interacts with Skia using this API to render widgets and layouts to the screen efficiently.
  • GPU Acceleration: One of the most important aspects of Skia’s performance is its ability to leverage GPU acceleration for rendering tasks. Skia uses OpenGL, Vulkan, Metal, and other platform-specific graphics APIs to offload rendering to the GPU, which makes rendering smoother and more efficient, particularly when dealing with animations and complex visual effects.
  • Vector Graphics: Skia supports vector graphics, which means that the visual elements drawn on the screen can be scaled up or down without losing quality. This is particularly important for Flutter apps, as it ensures that the UI looks crisp on devices with varying screen sizes and resolutions.
  • High-Performance Text Rendering: Skia comes with a robust text rendering engine capable of high-performance text layout, including the ability to handle complex scripts, kerning, text alignment, and font rasterization. Flutter uses Skia’s text rendering capabilities for all its text-based widgets, ensuring that text is drawn accurately and efficiently.
  • Image Rendering: Skia is responsible for rendering images in Flutter as well. Whether it’s raster images (like PNGs, JPEGs) or vector images (like SVGs), Skia is used to efficiently display these images with high performance. It also supports hardware-accelerated image manipulation, which makes operations like image scaling and transformations fast and efficient.

Skia handles the low-level drawing operations, while Flutter’s engine controls the rendering pipeline, managing the communication between the Dart code and Skia.


Dart and Skia: How They Communicate

Flutter’s use of Dart for the business logic of the application and Skia for rendering might seem like a disconnect, but thanks to the Flutter engine, these two layers work together seamlessly. The engine acts as a bridge between the high-level Dart code and the low-level Skia rendering commands.

When you write your Flutter app using Dart, you’re primarily working with Widgets that describe the structure and behavior of your UI. However, once the widget tree is constructed, the Flutter engine translates these high-level descriptions into low-level rendering commands that Skia can process.

The Dart VM and Skia work together to make sure Flutter’s rendering pipeline runs smoothly. The Dart code, running in the Dart VM, is responsible for creating the widget tree and managing the app’s state. When a widget needs to be drawn, the Dart code interacts with the Flutter engine, which then communicates with Skia to render the widget to the screen.

Dart communicates with Skia through the Flutter engine, which acts as a bridge. The engine converts the high-level widget description in Dart into low-level drawing commands that Skia can understand. Skia, in turn, takes care of the actual drawing operations, using GPU resources when available to speed up rendering.

This separation allows Flutter to remain efficient while providing developers with a high-level declarative API to build UIs. By abstracting away the details of low-level rendering, Flutter enables developers to focus on building beautiful and performant apps without needing to worry about graphics rendering details.

How Dart and Skia Communicate:

  • Dart VM: When you write Dart code, it’s compiled by the Dart VM. During development, Just-In-Time (JIT) compilation is used, which makes hot-reloading possible. In production, Ahead-Of-Time (AOT) compilation ensures faster startup times and better performance.
  • Flutter Engine: The engine contains a C++ layer that acts as an intermediary between Dart and Skia. It translates the layout and painting instructions from Dart into commands that Skia can process.
  • Skia: Skia is where the actual drawing happens. It interacts with the hardware through GPU APIs (like OpenGL, Vulkan, and Metal) to render the graphics and text to the screen. The Canvas API in Flutter acts as a wrapper around Skia’s lower-level functions, allowing you to draw shapes, images, and text.

For instance, when you call a setState() method in your Flutter app, which causes the widget tree to rebuild, Flutter’s engine will rebuild the RenderObject tree, and any changes to the layout or appearance of the UI will be communicated to Skia to be redrawn.

For example, consider the following Flutter widget that draws a circle:

CustomPaint(
size: Size(200, 200),
painter: CirclePainter(),
)

The CirclePainter class would extend the CustomPainter class and override the paint method to draw the circle using Skia’s Canvas:

class CirclePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
canvas.drawCircle(Offset(size.width / 2, size.height / 2), 50, paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}

Example: Flutter and Skia for Custom Drawing

Consider a scenario where you want to draw a custom shape (say, a custom circle) using Flutter’s CustomPainter class. The CustomPainter allows you to use Skia’s rendering API to draw shapes directly onto the screen. Here’s an example of how you might do this:

class CirclePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;

// Drawing a circle using Skia's Canvas API
canvas.drawCircle(Offset(size.width / 2, size.height / 2), 50, paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false; // We don't need to repaint unless the custom painter logic changes
}
}

class MyCustomPaintWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return CustomPaint(
painter: CirclePainter(),
size: Size(200, 200), // Size of the area to paint
);
}
}

In this example:

  • CustomPainter extends CustomPainter and overrides the paint() method, which is called to render the widget.
  • Canvas is used to draw the circle. Internally, the drawCircle method uses Skia’s API to paint the circle onto the screen.
  • The Paint object specifies the color and the style of the drawing.

Skia is at the heart of this process, enabling Flutter to draw the custom shapes using optimized, low-level rendering routines.


Deep Dive into Flutter’s Rendering Pipeline

Now, let’s go even deeper into understanding how Flutter’s rendering pipeline works and how it helps Flutter achieve its high performance. The rendering pipeline is the series of steps that take place after the widget tree is built and before the app’s UI is displayed on the screen. This process allows Flutter to efficiently transform widget descriptions into pixel-perfect visuals, and it involves key components like Widget Trees, Element Trees, RenderObject Trees, Layout, Painting, and Compositing.

Each of these steps plays a crucial role in ensuring that Flutter apps perform well, even when complex UIs and animations are involved.

Widget Tree to RenderObject Tree: The Transformative Process

The journey from a simple Text widget to a rendered element on the screen starts with the widget tree. Each widget defines part of the UI (e.g., a button, text field, etc.). When a widget is placed inside another widget, Flutter’s framework begins by converting the widget tree into an Element tree and then into a RenderObject tree.

Widget Tree

The widget tree is a hierarchy of Flutter widgets that are declarative descriptions of the UI. Widgets are lightweight and immutable, meaning that when a widget changes, it creates a new widget. For example, if the text in a Text widget changes, the widget tree is recreated with the new text value.

Element Tree

The Element tree is a more efficient representation of the widget tree that exists at runtime. Each Element corresponds to a widget in the widget tree and stores references to its state. Unlike widgets, which are immutable, elements can have state, making them ideal for managing the UI’s changes during execution.

RenderObject Tree

The RenderObject tree is the most crucial part of the rendering pipeline. The RenderObject represents the visual elements that Flutter needs to draw on the screen. Every RenderObject has properties such as size, position, paint, and layout constraints that help it define how the widget should appear and behave. The RenderObject tree is responsible for rendering the app’s UI and handles tasks such as layout and painting.


Layout Phase: Determining Size and Position

The layout phase of the Flutter rendering pipeline determines the size and position of each widget on the screen. During this phase, each RenderObject must figure out its size based on its parent’s constraints and the available space in the layout.

The layout phase is hierarchical. It starts from the root of the tree and recursively processes each child. Here’s how the layout process works:

  1. Parent Constraints: The parent widget gives constraints to its child. For example, if you have a Row widget with two Text widgets inside, the Row widget determines how much space the child widgets can occupy.
  2. Size Determination: Each child widget (RenderObject) will calculate its size based on these constraints. For example, if the parent widget is a Column, it might impose constraints that tell its children to expand vertically, or if the parent is a Row, the children might be constrained to expand horizontally.
  3. Positioning: After each child widget determines its size, Flutter positions the widgets in the layout tree by calculating their offsets (x and y positions). This ensures that each widget is placed correctly on the screen relative to its parent.

In a typical Column widget, for example, each child is positioned vertically, with padding and spacing taken into account during the layout phase.

Column(
children: [
Padding(
padding: EdgeInsets.all(10),
child: Text("Hello, Flutter!"),
),
RaisedButton(
onPressed: () {},
child: Text("Press Me"),
),
],
)

Here, during the layout phase, the Column widget will position each child vertically, taking into account the padding for the first Text widget and the space needed for the RaisedButton.


Paint Phase: Rendering Visuals to the Screen

The paint phase is when the actual drawing happens. After the layout phase has calculated where each widget should be positioned, it’s time to paint the widgets. This phase utilizes the Canvas API, which is where Skia comes into play.

The Canvas is a central component of Flutter’s rendering system, and it provides methods to draw shapes, text, images, and more. Each RenderObject can paint itself on the canvas by calling specific methods.

For example, a simple Container widget might use a RenderBox to draw a rectangle, while a Text widget might use a text drawing method.

Example: Custom Painter for Drawing Shapes

Let’s look at a basic example where we use a CustomPainter to draw a shape (circle) on the screen.

class CirclePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
canvas.drawCircle(Offset(size.width / 2, size.height / 2), 50, paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}

In this example, the CirclePainter class extends CustomPainter. The paint method is where the actual drawing happens. Here, we are using Skia’s canvas.drawCircle method to draw a blue circle at the center of the given size. This is an example of how Flutter uses the Canvas API to interact with Skia to render graphics on the screen.


Compositing: Combining Layers for Efficient Rendering

Once the widgets are painted, Flutter enters the compositing phase, where it combines different layers of the UI into a single image to be displayed on the screen. Flutter’s compositing system ensures that only the parts of the screen that need to be updated are redrawn.

In this phase, Flutter uses a layered compositing model, where each widget’s painted output is stored in a layer. These layers are then composited into a single image that will be displayed on the screen. The advantage of this approach is that Flutter only re-renders the portions of the screen that have changed, rather than redrawing the entire screen.

For instance, if a button changes color on tap, only the button’s layer needs to be updated, not the entire screen. This results in significant performance gains, especially in complex applications with many UI elements.

Here’s an example of how layers can be used for custom painting with CustomPaint:

CustomPaint(
painter: CirclePainter(),
)

When Flutter draws the CirclePainter, the painted content is stored in a layer. If this layer needs to be updated (for instance, if the circle’s position or color changes), only this layer is recomposited. This makes Flutter apps extremely efficient when updating the UI.


Conclusion

Understanding the internals of Flutter’s architecture, its custom rendering pipeline, and the relationship between Dart and Skia is crucial for developers aiming to fully leverage Flutter’s capabilities. Flutter’s approach to UI rendering, with a custom pipeline based on Skia, enables it to deliver high performance and consistent results across platforms. This architecture not only makes Flutter fast and flexible but also empowers developers to create sophisticated UIs with full control over rendering.

By learning about the engine, the rendering process, and how Flutter draws widgets on screen, you can optimize your Flutter apps further, troubleshoot performance issues, and even build custom rendering solutions when needed.


At What the Flutter, we love working with Flutter apps to make them high-performing that delight users. Contact us today to discuss how we can optimize your Flutter app performance.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


References:

https://www.javatpoint.com/flutter-architecture#:~:text=In%20Flutter%2C%20the%20application%20is,complex%20user%20interface%20very%20easily.

Flutter architectural overview
A high-level overview of the architecture of Flutter, including the core principles and concepts that form its design.docs.flutter.dev

Flutter’s Architecture Explained
We explore the intricacies of Flutter’s architecture, divided into three primary layers: the Framework, the Engine, and…somniosoftware.com


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
 And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project hourly or full-time as per your requirement! For any flutter-related queries, you can connect with us on Facebook, GitHub, Twitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.

Show/Hide Password Using Riverpod In Flutter

0

This article will explore the Show/Hide Password Using Riverpod In Flutter. We will implement a demo program and I would like to show how to show/hide password in TextFormField by using the Riverpod state management in your flutter applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.

For Hooks Riverpod:

hooks_riverpod | Flutter Package
Run this command: With Flutter: $ flutter pub add hooks_riverpod This will add a line like this to your package’s…pub.dev

Table Of Contents::

Introduction

Implementation

Code Implement

Code File

Conclusion



Introduction:

This demo video shows how to show/hide passwords in textformfield in flutter and how a show/hide password will work using the hooks_riverpod package in your flutter applications. We will show a user show/hide password on a textformfield by using riverpod and also use will once we click outside of the TextFormField keyboard will close. It will be shown on your device.

Demo Module ::


Implementation:

Step 1: Add the dependencies

Add dependencies to pubspec — yaml file.

dependencies:
flutter:
sdk: flutter
hooks_riverpod: ^latest version

Step 2: Add the assets

Add assets to pubspec — yaml file.

assets:
- assets/

Step 3: Import

import 'package:hooks_riverpod/hooks_riverpod.dart';

Step 4: Run flutter packages get in the root directory of your app.

How to implement code in dart file :

You need to implement it in your code respectively:

Create a new dart file called main.dart inside the lib folder.

We should adjust our main. dart file. We want to add ProviderScope to the root of our application that stores the state of the Providers.

void main() {
runApp(
const ProviderScope(child: MyApp()),
);
}

Then, we need to change StatelessWidget to ConsumerWidget. Once we do that, we must add WidgetRef inside of the build.

class MyApp extends ConsumerWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context, WidgetRef ref) {
final bool showPassState = ref.watch(showPassProvider);
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.cyan,
),
debugShowCheckedModeBanner: false,
home: const Splash(),
);
}
}

Now time to create our StateProvider.

This is the provider we will use for show and hide.

final showPassProvider = StateProvider<bool>((ref) => true);

Now we will create a new class HomeScreen with extended ConsumerWidget. We will return the GestureDetector widget. In this widget, we will add a Column widget. And ad two textformfield.

class HomeScreen extends ConsumerWidget {
const HomeScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context, WidgetRef ref) {
final bool showPassState = ref.watch(showPassProvider);
return GestureDetector(
onTap: FocusManager.instance.primaryFocus?.unfocus,
child: Scaffold(
appBar: AppBar(
title: const Text(
"Flutter Show/Hide Password Riverpod Demo",
style: TextStyle(fontSize: 18,color: Colors.white),
),
backgroundColor: Colors.cyanAccent.withOpacity(.4),
automaticallyImplyLeading: false,
centerTitle: true,
),
body: Column(
children: [
Image.asset("assets/logo.png",height: 240,width: 280,),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
decoration: const InputDecoration(
prefixIcon: Icon(Icons.person),
labelText: "Username",
hintText: "Username",
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
obscureText: showPassState,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: showPassState
? const Icon(Icons.visibility_off)
: const Icon(Icons.visibility),
onPressed: () => ref
.read(showPassProvider.notifier)
.update((state) => !state),
),
prefixIcon: const Icon(Icons.lock),
labelText: "Password",
hintText: "Password",
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
),
),
),
],
),
),
);
}
}

Since obscureText is true in the beginning. We will set showPassState in obscureText.

TextFormField(
// hide the password. when it is true.
obscureText: showPassState,
...

Then, at that point, we ought to update our state for symbol click. To do that We involved IconButton in our suffixIcon. That’s what to do, we ought to read and afterward call the update function on the notifier, and we need to change our Icon concerning our state.

IconButton(
icon: showPassState
? const Icon(Icons.visibility_off)
: const Icon(Icons.visibility),
onPressed: () => ref
.read(showPassProvider.notifier)
.update((state) => !state),
),

When we run the application, we ought to get the screen’s output like the underneath screen capture.

Final Output

Code File:

import 'package:flutter/material.dart';
import 'package:flutter_show_hide_password_riverpod_demo/splash_screen.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

final showPassProvider = StateProvider<bool>((ref) => true);

void main() {
runApp(
const ProviderScope(child: MyApp()),
);
}

class MyApp extends ConsumerWidget {
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context, WidgetRef ref) {
final bool showPassState = ref.watch(showPassProvider);
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.cyan,
),
debugShowCheckedModeBanner: false,
home: const Splash(),
);
}
}

class HomeScreen extends ConsumerWidget {
const HomeScreen({Key? key}) : super(key: key);

@override
Widget build(BuildContext context, WidgetRef ref) {
final bool showPassState = ref.watch(showPassProvider);
return GestureDetector(
onTap: FocusManager.instance.primaryFocus?.unfocus,
child: Scaffold(
appBar: AppBar(
title: const Text(
"Flutter Show/Hide Password Riverpod Demo",
style: TextStyle(fontSize: 18,color: Colors.white),
),
backgroundColor: Colors.cyanAccent.withOpacity(.4),
automaticallyImplyLeading: false,
centerTitle: true,
),
body: Column(
children: [
Image.asset("assets/logo.png",height: 240,width: 280,),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
decoration: const InputDecoration(
prefixIcon: Icon(Icons.person),
labelText: "Username",
hintText: "Username",
border: OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
obscureText: showPassState,
decoration: InputDecoration(
suffixIcon: IconButton(
icon: showPassState
? const Icon(Icons.visibility_off)
: const Icon(Icons.visibility),
onPressed: () => ref
.read(showPassProvider.notifier)
.update((state) => !state),
),
prefixIcon: const Icon(Icons.lock),
labelText: "Password",
hintText: "Password",
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(
Radius.circular(20),
),
),
),
),
),
],
),
),
);
}
}

Conclusion:

In the article, I have explained the Show/Hide Password Using Riverpod in a flutter; you can modify this code according to your choice. This was a small introduction to Show/Hide Password Using Riverpod On User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying the Show/Hide Password Using Riverpod in your flutter projectsWe will show you what the Introduction is. Make a demo program for working on show/hide password in TextFormField Using the hooks_riverpod package in your flutter applications. So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on FacebookGitHubTwitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.


Explore Memory Leaks In Flutter

0

In this article, we will Explore Memory Leaks In Flutter. We perceive how to execute a demo program. We will show you what is the reason, detect, and prevent memory leaks in your Flutter applications.

If you’re looking for the best Flutter app development company for your mobile application then feel free to contact us at — support@flutterdevs.com.


Table Of Contents::

What Is a Memory Leaks?

What Reasons For Memory Leaks In Flutter?

How To Detect Memory Leak In Flutter?

How To Prevent Memory Leaks In Flutter?

Conclusion



What Is a Memory Leaks?

Memory leaks in Flutter applications can happen when a program holds memory that is not generally required, making the application consume more memory than needed. One common reason is the presence of unused objects that stay in memory due to caching, improper disposal, or failure to remove listeners. This can prompt slower execution, crashes, and eventually, an unsuitable user experience.

Abusing streams, which are utilized to deal with asynchronous events in Flutter, can likewise prompt memory leaks on the off chance that stream subscriptions are not as expected dropped, making the stream keep running behind the background and consuming memory. Besides, stacking enormous pictures and videos without delivering them properly can add to memory leaks in Flutter applications.

What Reasons For Memory Leaks In Flutter?

Memory Leaks might happen for some reasons. Here are the absolute most normal reasons.

  • > Unused Objects — For this reason, unused objects that are unclosed streams are the circumstances when we make the stream yet neglect to close it. Streams can keep references to objects in memory even after they are not generally utilized causing memory leaks.
import 'dart:async';
void main() {
StreamController<String> _streamSub = StreamController<String>();
  _streamSub.add("Flutter, Devs!");
  _streamSub.stream.listen((data) {
print("stream data: $data");
});
}

Other, unused objects that are not taken out from memory can cause memory leaks. This is because the items consume space in memory, even though they are not generally needed. Unused objects can be made for different reasons, for example, caching, not appropriately disposing of objects, and not eliminating listeners when they are not generally required.

  • > Improper Use of Streams — For this reason, streams are utilized in Flutter to deal with asynchronous events, and improper utilization of streams can cause memory leaks. For instance, not appropriately canceling a stream subscription can make the stream keep running behind the background, consuming memory.
  • > Global Variables — For this reason, the garbage collector doesn’t work while putting away references to objects or widgets as global variables. So we need to deallocate that memory as it can likewise cause memory leaks.
  • > Large Images and Videos — For this reason, loading enormous images and videos can likewise cause memory leaks in Flutter. At the point when huge files are stacked, they take up a lot of memory, and on the off chance that they are not as expected delivered when they are not generally required, they can cause memory leaks.
  • >Widget Trees — For this reason, mistakenly putting the widgets on the widget tree, particularly while utilizing the stateful widget can likewise cause memory leaks in Flutter.

How To Detect Memory Leak In Flutter?

Recognizing memory leaks in your Flutter application can be a bit challenging yet it is conceivable. The following are a few strategies and tools that can distinguish and fix memory leaks in the Flutter Application.

  • > Flutter DevTools — Flutter DevTools is a performance and debugging tool for Dart programs and Flutter Applications. You can utilize Flutter DevTools to look at your application’s memory usage. You can get to the Flutter DevTools by running the following command:
flutter pub global run devtools
  • > Heap Snapshots — You can likewise take heap snapshots to look at the memory utilization and breaks at a particular point in time. Heap Snapshots can assist you with recognizing objects that are not being garbage collected as expected.
  • > Analyze Your Code — You should review your code and give close consideration to objects. Now check assuming that the objects are appropriately disposed of when they are not generally required. You should likewise check for the controller as they are one of the most well-known reasons for memory leaks in Flutter.

How To Prevent Memory Leaks In Flutter?

  • > Dispose of Objects — One of the best ways of forestalling memory spills in Ripple is to discard objects when they are not generally required. Discarding objects eliminates them from memory and opens up assets for different pieces of the application. To discard objects, utilize the arrange technique in the Stateful Gadget.
  • > Use Streams Properly — To prevent memory leaks caused by streams, consistently cancel the subscription when it is not generally required. This guarantees that the stream is done running behind the background, consuming memory.
  • > Use Images and Videos Efficiently — To prevent memory leaks influenced by enormous images and videos, utilize efficient techniques to load them. One such strategy is to utilize the flutter_cache_manager package, which assists with caching images and videos, reducing the amount of memory they consume.
  • Use Profiling Tools — Profiling tools, like the Flutter DevTools, can assist with recognizing memory leaks in your application. By analyzing the memory utilization of your application, you can pinpoint the areas that are causing memory leaks and do whatever it takes to fix them.
  • > Avoid Saving Flutter Object References in Background Threads — To prevent memory leaks, refrain from putting Flutter object references in background threads. All things being equal, utilize weak references or isolate communication mechanisms like message passing. Weak references enable objects to be garbage collected when they are no longer strongly referenced, preventing memory leaks. Isolate communication ensures that Flutter objects can be safely passed between isolates without retaining strong references, further guarding against memory leaks.

Conclusion:

In the article, I have explained the Memory Leaks In Flutter; you can modify this code according to your choice. This was a small introduction to the Memory Leaks In Flutter User Interaction from my side, and it’s working using Flutter.

I hope this blog will provide you with sufficient information on Trying the Memory Leaks In Flutter in your Flutter projectsMemory leaks are a typical issue in Flutter, however, they can be prevented by following best practices and utilizing the appropriate tools. By disposing of objects, utilizing streams appropriately, and efficiently loading images and videos, developers can prevent memory leaks and make high-performing applications that provide an ideal user experience.

Remember to use profiling tools to recognize memory leaks in your application, and do whatever it may take to fix them. With these tips, you can build Flutter applications that are quick, efficient, and reliable.

❤ ❤ Thanks for reading this article ❤❤

If I need to correct something? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


From Our Parent Company Aeologic

Aeologic Technologies is a leading AI-driven digital transformation company in India, helping businesses unlock growth with AI automation, IoT solutions, and custom web & mobile app development. We also specialize in AIDC solutions and technical manpower augmentation, offering end-to-end support from strategy and design to deployment and optimization.

Trusted across industries like manufacturing, healthcare, logistics, BFSI, and smart cities, Aeologic combines innovation with deep industry expertise to deliver future-ready solutions.

Feel free to connect with us:
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a Flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! For any flutter-related queries, you can connect with us on FacebookGitHubTwitter, and LinkedIn.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.