Skip to content

Instantly share code, notes, and snippets.

@imaNNeo
Created September 21, 2020 21:09
Show Gist options
  • Save imaNNeo/344a0ab766d99f418739cb3aca8a667c to your computer and use it in GitHub Desktop.
Save imaNNeo/344a0ab766d99f418739cb3aca8a667c to your computer and use it in GitHub Desktop.
Flutter4fun UI Challenge 1 - Seats
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: HomePage(),
);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0x060524),
body: Center(
child: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 22, vertical: 40),
child: SeatsGrid(),
),
),
),
);
}
}
enum SeatState {
Available,
Selected,
Reserved,
None,
}
class SeatsGrid extends StatelessWidget {
final List<List<SeatState>> seats = getSampleSeats();
@override
Widget build(BuildContext context) {
final rows = seats
.map((columnsSeatState) => Row(
children: makeColumns(columnsSeatState),
mainAxisSize: MainAxisSize.min,
))
.toList();
return Column(
mainAxisSize: MainAxisSize.min,
children: rows,
);
}
List<Widget> makeColumns(List<SeatState> seats) {
return seats.map((seatState) => makeSeat(seatState)).toList();
}
}
Widget makeSeat(SeatState seat) {
double size = 12, margin = 4, radius = 2;
switch (seat) {
case SeatState.None:
return Container(
margin: EdgeInsets.all(margin),
width: size,
height: size,
color: Colors.transparent,
);
case SeatState.Available:
return Container(
margin: EdgeInsets.all(margin),
width: size,
height: size,
decoration: BoxDecoration(
border: Border.all(color: Colors.blue),
borderRadius: BorderRadius.all(Radius.circular(radius)),
),
);
case SeatState.Reserved:
return Container(
margin: EdgeInsets.all(margin),
width: size,
height: size,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(radius)),
color: Colors.blue,
),
);
case SeatState.Selected:
return Container(
margin: EdgeInsets.all(margin),
width: size,
height: size,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(radius)),
color: Colors.purple,
),
);
default: throw ArgumentError();
}
}
List<List<SeatState>> getSampleSeats() {
const int none = 0, available = 1, reserved = 2, selected = 3;
return [
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 0],
[1, 1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 1],
[1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2],
[1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2],
[0, 1, 1, 2, 1, 1, 3, 3, 1, 2, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 0],
[0, 1, 1, 2, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 0],
].map((row) => row.map((columnNumber) {
SeatState state;
switch (columnNumber) {
case none: state = SeatState.None; break;
case available: state = SeatState.Available; break;
case reserved: state = SeatState.Reserved; break;
case selected: state = SeatState.Selected; break;
}
return state;
}).toList())
.toList();
}
@imaNNeo
Copy link
Author

imaNNeo commented Sep 21, 2020

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment