Skip to content

Implemented customBuilder #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions file_icon/lib/file_icon.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import 'package:flutter/widgets.dart';
import 'src/data.dart';

typedef CustomBuilder = Widget? Function(String? key, Icon icon);

class FileIcon extends StatelessWidget {
final String fileName;
final double size;
final double? size;

/// This color will override the color provided from Seti icons
final Color? color;

/// Use this widget to render custom widget by checking the key.
/// It returns recognized file extension as a [key], and [icon] widget which will be rendered.
/// This can be used to show custom icon when file type is unknown ([key] is null)
final CustomBuilder? customBuilder;

FileIcon(String fileName, {this.size})
: this.fileName = fileName.toLowerCase();
FileIcon(String fileName, {this.size, this.color, this.customBuilder}) : this.fileName = fileName.toLowerCase();

@override
Widget build(BuildContext context) {
String key;
String? key;

if (iconSetMap.containsKey(fileName)) {
key = fileName;
Expand All @@ -26,18 +35,16 @@ class FileIcon extends StatelessWidget {
}
}

if (key == null) {
key = '.txt';
}

return Icon(
final Icon iconWidget = Icon(
IconData(
iconSetMap[key].codePoint,
iconSetMap[key ?? '.txt']!.codePoint,
fontFamily: 'Seti',
fontPackage: 'file_icon',
),
color: Color(iconSetMap[key].color),
color: color ?? Color(iconSetMap[key ?? '.txt']!.color),
size: size,
);

return customBuilder != null ? (customBuilder!(key, iconWidget) ?? iconWidget) : iconWidget;
}
}