Skip to content

Instantly share code, notes, and snippets.

@RahmiTufanoglu
Last active March 22, 2023 12:45
Show Gist options
  • Save RahmiTufanoglu/d8cc54f09a18224d08d3c50f75938307 to your computer and use it in GitHub Desktop.
Save RahmiTufanoglu/d8cc54f09a18224d08d3c50f75938307 to your computer and use it in GitHub Desktop.
IterableExtension
import 'package:flutter/widgets.dart';
extension IterableExtension on Iterable<Widget> {
// This extension method generates a new Iterable of Widgets that inserts
// a given separator between each element in the original Iterable.
Iterable<Widget> separatedBy(Widget separator) sync* {
bool isFirst = true;
for (final widget in this) {
if (!isFirst) yield separator;
yield widget;
isFirst = false;
}
}
}
// Example:
Row(
children: [
const Text('XXX', style: TextStyle(color: Colors.white)),
const Text('XXX', style: TextStyle(color: Colors.white)),
const Text('XXX', style: TextStyle(color: Colors.white)),
const Text('XXX', style: TextStyle(color: Colors.white)),
const Text('XXX', style: TextStyle(color: Colors.white)),
].separatedBy(const SizedBox(width: 20.0)).toList(),
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment