Flutter
A comprehensive reference to Flutter
Flutter is an open-source UI software development toolkit created by Google. It enables developers to build natively compiled applications for mobile (iOS and Android), web, and desktop from a single codebase using the Dart programming language.
Key Characteristics:
- Cross-platform: Write once, deploy on multiple platforms.
- Widget-based: Everything in Flutter is a widget—from layout to UI elements.
- High performance: Uses its own rendering engine (Skia) for smooth graphics and animations.
- Hot reload: Instantly see code changes without restarting the app.
Basic App Structure
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', home: Scaffold( appBar: AppBar(title: Text('Hello Flutter')), body: Center(child: Text('Welcome')), ), ); } }
2. Widgets Overview
StatelessWidget
class Greeting extends StatelessWidget { final String name; Greeting({required this.name}); Widget build(BuildContext context) { return Text('Hello, $name'); } }
StatefulWidget
class Counter extends StatefulWidget { _CounterState createState() => _CounterState(); } class _CounterState extends State<Counter> { int count = 0; void increment() => setState(() => count++); Widget build(BuildContext context) { return Column( children: [ Text('Count: $count'), ElevatedButton(onPressed: increment, child: Text('Increment')), ], ); } }
3. Layout & Positioning
Column & Row
Column( children: [ Text('Top'), Text('Bottom'), ], ) Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('Left'), Text('Right'), ], )
Container
Container( padding: EdgeInsets.all(16), margin: EdgeInsets.symmetric(horizontal: 20), decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(8), ), child: Text('Styled Box'), )
Expanded & Flexible
Row( children: [ Expanded(child: Container(color: Colors.red)), Flexible(child: Container(color: Colors.green)), ], )
4. Input & Interaction
TextField
TextField( decoration: InputDecoration( labelText: 'Username', border: OutlineInputBorder(), ), )
GestureDetector
GestureDetector( onTap: () => print('Tapped'), child: Container( padding: EdgeInsets.all(12), color: Colors.amber, child: Text('Tap Me'), ), )
Buttons
ElevatedButton( onPressed: () => print('Clicked'), child: Text('Submit'), )
5. Navigation
Basic Navigation
Navigator.push( context, MaterialPageRoute(builder: (context) => SecondPage()), );
Returning Data
Navigator.pop(context, 'Result');
Named Routes
MaterialApp( routes: { '/': (context) => HomePage(), '/details': (context) => DetailsPage(), }, )
6. State Management
setState (Local State) Used in StatefulWidget to update UI.
Provider (Global State)
class Counter with ChangeNotifier { int value = 0; void increment() { value++; notifyListeners(); } }
Using Provider
Consumer<Counter>( builder: (context, counter, _) => Text('${counter.value}'), )
7. Lists & Scroll
ListView
ListView.builder( itemCount: 10, itemBuilder: (context, index) => ListTile( title: Text('Item $index'), ), )
GridView
GridView.count( crossAxisCount: 2, children: List.generate(4, (index) => Text('Grid $index')), )
8. Async & FutureBuilder
FutureBuilder
FutureBuilder<String>( future: fetchData(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) return CircularProgressIndicator(); else if (snapshot.hasError) return Text('Error: ${snapshot.error}'); else return Text('Data: ${snapshot.data}'); }, )
9. Animations
AnimatedContainer
AnimatedContainer( duration: Duration(seconds: 1), width: isExpanded ? 200 : 100, height: 100, color: Colors.blue, )
FadeTransition
FadeTransition( opacity: animation, child: Text('Fading Text'), )
10. Testing
Unit Test
test('adds one to input values', () { final calculator = Calculator(); expect(calculator.addOne(2), 3); });
Widget Test
testWidgets('Counter increments', (tester) async { await tester.pumpWidget(MyApp()); await tester.tap(find.byType(ElevatedButton)); await tester.pump(); expect(find.text('1'), findsOneWidget); });
11. Best Practices
- Use const widgets where possible
- Structure UI with reusable components
- Handle null safety with required and nullable types
- Use ThemeData for consistent styling
- Avoid deeply nested widgets—use helper methods
If you'd like this formatted as a JSON cheatsheet or integrated into a portfolio page, I can help with that too. Just say the word.