Skip to content

Commit

Permalink
Merge pull request #2 from n4sust1m/2
Browse files Browse the repository at this point in the history
2
  • Loading branch information
nasustim authored Aug 26, 2023
2 parents 3730327 + 8630ecc commit 133ea06
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 10 deletions.
20 changes: 20 additions & 0 deletions lib/domain/align/model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';

enum AlignDirection {
start,
center,
end,
}

MainAxisAlignment toMainAxisAlignment(AlignDirection ad) {
switch (ad) {
case AlignDirection.start:
return MainAxisAlignment.start;
case AlignDirection.center:
return MainAxisAlignment.center;
case AlignDirection.end:
return MainAxisAlignment.end;
default:
throw Error();
}
}
33 changes: 23 additions & 10 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_training_2023/domain/align/model.dart';
import 'package:flutter_training_2023/widgets/my_colored_box_list.dart';

void main() {
runApp(const MyApp());
Expand Down Expand Up @@ -95,23 +97,34 @@ class _MyHomePageState extends State<MyHomePage> {
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const SizedBox(
width: 50,
height: 50,
child: ColoredBox(color: Color(0xFFFFFF00)),
),
const SizedBox(
width: 100,
height: 100,
child: ColoredBox(color: Color(0xFFFF0000)),
),
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
const MyColoredBoxList(
width: 40,
height: 40,
align: AlignDirection.start,
amount: 2,
color: Color(0xFF0000FF),
),
const MyColoredBoxList(
width: 60,
height: 60,
align: AlignDirection.center,
amount: 3,
color: Color(0xFFFF0000),
),
const MyColoredBoxList(
width: 80,
height: 80,
align: AlignDirection.end,
amount: 2,
color: Color(0xFF00FF00),
),
],
),
),
Expand Down
24 changes: 24 additions & 0 deletions lib/widgets/my_colored_box.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';

class MyColoredBox extends StatelessWidget {
final Color color;
final int width;
final int height;
const MyColoredBox(
{Key? key,
required this.color,
required this.width,
required this.height})
: super(key: key);

@override
Widget build(BuildContext context) {
return SizedBox(
width: width.toDouble(),
height: height.toDouble(),
child: ColoredBox(
color: color,
),
);
}
}
42 changes: 42 additions & 0 deletions lib/widgets/my_colored_box_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';
import 'package:flutter_training_2023/domain/align/model.dart';
import 'package:flutter_training_2023/widgets/my_colored_box.dart';

class MyColoredBoxList extends StatelessWidget {
final int width;
final int height;
final AlignDirection align;
final int amount;
final Color color;
const MyColoredBoxList({
Key? key,
required this.width,
required this.height,
required this.align,
required this.amount,
required this.color,
}) : super(key: key);

@override
Widget build(BuildContext context) {
MainAxisAlignment mainAxisAlignment = toMainAxisAlignment(align);

List<Widget> children = List.filled(
amount,
[
MyColoredBox(
color: color,
width: width,
height: height,
),
const SizedBox(width: 10),
],
).expand((e) => e).toList();
children = children.sublist(0, children.length - 1); // 末尾のSizedBoxを取り除く

return Row(
mainAxisAlignment: mainAxisAlignment,
children: children,
);
}
}

0 comments on commit 133ea06

Please sign in to comment.