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

feat: #2396 - new widget SmoothProductCardTemplate #2593

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ class SmoothProductCardFound extends StatelessWidget {
child: Ink(
decoration: BoxDecoration(
borderRadius: ROUNDED_BORDER_RADIUS,
color: Theme.of(context).brightness == Brightness.light
? Colors.white
: Colors.black,
color:
backgroundColor ?? (isDarkMode ? Colors.black : Colors.white),
),
child: SmoothCard(
elevation: elevation,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'package:flutter/material.dart';
import 'package:smooth_app/cards/product_cards/smooth_product_card_found.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_card.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_product_image_container.dart';
import 'package:smooth_app/helpers/ui_helpers.dart';

/// Empty template for a product card display.
///
/// Based on the "real" [SmoothProductCardFound].
class SmoothProductCardTemplate extends StatelessWidget {
const SmoothProductCardTemplate();

@override
Widget build(BuildContext context) {
final Size screenSize = MediaQuery.of(context).size;
final ThemeData themeData = Theme.of(context);
final bool isDarkMode = themeData.colorScheme.brightness == Brightness.dark;
final Color itemColor = isDarkMode ? PRIMARY_GREY_COLOR : LIGHT_GREY_COLOR;
final Color backgroundColor = isDarkMode ? Colors.black : Colors.white;
final double iconSize = IconWidgetSizer.getIconSizeFromContext(context);
final Widget textWidget = Container(
width: screenSize.width * .4,
height: screenSize.width * .05,
Comment on lines +23 to +24
Copy link
Member

Choose a reason for hiding this comment

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

Some magic numbers, what happenes when someone changes the font size

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't care about the font size, I just want 3 lines that fit in width * .2 as in the original card.

color: itemColor,
);
// In the actual display, it's a 240x130 svg resized with iconSize
final Widget svgWidget = Container(
height: iconSize * .9,
width: 240 * iconSize / 130,
Comment on lines +29 to +30
Copy link
Member

Choose a reason for hiding this comment

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

I guess you took these values from the original card. We should store them in a const

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair enough, but I don't know how to do that cleanly. You see, the 240x130 ratio comes from the svg files themselves.
The general idea is to have a template that looks similar to a real product card, not to match it in every detail, especially for a widget that is transient.

color: itemColor,
);
return Container(
decoration: BoxDecoration(
borderRadius: ROUNDED_BORDER_RADIUS,
color: backgroundColor,
),
child: SmoothCard(
elevation: SmoothProductCardFound.elevation,
color: Colors.transparent,
padding: const EdgeInsets.all(VERY_SMALL_SPACE),
child: Row(
children: <Widget>[
SmoothProductImageContainer(
width: screenSize.width * 0.20,
height: screenSize.width * 0.20,
child: Container(color: itemColor),
),
const Padding(padding: EdgeInsets.only(left: VERY_SMALL_SPACE)),
Expanded(
child: SizedBox(
height: screenSize.width * 0.2,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
textWidget,
textWidget,
textWidget,
],
),
),
),
const Padding(padding: EdgeInsets.only(left: VERY_SMALL_SPACE)),
Padding(
padding: const EdgeInsets.all(VERY_SMALL_SPACE),
child: Column(
children: <Widget>[
svgWidget,
Container(height: iconSize * .2),
svgWidget,
],
),
),
],
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_product_image_container.dart';

/// Main product image on a product card.
class SmoothProductImage extends StatelessWidget {
const SmoothProductImage({
required this.product,
Expand All @@ -25,51 +26,40 @@ class SmoothProductImage extends StatelessWidget {
if (result != null) {
return result;
}
return ClipRRect(
borderRadius: ROUNDED_BORDER_RADIUS,
child: FittedBox(
child: Container(
width: width,
height: height,
decoration: const BoxDecoration(
borderRadius: ROUNDED_BORDER_RADIUS,
),
child: Center(
child: SvgPicture.asset(
'assets/product/product_not_found.svg',
fit: BoxFit.cover,
),
),
return SmoothProductImageContainer(
width: width,
height: height,
child: Center(
child: SvgPicture.asset(
'assets/product/product_not_found.svg',
fit: BoxFit.cover,
),
),
);
}

Widget? _buildFromUrl(final String? url) => url == null || url.isEmpty
? null
: ClipRRect(
borderRadius: ROUNDED_BORDER_RADIUS,
child: SizedBox(
width: width,
height: height,
child: Image.network(
url,
fit: BoxFit.contain,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent? progress) =>
progress == null
? child
: Center(
child: CircularProgressIndicator(
strokeWidth: 2.5,
valueColor: const AlwaysStoppedAnimation<Color>(
Colors.white,
),
value: progress.cumulativeBytesLoaded /
progress.expectedTotalBytes!,
: SmoothProductImageContainer(
width: width,
height: height,
child: Image.network(
url,
fit: BoxFit.contain,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent? progress) =>
progress == null
? child
: Center(
child: CircularProgressIndicator(
strokeWidth: 2.5,
valueColor: const AlwaysStoppedAnimation<Color>(
Colors.white,
),
value: progress.cumulativeBytesLoaded /
progress.expectedTotalBytes!,
),
),
),
),
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';

/// Container to display the main product image on a product card.
class SmoothProductImageContainer extends StatelessWidget {
const SmoothProductImageContainer({
required this.height,
required this.width,
required this.child,
});

final double height;
final double width;
final Widget child;

@override
Widget build(BuildContext context) => ClipRRect(
borderRadius: ROUNDED_BORDER_RADIUS,
child: SizedBox(
width: width,
height: height,
child: child,
),
);
}