Irbisa · Cheatsheet
Basic Widgets
Core layout widgets: Container, Row, Column, Stack, ListView
What is the difference between
Container,SizedBox,PaddingandColoredBox?What happens when this
Containeris built, and how do you fix it?Container( color: Colors.white, padding: const EdgeInsets.all(16), decoration: BoxDecoration( borderRadius: BorderRadius.circular(12), ), child: const Text('Card'),)In
RowandColumn, what domainAxisAlignment,crossAxisAlignmentandmainAxisSizecontrol?Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: TextBaseline.alphabetic, // required for baseline children: [ Text('Total', style: Theme.of(context).textTheme.titleMedium),…What is the difference between
ExpandedandFlexible?// Expanded: both panels fill the row, 1/3 and 2/3Row( children: [ Expanded(child: Container(color: Colors.red)), Expanded(flex: 2, child: Container(color: Colors.blue)), ],…What do
AspectRatio,FittedBoxandConstrainedBoxdo, and when do you use each?// 16:9 box at any screen widthAspectRatio( aspectRatio: 16 / 9, child: Image.network(thumbnailUrl, fit: BoxFit.cover),)…How does
Stackwork withPositioned, and when do you use it instead ofRoworColumn?// A caption over a photo: the image sets the Stack's sizeStack( children: [ Image.asset('assets/cover.jpg'), const Positioned.fill( child: DecoratedBox(…What does
Wrapdo, and how is it different from aRowthat overflows?Wrap( spacing: 8, // between chips in a line runSpacing: 8, // between lines children: [ for (final tag in tags) Chip(label: Text(tag)), ],…This row works for short names but breaks on long ones. What error appears, and how do you fix it?
Row( children: const [ Icon(Icons.person), SizedBox(width: 8), Text('Konstantin Konstantinopolsky-Vasilevsky, Senior Software Engineer, Platform Team'), Icon(Icons.chevron_right), ],)What does
SafeAreado, and where should you put it?What is the difference between
ListView(children: ...),ListView.builderand aColumnin aSingleChildScrollView?// Data-driven list: rows are built only when they come into viewListView.builder( itemCount: messages.length, itemExtent: 72, // every row is 72 px: cheap jumps and scrollbar itemBuilder: (context, index) => MessageTile(message: messages[index]),)…What is the difference between
Visibility,OpacityandOffstage?What are the Material 3 button widgets, and when do you pick each?
Why does an
InkWellinside a colouredContainershow no ripple, and what do you use instead?// No visual feedback; opaque makes the whole padded box tappableGestureDetector( behavior: HitTestBehavior.opaque, onTap: _select, child: const Padding(padding: EdgeInsets.all(12), child: Text('Tap me')),)…How do you show images from assets, the network, memory and a file, and how is
Image.networkdifferent fromNetworkImage?// pubspec.yaml -> flutter: assets: [assets/images/]Image.asset('assets/images/logo.png', width: 96) Image.network( 'https://cdn.example.com/avatar.jpg', width: 64,…How do you show a
SnackBar, and what isScaffoldMessengerfor?ScaffoldMessenger.of(context) ..hideCurrentSnackBar() ..showSnackBar( SnackBar( content: const Text('Message archived'), behavior: SnackBarBehavior.floating,…How do you build tabs with
TabBar,TabBarViewandDefaultTabController?DefaultTabController( length: 3, child: Scaffold( appBar: AppBar( title: const Text('Orders'), bottom: const TabBar(…Explain "constraints go down, sizes go up, parent sets position". Why is
width: 300on aContainersometimes ignored?// wrong: SizedBox.expand forces the full size, so 300 is clampedSizedBox.expand( child: Container(width: 300, height: 120, color: Colors.teal), // fills everything); // right: Center loosens the constraints, so the child may be smaller…Putting a
ListViewinside aColumnthrows a layout error. What causes it, and what are the fixes?// wrong: Vertical viewport was given unbounded heightColumn( children: [ const ProfileHeader(), ListView.builder(itemCount: items.length, itemBuilder: _row), ],…How does
GridViewdecide a tile's height, and why do tiles overflow on a narrower phone?// wrong: height comes from width, so large text or a narrow phone overflowsGridView.builder( gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, childAspectRatio: 0.72, ),…Tapping this button throws "Scaffold.of() called with a context that does not contain a Scaffold". Why, and how do you fix it?
class HomePage extends StatelessWidget { const HomePage({super.key}); @override Widget build(BuildContext context) { return Scaffold( drawer: const Drawer(child: Text('Menu')), body: Center( child: TextButton( onPressed: () => Scaffold.of(context).openDrawer(), child: const Text('Open menu'), ), ), ); }}Find the bug: after switching to another bottom tab and back, the feed jumps to the top and loads its data again.
class _HomeShellState extends State<HomeShell> { int _index = 0; static const _pages = [FeedPage(), SearchPage(), ProfilePage()]; @override Widget build(BuildContext context) { return Scaffold( body: _pages[_index], bottomNavigationBar: NavigationBar( selectedIndex: _index, onDestinationSelected: (i) => setState(() => _index = i), destinations: _destinations, ), ); }}What is the difference between implicit and explicit animations, and which should you reach for first?
// Implicit: change the value, Flutter animates to itAnimatedContainer( duration: const Duration(milliseconds: 250), curve: Curves.easeOut, width: _expanded ? 240 : 120, color: _expanded ? Colors.teal : Colors.grey,…How does
CustomPainterwork, and when would you use it?import 'dart:math' as math; class RingPainter extends CustomPainter { RingPainter({required this.progress, required this.color}); final double progress; // 0.0 to 1.0…Find the bug: this upload ring stays at its first value, although the parent calls
setStateand passes a newRingPainter(_progress).class RingPainter extends CustomPainter { RingPainter(this.progress); final double progress; // 0.0 to 1.0 @override void paint(Canvas canvas, Size size) { final paint = Paint() ..style = PaintingStyle.stroke ..strokeWidth = 8; final rect = (Offset.zero & size).deflate(4); canvas.drawArc(rect, -math.pi / 2, progress * 2 * math.pi, false, paint); } @override bool shouldRepaint(RingPainter oldDelegate) => false;}