Flutter
Flutter
1. Flutter Introduction
Flutter is an open-source UI framework by Google.
Used for cross-platform app development (Android, iOS, Web, Desktop).
Uses Dart as its programming language.
Features hot reload for fast development.
Based on widgets, everything in Flutter is a widget.
Provides native performance using its own rendering engine.
Allows single codebase for multiple platforms.
Supports custom animations, themes, and adaptive UI.
Used by companies like Google, Alibaba, BMW, and more.
Ideal for fast, scalable, and interactive applications.
Example:
void main() {
runApp(MaterialApp(home: Scaffold(body: Center(child: Text('Hello,
Flutter!')))));
}
2. Flutter Architecture
Follows a layered architecture for UI rendering.
Consists of:
1. Framework Layer (Dart-based UI and widgets).
2. Engine Layer (C++ rendering, animations, and graphics).
3. Embedder Layer (Platform-specific integrations).
Uses Skia graphics engine for fast UI rendering.
Follows a reactive programming model.
UI updates are declarative like React.
Provides state management solutions (Provider, Bloc, Riverpod).
Supports plugins for native features (camera, location, etc.).
Uses Material Design and Cupertino widgets for Android and iOS.
Ensures high-performance and smooth UI transitions.
3. Installation
Requires Flutter SDK and Dart SDK.
Install Flutter using:
Windows: flutter_windows_xxx.zip
macOS: brew install flutter
Linux: Download .tar.xz from Flutter’s site.
Set up environment variables ( flutter/bin path).
Run flutter doctor to check setup.
Install Android Studio/Xcode for emulators.
Enable Developer Mode & USB Debugging on Android.
Use flutter create my_app to create a project.
Run flutter run to start the app.
Works on VS Code, IntelliJ, and Android Studio.
Example:
void main() {
runApp(MaterialApp(home: Scaffold(appBar: AppBar(title: Text('Simple
App')))));
}
Example:
void main() {
print("Hello, Dart!");
}
6. Introduction to Widgets
Widgets are building blocks of a Flutter app.
Two types: StatelessWidget (immutable) & StatefulWidget (dynamic state).
Examples:
Text for text
Container for layout
Image for images
Everything is a widget, even the app layout.
Widgets follow a tree structure.
Uses build() method for UI updates.
Custom widgets improve code reusability.
Supports composition over inheritance.
Enhances modularity and maintainability.
Example:
7. Layouts
Flutter UI uses a widget tree.
Row for horizontal, Column for vertical layouts.
Stack for overlapping widgets.
Expanded and Flexible for responsive UI.
Uses Padding , Margin , Alignment for spacing.
MediaQuery helps in adaptive layouts.
Supports grid layout using GridView.
Can be customized using BoxDecoration .
Uses Constraints-based rendering.
Works well for mobile, tablet, and web UIs.
Example:
Column(
children: [Text('Hello'), Text('Flutter')],
)
8. Gestures
Detects user interactions like tap, swipe, pinch, long press.
Uses GestureDetector for event handling.
Supports onTap, onDoubleTap, onLongPress events.
Helps in drag-and-drop UI interactions.
Used in scrolling, zooming, and animations.
Supports multi-touch events.
Can combine multiple gestures.
Used in interactive buttons and UI elements.
Can handle swipe-to-delete actions.
Essential for smooth UX in mobile apps.
Example:
GestureDetector(
onTap: () => print("Tapped!"),
child: Container(color: Colors.blue, height: 50, width: 50),
)
9. State Management
Controls UI updates when data changes.
Flutter has multiple approaches:
1. setState() (local state, basic).
2. Provider (dependency injection, recommended).
3. Bloc (event-driven, best for large apps).
4. Riverpod (advanced state management).
Helps in data persistence and UI responsiveness.
Reduces boilerplate code.
Ensures consistent UI behavior.
Essential for real-time and dynamic apps.
Allows separation of UI and business logic.
Example:
AnimationController controller;
@override
void initState() {
controller = AnimationController(vsync: this, duration: Duration(seconds:
2));
controller.forward();
}
11. Packages
Flutter has thousands of pre-built packages.
Found on pub.dev, the official package repository.
Install using flutter pub add package_name .
Examples:
http for API calls
shared_preferences for local storage
firebase_core for Firebase integration
provider for state management
image_picker for camera access
Makes development faster and easier.
Supports third-party and community-developed plugins.
Custom packages can be created and published.
Reduces code duplication and improves efficiency.
Allows integrating native device features like GPS, Camera, etc.
Example:
14. Internationalization
Supports multiple languages and locales.
Uses intl package for translations.
MaterialApp has locale and localizationsDelegates options.
Stores translated strings in JSON files.
Allows dynamic language switching.
Supports date, time, and currency formats per region.
Uses arb files for automated translation.
Helps in reaching global audiences.
Essential for multi-language applications.
Reduces manual translation effort.
Example:
MaterialApp(
locale: Locale('en', 'US'),
supportedLocales: [Locale('en', 'US'), Locale('fr', 'FR')],
localizationsDelegates: [AppLocalizations.delegate],
);
15. Testing
Flutter supports unit testing, widget testing, and integration testing.
Uses flutter_test package for automated tests.
mockito for mocking API responses.
flutter_driver for UI automation testing.
golden tests for snapshot testing of widgets.
Helps in debugging and ensuring code quality.
Prevents unexpected crashes and UI inconsistencies.
Runs tests using flutter test .
Reduces manual testing effort.
Essential for large-scale applications and CI/CD pipelines.
16. Deployment
For Android, generates APK & AAB using flutter build apk/aab .
For iOS, builds via Xcode & App Store Connect.
Uses Firebase App Distribution for beta testing.
Supports Code Signing & Certificates.
Handles app versioning and updates.
Can be integrated with CI/CD pipelines (GitHub Actions, Codemagic).
Uses Flutter Web for PWA deployment.
Ensures secure and optimized release builds.
Essential for app publishing on Play Store and App Store.
Helps in delivering seamless updates to users.
Example:
MethodChannel('com.example.myapp').invokeMethod('getBatteryLevel');