Skip to content
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 27 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d6c6c7e
「画像の形」のenum定義
haterain0203 Jul 22, 2023
0ff27aa
StatelessWidgetの大枠追加
haterain0203 Jul 22, 2023
7a91f64
cached_network_image: ^3.2.3の追加
haterain0203 Jul 22, 2023
043769f
汎用的な画像Widgetの定義
haterain0203 Jul 24, 2023
d2551b5
Merge branch 'main' of https://github.com/KosukeSaigusa/mottai-flutte…
haterain0203 Jul 24, 2023
f28b976
Componentsの確認ページ追加
haterain0203 Jul 24, 2023
b05087d
各パッケージでfvm flutter pub実行
haterain0203 Jul 24, 2023
19e8e6f
作成したComponentsの使用例を示すクラス追加
haterain0203 Jul 24, 2023
7bdea19
当該クラス内でのみ使用するクラスをprivate化
haterain0203 Jul 24, 2023
94a3b3e
コメント追加
haterain0203 Jul 24, 2023
0720420
printをdebugPrintに修正
haterain0203 Jul 24, 2023
c4b6f53
imageUrlが空の場合のサンプル追加
haterain0203 Jul 24, 2023
1e562cc
名前付きコンストラクタに修正
haterain0203 Jul 24, 2023
e383440
プロパティ名の修正(radius -> borderRadius)
haterain0203 Jul 24, 2023
f12a6b0
デフォルトサイズをクラス自体の変数に修正
haterain0203 Jul 24, 2023
30e2a0c
_ImageDisplayContainerをimageShapeに応じたswitch-caseに修正
haterain0203 Jul 24, 2023
fda64db
_ImageDisplayContainer.placeholderの追加
haterain0203 Jul 24, 2023
1be450f
borderRadiusの実装漏れ対応
haterain0203 Jul 24, 2023
c03f552
loading時の表示Widgetを任意で指定できるよう修正(デフォルトでは、指定された形のグレー色のWidgetを表示)
haterain0203 Jul 24, 2023
e3f09e5
三項演算子をif文に修正
haterain0203 Jul 24, 2023
6899626
Merge branch 'main' of https://github.com/KosukeSaigusa/mottai-flutte…
haterain0203 Jul 24, 2023
2c5ebdf
Merge branch 'main' of https://github.com/KosukeSaigusa/mottai-flutte…
haterain0203 Jul 26, 2023
cee51c7
errorWidgetのサンプル追加
haterain0203 Jul 26, 2023
7167511
errorWidgetも指定された形で表示するよう修正
haterain0203 Jul 26, 2023
d32565b
プロパティをloadingからloadingWidgetに修正
haterain0203 Jul 26, 2023
ec98a2b
不必要な波括弧を削除
haterain0203 Jul 26, 2023
cef4d94
Merge branch 'main' of https://github.com/KosukeSaigusa/mottai-flutte…
haterain0203 Jul 26, 2023
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
186 changes: 186 additions & 0 deletions packages/dart_flutter_common/lib/src/components/image.dart
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,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import '../../../scaffold_messenger_controller.dart';
import '../../../user/user.dart';
import '../../../user/user_mode.dart';
import '../../color/ui/color.dart';
import '../../deveopment_components/development_components.dart';
import '../../image_detail_view/image_detail_view_stub.dart';
import '../../sample_todo/ui/sample_todos.dart';

Expand Down Expand Up @@ -233,6 +234,16 @@ class DevelopmentItemsPage extends ConsumerWidget {
// ),
// ),
),
ListTile(
title: const Text('Components'),
// TODO: 後に auto_route を採用して Navigator.pushNamed を使用する予定
onTap: () => Navigator.push<void>(
context,
MaterialPageRoute<void>(
builder: (context) => const DevelopmentComponents(),
),
),
),
ListTile(
title: const Text(
'画像の詳細拡大画面サンプル',
Expand All @@ -241,7 +252,7 @@ class DevelopmentItemsPage extends ConsumerWidget {
onTap: () => Navigator.push<void>(
context,
MaterialPageRoute<void>(
builder: (context) => ImageDetailViewStubPage(),
builder: (context) => const ImageDetailViewStubPage(),
),
),
),
Expand Down
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(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[任意]

エラーのケース(もし可能なら、loading のケース)の見た目のサンプルもあると嬉しいです!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらについてはまだ対応できていませんので、追って対応します!

Copy link
Collaborator Author

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のケースは対応できていません。

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(),
],
),
);
}
}
2 changes: 1 addition & 1 deletion packages/mottai_flutter_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ packages:
source: hosted
version: "8.6.1"
cached_network_image:
dependency: transitive
dependency: "direct main"
description:
name: cached_network_image
sha256: fd3d0dc1d451f9a252b32d95d3f0c3c487bc41a75eba2e6097cb0b9c71491b15
Expand Down
1 change: 1 addition & 0 deletions packages/mottai_flutter_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ environment:

dependencies:
auto_route: ^7.7.0
cached_network_image: ^3.2.3
crypto: ^3.0.1
dart_flutter_common:
path: ../dart_flutter_common
Expand Down