Skip to content

Commit

Permalink
Merge pull request #13 from Floating-Dartists/refactor/accordion
Browse files Browse the repository at this point in the history
refactor: accordion
  • Loading branch information
TesteurManiak committed Dec 20, 2023
2 parents f987016 + 2d7c335 commit a93fb4d
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 97 deletions.
52 changes: 25 additions & 27 deletions lib/src/components/accordion/accordion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,71 @@ import 'package:flutter_dsfr/flutter_dsfr.dart';
import 'package:flutter_dsfr/src/components/accordion/accordion_border.dart';
import 'package:flutter_dsfr/src/components/accordion/accordion_panel.dart';

/// {@template accordion_data}
/// Create an Accordion .
/// Must be render inside a Scrollable widget
///
/// Specs: https://gouvfr.atlassian.net/wiki/spaces/DB/pages/312082509/Accord+on+-+Accordion
/// {@endtemplate}
class DSFRAccordion extends StatefulWidget {
/// {@macro accordion_data}
const DSFRAccordion({required this.panels, super.key});

final List<DSFRAccordionData> panels;

@override
State<StatefulWidget> createState() => _DSFRAccordionState();
State<DSFRAccordion> createState() => _DSFRAccordionState();
}

class _DSFRAccordionState extends State<DSFRAccordion> {
UniqueKey? _accordionValue;
late final _itemsValues =
UniqueKey? accordionValue;
late final itemsValues =
List<UniqueKey>.generate(widget.panels.length, (_) => UniqueKey());
late final _panels = widget.panels;
late final panels = widget.panels;
bool initialized = false;

@override
void initState() {
super.initState();
_setDefaultAccordionValue();
setDefaultAccordionValue();
}

// ! this should only be called once in initState
void _setDefaultAccordionValue() {
void setDefaultAccordionValue() {
if (initialized) return;
final lastInitialyExpandedIndex =
_panels.lastIndexWhere((panelData) => panelData.isInitialyExpanded);
panels.lastIndexWhere((panelData) => panelData.isInitialyExpanded);

if (lastInitialyExpandedIndex != -1 &&
_itemsValues.length == _panels.length) {
_accordionValue = _itemsValues[lastInitialyExpandedIndex];
itemsValues.length == panels.length) {
accordionValue = itemsValues[lastInitialyExpandedIndex];
}
initialized = true;
}

List<Widget> _renderPanels() {
final children = <Widget>[];
Iterable<Widget> renderPanels() sync* {
for (var i = 0; i < panels.length; i++) {
final isLastInGroup = i == panels.length - 1;
final panelData = panels[i];

for (var i = 0; i < _panels.length; i++) {
final isLastInGroup = i == _panels.length - 1;
final panelData = _panels[i];

final child = DSFRAccordionBorder(
yield DSFRAccordionBorder(
isLastInGroup: isLastInGroup,
child: DSFRAccordionPanel(
data: panelData,
accordionValue: _accordionValue,
itemValue: _itemsValues[i],
accordionValue: accordionValue,
itemValue: itemsValues[i],
onExpandedChange: (newValue) {
setState(() {
_accordionValue = newValue;
});
setState(() => accordionValue = newValue);
},
),
);

children.add(child);
}

return children;
}

@override
Widget build(BuildContext context) {
return Column(
children: _renderPanels(),
mainAxisSize: MainAxisSize.min,
children: [...renderPanels()],
);
}
}
58 changes: 14 additions & 44 deletions lib/src/components/accordion/accordion_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,17 @@ class DSFRAccordionPanel extends StatelessWidget {
});

final DSFRAccordionData data;
final void Function(UniqueKey?) onExpandedChange;
final ValueChanged<UniqueKey?> onExpandedChange;
final UniqueKey? accordionValue;
final UniqueKey itemValue;

bool get _isExpanded => itemValue == accordionValue;

Widget _getTitle(
BuildContext context, {
required DSFRTypography dsfrTypography,
required DSFRColors dsfrColors,
}) {
if (_isExpanded) {
return Text(
data.title,
style: dsfrTypography.boldText.copyWith(
color: dsfrColors.text,
),
);
}

return Text(
data.title,
style:
DefaultTextStyle.of(context).style.copyWith(color: dsfrColors.text),
);
}

Widget _getTrailingIcon({
required DSFRColors dsfrColors,
required DSFRSizes dsfrSizes,
}) {
final iconData = _isExpanded ? DSFRIcons.substract : DSFRIcons.add;

return Icon(
iconData,
color: dsfrColors.text,
size: dsfrSizes.w2,
);
}

@override
Widget build(BuildContext context) {
final dsfrTheme = DSFRThemeData.of(context);
final dsfrTypography = dsfrTheme.typography;
final dsfrColors = dsfrTheme.colors;
final dsfrSizes = dsfrTheme.sizes;
final isExpanded = itemValue == accordionValue;

return Theme(
data: Theme.of(context).copyWith(
Expand All @@ -75,14 +40,19 @@ class DSFRAccordionPanel extends StatelessWidget {
onExpandedChange(null);
}
},
isExpanded: _isExpanded,
title: _getTitle(
context,
dsfrTypography: dsfrTypography,
dsfrColors: dsfrColors,
isExpanded: isExpanded,
title: DefaultTextStyle(
style: (isExpanded
? dsfrTypography.boldText
: dsfrTypography.defaultText)
.copyWith(color: dsfrColors.text),
child: Text(data.title),
),
trailing: Icon(
isExpanded ? DSFRIcons.substract : DSFRIcons.add,
color: dsfrColors.text,
size: dsfrSizes.w2,
),
trailing:
_getTrailingIcon(dsfrColors: dsfrColors, dsfrSizes: dsfrSizes),
child: data.content,
),
);
Expand Down
27 changes: 10 additions & 17 deletions test/components/accordion/accordion_robot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,16 @@ class AccordionRobot {
}) async {
await tester.pumpWidget(
MaterialApp(
home: Material(
child: Theme(
data: ThemeData(
fontFamily: DSFRFonts.marianne,
brightness: Brightness.light,
extensions: const [
DSFRTypography.medium(),
DSFRColors.light(),
DSFRSizes.regular(),
],
),
child: SingleChildScrollView(
child: DSFRAccordion(
panels: panels,
),
),
),
theme: ThemeData(
useMaterial3: false,
fontFamily: DSFRFonts.marianne,
brightness: Brightness.light,
extensions: const [
DSFRThemeData(colors: DSFRColors.light()),
],
),
home: SingleChildScrollView(
child: DSFRAccordion(panels: panels),
),
),
);
Expand Down
14 changes: 5 additions & 9 deletions test/components/accordion/accordion_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,14 @@ void main() {
testWidgets(
'it have a radio like logic, only one content can be open at a time',
(tester) async {
final panels = [
const DSFRAccordionData(
const panels = [
DSFRAccordionData(
title: titleText,
content: Text(
contentText1,
),
content: Text(contentText1),
),
const DSFRAccordionData(
DSFRAccordionData(
title: titleText,
content: Text(
contentText2,
),
content: Text(contentText2),
),
];

Expand Down
Binary file modified test/components/accordion/goldens/ci/accordion_dark.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/components/accordion/goldens/ci/accordion_light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/components/buttons/goldens/ci/france_connect_button.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/components/buttons/goldens/ci/france_connect_button_dark.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a93fb4d

Please sign in to comment.