Keep your code modular by organizing widgets into separate files and folders. This promotes reusability and makes it easier to maintain.
Author: saqibkhan
-
Skia Graphics Engine
Flutter uses the Skia graphics engine, which allows it to render UI elements quickly and smoothly, giving applications a native look and feel.
-
Leverage the Hot Reload Feature
Use Flutter’s hot reload to see changes in real-time without losing your app’s state, making the development process faster and more efficient.
-
Use Stateless and Stateful Widgets Wisely
Choose
StatelessWidgetfor immutable components andStatefulWidgetwhen you need to manage state. This improves performance and makes your code cleaner. -
Hot Reload
Flutter’s hot reload feature lets developers see changes in real time without restarting the application. This speeds up the development process and makes it easier to experiment with UI.
-
Understand the Widget Tree
Familiarize yourself with how Flutter’s widget tree works. Every UI element is a widget, and understanding this hierarchy helps in debugging and optimizing performance.
-
Implementing a Bottom Sheet
This example demonstrates how to show a bottom sheet with options when a button is pressed.
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) {
} } class BottomSheetExample extends StatelessWidget { void _showBottomSheet(BuildContext context) {return MaterialApp( title: 'Bottom Sheet Example', theme: ThemeData(primarySwatch: Colors.blue), home: BottomSheetExample(), );
} @override Widget build(BuildContext context) {showModalBottomSheet( context: context, builder: (context) { return Container( padding: EdgeInsets.all(16.0), height: 200, child: Column( children: [ Text('Select an option', style: TextStyle(fontSize: 18)), ListTile( title: Text('Option 1'), onTap: () { Navigator.pop(context); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Selected Option 1')), ); }, ), ListTile( title: Text('Option 2'), onTap: () { Navigator.pop(context); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Selected Option 2')), ); }, ), ], ), ); }, );
} }return Scaffold( appBar: AppBar(title: Text('Bottom Sheet Example')), body: Center( child: ElevatedButton( onPressed: () => _showBottomSheet(context), child: Text('Show Bottom Sheet'), ), ), ); -
Widgets
Everything in Flutter is a widget, from layout elements to buttons and text. This widget-centric approach makes it easy to create complex UIs by composing simple widgets together.
-
Using a TabBar for Navigation
This example shows how to use a
TabBarfor navigating between different sections of an app.import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) {
} } class TabBarExample extends StatelessWidget { @override Widget build(BuildContext context) {return MaterialApp( title: 'TabBar Example', theme: ThemeData(primarySwatch: Colors.blue), home: TabBarExample(), );
} }return DefaultTabController( length: 3, child: Scaffold( appBar: AppBar( title: Text('TabBar Example'), bottom: TabBar( tabs: [ Tab(text: 'Home'), Tab(text: 'Search'), Tab(text: 'Profile'), ], ), ), body: TabBarView( children: [ Center(child: Text('Home Screen')), Center(child: Text('Search Screen')), Center(child: Text('Profile Screen')), ], ), ), ); -
Displaying Data in a Grid View
This example illustrates how to create a grid layout to display images or items.
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) {
} } class GridViewExample extends StatelessWidget { final List<String> _images = [return MaterialApp( title: 'Grid View Example', theme: ThemeData(primarySwatch: Colors.blue), home: GridViewExample(), );
]; @override Widget build(BuildContext context) {'https://via.placeholder.com/150', 'https://via.placeholder.com/150', 'https://via.placeholder.com/150', 'https://via.placeholder.com/150', 'https://via.placeholder.com/150', 'https://via.placeholder.com/150',
} }return Scaffold( appBar: AppBar(title: Text('Grid View Example')), body: GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, childAspectRatio: 1, ), itemCount: _images.length, itemBuilder: (context, index) { return Card( child: Image.network( _images[index], fit: BoxFit.cover, ), ); }, ), );