Last active
March 22, 2023 12:45
-
-
Save RahmiTufanoglu/d8cc54f09a18224d08d3c50f75938307 to your computer and use it in GitHub Desktop.
IterableExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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