Skip to content

Instantly share code, notes, and snippets.

@mono0926
Last active February 3, 2023 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0926/4046ed33de0b9ca775e596d5864e8bb2 to your computer and use it in GitHub Desktop.
Save mono0926/4046ed33de0b9ca775e596d5864e8bb2 to your computer and use it in GitHub Desktop.
Flex VS Row/Column
import 'package:flutter/material.dart';
void main() => runApp(const App());
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
var _isCompact = true;
@override
Widget build(BuildContext context) {
final children = List.generate(
5,
(index) => _ColorfulBox(index: index),
);
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Row(
children: [
Expanded(
child: Flex(
direction: _isCompact ? Axis.vertical : Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.center,
children: children,
),
),
const VerticalDivider(),
Expanded(
child: _isCompact
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: children,
)
: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: children,
),
),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(() {
_isCompact = !_isCompact;
}),
child: const Icon(Icons.refresh),
),
);
}
}
class _ColorfulBox extends StatefulWidget {
const _ColorfulBox({required this.index});
final int index;
@override
State<_ColorfulBox> createState() => _ColorfulBoxState();
}
class _ColorfulBoxState extends State<_ColorfulBox> {
final _colors = [
Colors.red,
Colors.green,
Colors.blue,
Colors.yellow,
Colors.purple,
Colors.orange,
Colors.pink,
Colors.cyan,
Colors.lime,
Colors.teal,
Colors.indigo,
Colors.brown,
Colors.grey,
];
late var _index = widget.index;
@override
Widget build(BuildContext context) {
_index++;
return Container(
width: 50,
height: 50,
color: _colors[_index % _colors.length],
alignment: Alignment.center,
child: Text('$_index'),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment