Skip to content

Instantly share code, notes, and snippets.

@yeoupooh
Last active November 1, 2022 01:49
Show Gist options
  • Save yeoupooh/4657e1e3feed227c4b8a026cfb4915c6 to your computer and use it in GitHub Desktop.
Save yeoupooh/4657e1e3feed227c4b8a026cfb4915c6 to your computer and use it in GitHub Desktop.
flutter flex layout example
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
const MyHomePage({
Key? key,
required this.title,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
Icon _getIcon(state) {
switch (state) {
case 0:
return const Icon(Icons.remove);
case 1:
return const Icon(Icons.play_arrow);
case 2:
return const Icon(Icons.warning);
default:
return const Icon(Icons.done);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Flex(
direction: Axis.horizontal,
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 1,
child: Container(
padding: const EdgeInsets.all(16.0),
child: ListView.builder(
itemBuilder: (context, index) {
return Wrap(
crossAxisAlignment: WrapCrossAlignment.center,
children: [
_getIcon(index % 4),
Text('item$index'),
],
);
},
itemCount: 100,
),
),
),
Expanded(
flex: 1,
child: Container(
padding: const EdgeInsets.all(16.0),
child: Flex(
direction: Axis.vertical,
children: [
const Expanded(
child: Text('right1'),
),
SizedBox(
height: 20,
child: ElevatedButton(
child: const Text('button1'),
onPressed: () {
print('button1');
},
),
),
],
),
),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment