Getting Started
3 snippetsBasic Flutter app structure
Main Entry Point
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}Basic App Structure
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: const HomePage(),
);
}
}Scaffold Widget
Scaffold(
appBar: AppBar(
title: const Text('My App'),
),
body: const Center(
child: Text('Hello, Flutter!'),
),
)Common Widgets
6 snippetsEssential UI building blocks
Text Widget
Text(
'Hello, Flutter!',
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
)Container
Container(
width: 100,
height: 100,
padding: const EdgeInsets.all(16),
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8),
),
child: const Text('Box'),
)Row & Column
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(Icons.star),
Text('Rating'),
Icon(Icons.star),
],
)
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Title'),
Text('Subtitle'),
],
)Image
// Network image
Image.network(
'https://example.com/image.png',
width: 200,
height: 200,
)
// Asset image
Image.asset('assets/logo.png')Icon
Icon(
Icons.favorite,
color: Colors.red,
size: 32,
)Button Widgets
ElevatedButton(
onPressed: () {
print('Pressed');
},
child: const Text('Click Me'),
)
TextButton(
onPressed: () {},
child: const Text('Text Button'),
)
IconButton(
icon: const Icon(Icons.add),
onPressed: () {},
)Layout Widgets
6 snippetsPositioning and sizing widgets
Center
Center(
child: Text('Centered Text'),
)Padding
Padding(
padding: const EdgeInsets.all(16.0),
child: Text('Padded Text'),
)
// Specific edges
Padding(
padding: const EdgeInsets.only(
left: 16,
top: 8,
),
child: Text('Custom Padding'),
)Stack
Stack(
children: [
Container(color: Colors.blue, width: 200, height: 200),
Positioned(
top: 20,
left: 20,
child: Text('Overlay'),
),
],
)Expanded & Flexible
Row(
children: [
Expanded(
flex: 2,
child: Container(color: Colors.blue),
),
Expanded(
flex: 1,
child: Container(color: Colors.red),
),
],
)ListView
ListView(
children: [
ListTile(title: Text('Item 1')),
ListTile(title: Text('Item 2')),
ListTile(title: Text('Item 3')),
],
)
// ListView.builder for dynamic lists
ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
return ListTile(title: Text(items[index]));
},
)GridView
GridView.count(
crossAxisCount: 2,
children: List.generate(6, (index) {
return Card(
child: Center(
child: Text('Item $index'),
),
);
}),
)Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Stateful Widgets
4 snippetsManaging state in widgets
StatefulWidget Template
class Counter extends StatefulWidget {
const Counter({super.key});
@override
State<Counter> createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _count = 0;
@override
Widget build(BuildContext context) {
return Text('$_count');
}
}setState
void _incrementCounter() {
setState(() {
_count++;
});
}Lifecycle Methods
@override
void initState() {
super.initState();
// Initialize state
}
@override
void dispose() {
// Cleanup
super.dispose();
}TextEditingController
class MyForm extends StatefulWidget {
@override
State<MyForm> createState() => _MyFormState();
}
class _MyFormState extends State<MyForm> {
final _controller = TextEditingController();
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return TextField(
controller: _controller,
);
}
}Forms & Input
3 snippetsUser input handling
TextField
TextField(
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Username',
hintText: 'Enter your username',
),
onChanged: (value) {
print(value);
},
)Form Validation
final _formKey = GlobalKey<FormState>();
Form(
key: _formKey,
child: Column(
children: [
TextFormField(
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
// Process form
}
},
child: const Text('Submit'),
),
],
),
)Checkbox & Switch
Checkbox(
value: _isChecked,
onChanged: (bool? value) {
setState(() {
_isChecked = value!;
});
},
)
Switch(
value: _isEnabled,
onChanged: (bool value) {
setState(() {
_isEnabled = value;
});
},
)Async & HTTP
3 snippetsAsynchronous operations and API calls
FutureBuilder
FutureBuilder<String>(
future: fetchData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data!);
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
}
return const CircularProgressIndicator();
},
)HTTP GET Request
// pubspec.yaml: http: ^1.1.0
import 'package:http/http.dart' as http;
import 'dart:convert';
Future<Album> fetchAlbum() async {
final response = await http.get(
Uri.parse('https://api.example.com/albums/1'),
);
if (response.statusCode == 200) {
return Album.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}HTTP POST Request
Future<Album> createAlbum(String title) async {
final response = await http.post(
Uri.parse('https://api.example.com/albums'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'title': title,
}),
);
if (response.statusCode == 201) {
return Album.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to create album');
}
}State Management
4 snippetsProvider pattern basics
ChangeNotifier
// pubspec.yaml: provider: ^6.0.0
import 'package:flutter/foundation.dart';
class Counter extends ChangeNotifier {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
}ChangeNotifierProvider
import 'package:provider/provider.dart';
void main() {
runApp(
ChangeNotifierProvider(
create: (context) => Counter(),
child: const MyApp(),
),
);
}Consumer
Consumer<Counter>(
builder: (context, counter, child) {
return Text('${counter.count}');
},
)Provider.of
final counter = Provider.of<Counter>(context);
ElevatedButton(
onPressed: counter.increment,
child: const Text('Increment'),
)