Skip to content

Instantly share code, notes, and snippets.

@panuavakul
Created April 24, 2024 12:25
Show Gist options
  • Save panuavakul/034950da01543474b0b4b1cd64104c33 to your computer and use it in GitHub Desktop.
Save panuavakul/034950da01543474b0b4b1cd64104c33 to your computer and use it in GitHub Desktop.
Flutter Stacked Items with Row/Column
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
final colors = [Colors.blue, Colors.red, Colors.yellow, Colors.amberAccent];
final reverse = colors.reversed.toList();
const heightFactor = 0.8;
const widthFactor = 0.8;
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Container(
color: Colors.black12,
child: Column(
children: [
const Text('Hello Stacked Boxes'),
const SizedBox(height: 16),
SizedBox(
child: Column(
verticalDirection: VerticalDirection.up,
mainAxisSize: MainAxisSize.min,
children: [
Align(
heightFactor: heightFactor,
child: Row(
mainAxisSize: MainAxisSize.min,
textDirection: TextDirection.rtl,
children: List.generate(
colors.length,
(index) => Align(
alignment: Alignment.centerRight,
widthFactor: widthFactor,
child: Container(
height: 50,
width: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: colors[index],
),
),
),
),
),
),
Align(
heightFactor: heightFactor,
child: Row(
mainAxisSize: MainAxisSize.min,
textDirection: TextDirection.rtl,
children: List.generate(
colors.length,
(index) => Align(
alignment: Alignment.centerRight,
widthFactor: widthFactor,
child: Container(
height: 50,
width: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: reverse[index],
),
),
),
),
),
),
],
),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment