Skip to content

Commit 37b89ab

Browse files
committed
refactor: avatar loading
1 parent e2ac1af commit 37b89ab

File tree

4 files changed

+16
-27
lines changed

4 files changed

+16
-27
lines changed

lib/ui/activity/activity_tab.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22

33
import 'package:F4Lab/gitlab_client.dart';
4+
import 'package:F4Lab/util/widget_util.dart';
45
import 'package:F4Lab/widget/comm_ListView.dart';
56
import 'package:flutter/material.dart';
67
import 'package:flutter/widgets.dart';
@@ -41,9 +42,7 @@ class FeedState extends CommListState<TabActivity> {
4142
final item = data[index];
4243
return Card(
4344
child: ListTile(
44-
leading: CircleAvatar(
45-
backgroundImage: NetworkImage(item['avatar']),
46-
),
45+
leading: loadAvatar(item['avatar'], item['title']),
4746
title: Text(item['title']),
4847
onTap: () {},
4948
),

lib/ui/project/project_tabs.dart

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:F4Lab/model/project.dart' as model;
22
import 'package:F4Lab/ui/project/project_detail.dart';
3+
import 'package:F4Lab/util/widget_util.dart';
34
import 'package:F4Lab/widget/comm_ListView.dart';
45
import 'package:flutter/material.dart';
56

@@ -41,20 +42,11 @@ class ProjectState extends CommListState<ProjectTab> {
4142
@override
4243
Widget childBuild(BuildContext context, int index) {
4344
final item = model.Project.fromJson(data[index]);
45+
final name = item.name;
46+
final color = Theme.of(context).primaryColor;
4447
return Card(
4548
child: ListTile(
46-
leading: item.avatarUrl != null
47-
? CircleAvatar(
48-
backgroundImage: NetworkImage(item.avatarUrl),
49-
backgroundColor: Theme.of(context).primaryColor,
50-
)
51-
: CircleAvatar(
52-
backgroundColor: Theme.of(context).primaryColor,
53-
child: Text(
54-
item.name.substring(0, 1).toUpperCase(),
55-
style: TextStyle(color: Theme.of(context).accentColor),
56-
),
57-
),
49+
leading: loadAvatar(item.avatarUrl ?? null, name, color: color),
5850
title: Text(item.nameWithNamespace),
5951
subtitle: Text(item.description ?? item.lastActivityAt),
6052
trailing: item.defaultBranch != null

lib/ui/todo/todo_tab.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:F4Lab/model/todo.dart' as TodoModel;
2+
import 'package:F4Lab/util/widget_util.dart';
23
import 'package:F4Lab/widget/comm_ListView.dart';
34
import 'package:flutter/material.dart';
45

@@ -13,9 +14,7 @@ class TodoState extends CommListState<TabTodo> {
1314
final todoItem = TodoModel.Todo.fromJson(data[index]);
1415
return Card(
1516
child: ExpansionTile(
16-
leading: CircleAvatar(
17-
backgroundImage: NetworkImage(todoItem.author.avatarUrl),
18-
),
17+
leading: loadAvatar(todoItem.author.avatarUrl, todoItem.author.name),
1918
title: Text.rich(TextSpan(
2019
text: "${todoItem.author.name} ",
2120
style: TextStyle(fontWeight: FontWeight.w100),

lib/util/widget_util.dart

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ import 'package:flutter/material.dart';
33
Widget loadAvatar(String url, String name, {Color color = Colors.teal}) {
44
assert(name != null);
55
if (url != null) {
6+
debugPrint("[loadAvatar] Start load: $url");
67
NetworkImage image;
7-
try {
8-
image = new NetworkImage(url);
9-
return CircleAvatar(
10-
backgroundImage: image,
11-
);
12-
} catch (Exception) {
13-
print("Load avatar error: $Exception");
14-
}
8+
image = new NetworkImage(url);
9+
return CircleAvatar(
10+
backgroundImage: image,
11+
backgroundColor: color,
12+
);
1513
}
1614
return new CircleAvatar(
1715
child: Text(
1816
name,
1917
textAlign: TextAlign.center,
20-
overflow: TextOverflow.fade,
18+
overflow: TextOverflow.ellipsis,
19+
softWrap: true,
2120
),
2221
backgroundColor: color,
2322
);

0 commit comments

Comments
 (0)