refactor: use list extension

This commit is contained in:
Rongjian Zhang 2022-10-08 00:46:34 +08:00
parent b65ce98077
commit 7ca3fe03c5
5 changed files with 66 additions and 83 deletions

View File

@ -30,9 +30,7 @@ class GhTrendingScreen extends StatelessWidget {
bodyBuilder: (payload, activeTab) {
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: join(
CommonStyle.border,
activeTab == 0
children: (activeTab == 0
? [
for (var v in payload.cast<GithubTrendingRepository>())
RepoItem.gh(
@ -82,8 +80,8 @@ class GhTrendingScreen extends StatelessWidget {
),
),
)
],
),
])
.withSeparator(CommonStyle.border),
);
},
);

View File

@ -77,32 +77,17 @@ class GithubPalette {
// final pageSize = 5;
const kPageSize = 30;
List<T> join<T>(T seperator, List<T> xs) {
extension MyList on List {
List<T> withSeparator<T>(T separator) {
final result = <T>[];
xs.asMap().forEach((index, x) {
if (x == null) return;
result.add(x);
if (index < xs.length - 1) {
result.add(seperator);
for (var i = 0; i < length; i++) {
if (i != 0) {
result.add(separator);
}
result.add(this[i]);
}
});
return result;
}
List<T> joinAll<T>(T seperator, List<List<T>> xss) {
final result = <T>[];
xss.asMap().forEach((index, x) {
if (x.isEmpty) return;
result.addAll(x);
if (index < xss.length - 1) {
result.add(seperator);
}
});
return result;
}
final numberFormat = NumberFormat();

View File

@ -56,7 +56,7 @@ class EventItem extends StatelessWidget {
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: join(const SizedBox(height: 6), [
children: [
Text.rich(
TextSpan(
style: TextStyle(
@ -81,7 +81,7 @@ class EventItem extends StatelessWidget {
],
),
if (card != null) card
]),
].withSeparator(const SizedBox(height: 6)),
),
),
],
@ -233,7 +233,7 @@ class EventItem extends StatelessWidget {
borderRadius: const BorderRadius.all(Radius.circular(4))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: join(const SizedBox(height: 6), [
children: [
Row(
children: <Widget>[
IssueIcon(state, size: 20),
@ -285,7 +285,7 @@ class EventItem extends StatelessWidget {
]
],
)
]),
].withSeparator(const SizedBox(height: 6)),
),
),
);

View File

@ -68,7 +68,7 @@ class IssueItem extends StatelessWidget {
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: join(const SizedBox(height: 8), [
children: [
Text.rich(
TextSpan(
children: [
@ -129,7 +129,7 @@ class IssueItem extends StatelessWidget {
],
),
)
]),
].withSeparator(const SizedBox(height: 8)),
),
),
// Column(

View File

@ -30,7 +30,7 @@ class RepoHeader extends StatelessWidget {
padding: CommonStyle.padding,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: join(const SizedBox(height: 12), [
children: [
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
@ -73,7 +73,7 @@ class RepoHeader extends StatelessWidget {
),
),
if (trailings != null) ...trailings!
]),
].withSeparator(const SizedBox(height: 12)),
),
);
}