-
Notifications
You must be signed in to change notification settings - Fork 4
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
#36 create generic image widget #69
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d6c6c7e
「画像の形」のenum定義
haterain0203 0ff27aa
StatelessWidgetの大枠追加
haterain0203 7a91f64
cached_network_image: ^3.2.3の追加
haterain0203 043769f
汎用的な画像Widgetの定義
haterain0203 d2551b5
Merge branch 'main' of https://github.com/KosukeSaigusa/mottai-flutte…
haterain0203 f28b976
Componentsの確認ページ追加
haterain0203 b05087d
各パッケージでfvm flutter pub実行
haterain0203 19e8e6f
作成したComponentsの使用例を示すクラス追加
haterain0203 7bdea19
当該クラス内でのみ使用するクラスをprivate化
haterain0203 94a3b3e
コメント追加
haterain0203 0720420
printをdebugPrintに修正
haterain0203 c4b6f53
imageUrlが空の場合のサンプル追加
haterain0203 1e562cc
名前付きコンストラクタに修正
haterain0203 e383440
プロパティ名の修正(radius -> borderRadius)
haterain0203 f12a6b0
デフォルトサイズをクラス自体の変数に修正
haterain0203 30e2a0c
_ImageDisplayContainerをimageShapeに応じたswitch-caseに修正
haterain0203 fda64db
_ImageDisplayContainer.placeholderの追加
haterain0203 1be450f
borderRadiusの実装漏れ対応
haterain0203 c03f552
loading時の表示Widgetを任意で指定できるよう修正(デフォルトでは、指定された形のグレー色のWidgetを表示)
haterain0203 e3f09e5
三項演算子をif文に修正
haterain0203 6899626
Merge branch 'main' of https://github.com/KosukeSaigusa/mottai-flutte…
haterain0203 2c5ebdf
Merge branch 'main' of https://github.com/KosukeSaigusa/mottai-flutte…
haterain0203 cee51c7
errorWidgetのサンプル追加
haterain0203 7167511
errorWidgetも指定された形で表示するよう修正
haterain0203 d32565b
プロパティをloadingからloadingWidgetに修正
haterain0203 ec98a2b
不必要な波括弧を削除
haterain0203 cef4d94
Merge branch 'main' of https://github.com/KosukeSaigusa/mottai-flutte…
haterain0203 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
186 changes: 186 additions & 0 deletions
186
packages/dart_flutter_common/lib/src/components/image.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,187 @@ | ||
import 'package:cached_network_image/cached_network_image.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
enum ImageShape { | ||
circle, | ||
square, | ||
rectangle, | ||
} | ||
|
||
class GenericImageWidget extends StatelessWidget { | ||
const GenericImageWidget.circle({ | ||
required this.imageUrl, | ||
this.onTap, | ||
this.size, | ||
this.loadingWidget, | ||
super.key, | ||
}) : imageShape = ImageShape.circle, | ||
borderRadius = null, | ||
height = null, | ||
width = null; | ||
|
||
const GenericImageWidget.square({ | ||
required this.imageUrl, | ||
this.onTap, | ||
this.size, | ||
this.borderRadius, | ||
this.loadingWidget, | ||
super.key, | ||
}) : imageShape = ImageShape.square, | ||
height = null, | ||
width = null; | ||
|
||
const GenericImageWidget.reqtangle({ | ||
required this.imageUrl, | ||
this.onTap, | ||
required this.height, | ||
required this.width, | ||
this.borderRadius, | ||
this.loadingWidget, | ||
super.key, | ||
}) : imageShape = ImageShape.rectangle, | ||
size = null; | ||
|
||
final String imageUrl; | ||
final ImageShape imageShape; | ||
final VoidCallback? onTap; | ||
final double? size; | ||
final double? height; | ||
final double? width; | ||
final double? borderRadius; | ||
final Widget? loadingWidget; | ||
@override | ||
Widget build(BuildContext context) { | ||
if (imageUrl.isEmpty) { | ||
return _ImageDisplayContainer.placeholder( | ||
imageShape: imageShape, | ||
size: size, | ||
height: height, | ||
width: width, | ||
borderRadius: borderRadius, | ||
); | ||
} | ||
return GestureDetector( | ||
onTap: onTap, | ||
child: CachedNetworkImage( | ||
imageUrl: imageUrl, | ||
imageBuilder: (context, imageProvider) { | ||
return _ImageDisplayContainer( | ||
imageShape: imageShape, | ||
size: size, | ||
height: height, | ||
width: width, | ||
borderRadius: borderRadius, | ||
decorationImage: DecorationImage( | ||
image: imageProvider, | ||
fit: BoxFit.cover, | ||
), | ||
); | ||
}, | ||
placeholder: (context, url) => | ||
loadingWidget ?? | ||
_ImageDisplayContainer.placeholder( | ||
imageShape: imageShape, | ||
size: size, | ||
height: height, | ||
width: width, | ||
borderRadius: borderRadius, | ||
), | ||
errorWidget: (context, url, error) => | ||
_ImageDisplayContainer.errorWidget( | ||
imageShape: imageShape, | ||
size: size, | ||
height: height, | ||
width: width, | ||
borderRadius: borderRadius, | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class _ImageDisplayContainer extends StatelessWidget { | ||
const _ImageDisplayContainer({ | ||
required this.imageShape, | ||
this.size, | ||
this.height, | ||
this.width, | ||
this.borderRadius, | ||
this.decorationImage, | ||
}) : color = null, | ||
icon = null; | ||
|
||
const _ImageDisplayContainer.placeholder({ | ||
required this.imageShape, | ||
this.size, | ||
this.height, | ||
this.width, | ||
this.borderRadius, | ||
}) : color = Colors.grey, | ||
decorationImage = null, | ||
icon = null; | ||
|
||
const _ImageDisplayContainer.errorWidget({ | ||
required this.imageShape, | ||
this.size, | ||
this.height, | ||
this.width, | ||
this.borderRadius, | ||
}) : color = Colors.grey, | ||
decorationImage = null, | ||
icon = const Icon( | ||
Icons.broken_image, | ||
color: Colors.white, | ||
); | ||
|
||
final ImageShape imageShape; | ||
final Color? color; | ||
final double? size; | ||
final double? height; | ||
final double? width; | ||
final double? borderRadius; | ||
final DecorationImage? decorationImage; | ||
final Icon? icon; | ||
|
||
static const double _defaultSize = 64; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
switch (imageShape) { | ||
case ImageShape.circle: | ||
return Container( | ||
height: size ?? _defaultSize, | ||
width: size ?? _defaultSize, | ||
decoration: BoxDecoration( | ||
color: color, | ||
shape: BoxShape.circle, | ||
image: decorationImage, | ||
), | ||
child: icon, | ||
); | ||
|
||
case ImageShape.square: | ||
return Container( | ||
height: size ?? _defaultSize, | ||
width: size ?? _defaultSize, | ||
decoration: BoxDecoration( | ||
color: color, | ||
image: decorationImage, | ||
borderRadius: BorderRadius.circular(borderRadius ?? 0), | ||
), | ||
child: icon, | ||
); | ||
|
||
case ImageShape.rectangle: | ||
return Container( | ||
height: height, | ||
width: width, | ||
decoration: BoxDecoration( | ||
color: color, | ||
image: decorationImage, | ||
borderRadius: BorderRadius.circular(borderRadius ?? 0), | ||
), | ||
child: icon, | ||
); | ||
} | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
...ages/mottai_flutter_app/lib/development/deveopment_components/development_components.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import 'package:dart_flutter_common/dart_flutter_common.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
class DevelopmentComponents extends StatelessWidget { | ||
const DevelopmentComponents({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text('開発中のComponents'), | ||
), | ||
body: Column( | ||
children: [ | ||
Column( | ||
children: [ | ||
const Text('汎用的な画像Widget'), | ||
const GenericImageWidget.circle( | ||
imageUrl: '', | ||
), | ||
const SizedBox( | ||
height: 10, | ||
), | ||
const GenericImageWidget.circle( | ||
imageUrl: 'https://picsum.photos/128', | ||
), | ||
const SizedBox( | ||
height: 10, | ||
), | ||
const GenericImageWidget.square( | ||
imageUrl: 'https://picsum.photos/200', | ||
size: 100, | ||
), | ||
const SizedBox( | ||
height: 10, | ||
), | ||
GenericImageWidget.reqtangle( | ||
imageUrl: 'https://picsum.photos/100', | ||
height: 100, | ||
width: 250, | ||
borderRadius: 10, | ||
onTap: () => debugPrint('onTap'), | ||
), | ||
const SizedBox( | ||
height: 10, | ||
), | ||
// errorWidgetのサンプル | ||
GenericImageWidget.reqtangle( | ||
imageUrl: 'https://testinvalidurl.com', | ||
height: 100, | ||
width: 250, | ||
borderRadius: 10, | ||
onTap: () => debugPrint('onTap'), | ||
), | ||
], | ||
), | ||
const Divider(), | ||
], | ||
), | ||
); | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[任意]
エラーのケース(もし可能なら、loading のケース)の見た目のサンプルもあると嬉しいです!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらについてはまだ対応できていませんので、追って対応します!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cee51c7
にてエラーのケースのサンプルを追加しました!
7167511
で、エラーケースの場合は指定された形やサイズの中にアイコンを表示するようにしてみましたが、いかがでしょう?
(元々はアイコンのみ表示される仕様にしていた)
loading のケースは、GPT先生に相談して
http://deelay.me/
というサービスを教えてもらったんですが、今は動作してないようでした・・・
他に実現できる方法がわからなかったので、loadingのケースは対応できていません。