Skip to content

Commit

Permalink
feat: Support for a "source" within a Knownledge Panel Text element (#…
Browse files Browse the repository at this point in the history
…1458)

* Support for source field within a Knowledge Panel text element

* Move to a basic Button instead
  • Loading branch information
g123k authored Apr 2, 2022
1 parent 721a49b commit 53fb69c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/KnowledgePanelElement.dart';
import 'package:openfoodfacts/model/KnowledgePanels.dart';
import 'package:smooth_app/cards/product_cards/knowledge_panels/knowledge_panel_card.dart';
Expand All @@ -7,6 +8,8 @@ import 'package:smooth_app/cards/product_cards/knowledge_panels/knowledge_panel_
import 'package:smooth_app/cards/product_cards/knowledge_panels/knowledge_panel_world_map_card.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/smooth_html_widget.dart';
import 'package:smooth_app/helpers/launch_url_helper.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';

class KnowledgePanelElementCard extends StatelessWidget {
const KnowledgePanelElementCard({
Expand All @@ -21,8 +24,8 @@ class KnowledgePanelElementCard extends StatelessWidget {
Widget build(BuildContext context) {
switch (knowledgePanelElement.elementType) {
case KnowledgePanelElementType.TEXT:
return SmoothHtmlWidget(
knowledgePanelElement.textElement!.html,
return _KnowledgePanelTextElementCard(
textElement: knowledgePanelElement.textElement!,
);
case KnowledgePanelElementType.IMAGE:
return Image.network(
Expand Down Expand Up @@ -51,3 +54,60 @@ class KnowledgePanelElementCard extends StatelessWidget {
}
}
}

/// A Knowledge Panel Text element may contain a source.
/// This widget add this information if needed and allows to open the url
/// (if provided)
class _KnowledgePanelTextElementCard extends StatelessWidget {
const _KnowledgePanelTextElementCard({
required this.textElement,
Key? key,
}) : super(key: key);

final KnowledgePanelTextElement textElement;

@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;

Widget text = SmoothHtmlWidget(
textElement.html,
);

if (hasSource) {
text = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
text,
const SizedBox(
height: MEDIUM_SPACE,
),
// Remove Icon
IconTheme.merge(
data: const IconThemeData(
size: 0.0,
),
child: addPanelButton(
appLocalizations
.knowledge_panel_text_source(textElement.sourceText!),
iconData: null,
onPressed: () {
LaunchUrlHelper.launchURL(
textElement.sourceUrl!,
false,
);
},
),
),
],
);
}

return text;
}

bool get hasSource =>
textElement.sourceText?.isNotEmpty == true &&
textElement.sourceUrl?.isNotEmpty == true;
}
9 changes: 9 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -622,5 +622,14 @@
"refuse_button_label": "Refuse",
"@refuse": {
"description": "Button to decline the request of sending the anonymous analytics"
},
"knowledge_panel_text_source": "Go further on {source_name}",
"@knowledge_panel_text_source": {
"description": "Source field within a text knowledge panel.",
"placeholders": {
"source_name": {
"type": "String"
}
}
}
}
9 changes: 9 additions & 0 deletions packages/smooth_app/lib/l10n/app_fr.arb
Original file line number Diff line number Diff line change
Expand Up @@ -618,5 +618,14 @@
"refuse_button_label": "Refuse",
"@refuse": {
"description": "Button to decline the request of sending the anonymous analytics"
},
"knowledge_panel_text_source": "En savoir plus sur {source_,ame}",
"@knowledge_panel_text_source": {
"description": "Source field within a text knowledge panel.",
"placeholders": {
"source_name": {
"type": "String"
}
}
}
}

0 comments on commit 53fb69c

Please sign in to comment.