Skip to content

Instantly share code, notes, and snippets.

@votruk
Created January 20, 2022 11:36
Show Gist options
  • Select an option

  • Save votruk/73967d45d6e4b31530ea66e96f1f13c9 to your computer and use it in GitHub Desktop.

Select an option

Save votruk/73967d45d6e4b31530ea66e96f1f13c9 to your computer and use it in GitHub Desktop.
Distribute evenly
import 'dart:math';
import 'package:flutter/material.dart';
main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
final columnsCount = max(5, (constraints.maxWidth / 100).floor());
final lettersWindowed = letters.window(columnsCount);
final emptyCardsAtLastRow =
columnsCount - letters.length % columnsCount;
lettersWindowed[lettersWindowed.length - 1] = lettersWindowed.last
..addAll(List.generate(emptyCardsAtLastRow, (_) => ""));
return Column(
children: lettersWindowed.map((lettersRow) {
return Expanded(
child: Row(
children: lettersRow.map(
(letter) {
return Expanded(
child: letter == ""
? const SizedBox.shrink()
: Container(
margin: const EdgeInsets.all(8),
alignment: Alignment.center,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
border: Border.all(),
),
child: Text(letter),
),
);
},
).toList(),
),
);
}).toList(),
);
},
),
),
);
}
}
const letters = [
"а",
"б",
"в",
"г",
"д",
"е",
"ё",
"ж",
"з",
"и",
"й",
"к",
"л",
"м",
"н",
"о",
"п",
"р",
"с",
"т",
"у",
"ф",
"х",
"ц",
"ч",
"ш",
"щ",
"ъ",
"ы",
"ь",
"э",
"ю",
"я",
];
extension MyList<T> on List<T> {
List<List<T>> window(final int chunkSize) {
var chunks = <List<T>>[];
for (var i = 0; i < length; i += chunkSize) {
var end = (i + chunkSize < length) ? i + chunkSize : length;
chunks.add(sublist(i, end));
}
return chunks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment