AI Generate Flutter docs instantly

Flutter Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

3 snippets

Basic 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 snippets

Essential 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 snippets

Positioning 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.

Try Free

Stateful Widgets

4 snippets

Managing 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 snippets

User 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 snippets

Asynchronous 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 snippets

Provider 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'),
)

More Cheat Sheets

FAQ

Frequently asked questions

What is a Flutter cheat sheet?

A Flutter cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in Flutter. It helps developers quickly look up syntax without searching through documentation.

How do I learn Flutter quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important Flutter concepts?

Key Flutter concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my Flutter code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your Flutter code using AI.

Code Conversion Tools

Convert Flutter to Other Languages

Easily translate your Flutter code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate Flutter Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week