Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -101,62 +101,55 @@ final checkboxFilterChipsInput = CatalogItem(
]
''',
],
widgetBuilder:
({
required data,
required id,
required buildChild,
required dispatchEvent,
required context,
required dataContext,
required getComponent,
}) {
final checkboxFilterChipsData = _CheckboxFilterChipsInputData.fromMap(
data as Map<String, Object?>,
widgetBuilder: (context) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Because context usually refers to a BuildContext, can we change the name of this to be "catalogItemContext" or something else that is specific so people reading this can tell it's not a BuildContext?

I think this will also help later where you have to come up with other names for the build context: you can go back to calling it context, and the catalog item one can be something else. Maybe itemContext?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

itemContext sounds good!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I renamed it in main signature

will rename in other places in next PR

final checkboxFilterChipsData = _CheckboxFilterChipsInputData.fromMap(
context.data as Map<String, Object?>,
);
IconData? icon;
if (checkboxFilterChipsData.iconName != null) {
try {
icon = iconFor(
TravelIcon.values.byName(checkboxFilterChipsData.iconName!),
);
IconData? icon;
if (checkboxFilterChipsData.iconName != null) {
try {
icon = iconFor(
TravelIcon.values.byName(checkboxFilterChipsData.iconName!),
);
} catch (e) {
developer.log(
'Invalid icon name: ${checkboxFilterChipsData.iconName}',
name: 'CheckboxFilterChipsInput',
error: e,
);
icon = null;
}
}
} catch (e) {
developer.log(
'Invalid icon name: ${checkboxFilterChipsData.iconName}',
name: 'CheckboxFilterChipsInput',
error: e,
);
icon = null;
}
}

final selectedOptionsRef = checkboxFilterChipsData.selectedOptions;
final notifier = dataContext.subscribeToObjectArray(selectedOptionsRef);
final selectedOptionsRef = checkboxFilterChipsData.selectedOptions;
final notifier = context.dataContext.subscribeToObjectArray(
selectedOptionsRef,
);

return ValueListenableBuilder<List<Object?>?>(
valueListenable: notifier,
builder: (context, currentSelectedValues, child) {
final selectedOptionsSet = (currentSelectedValues ?? [])
.cast<String>()
.toSet();
return _CheckboxFilterChip(
chipLabel: checkboxFilterChipsData.chipLabel,
options: checkboxFilterChipsData.options,
icon: icon,
selectedOptions: selectedOptionsSet,
onChanged: (newSelectedOptions) {
final path = selectedOptionsRef['path'] as String?;
if (path != null) {
dataContext.update(
DataPath(path),
newSelectedOptions.toList(),
);
}
},
);
return ValueListenableBuilder<List<Object?>?>(
valueListenable: notifier,
builder: (buildContext, currentSelectedValues, child) {
final selectedOptionsSet = (currentSelectedValues ?? [])
.cast<String>()
.toSet();
return _CheckboxFilterChip(
chipLabel: checkboxFilterChipsData.chipLabel,
options: checkboxFilterChipsData.options,
icon: icon,
selectedOptions: selectedOptionsSet,
onChanged: (newSelectedOptions) {
final path = selectedOptionsRef['path'] as String?;
if (path != null) {
context.dataContext.update(
DataPath(path),
newSelectedOptions.toList(),
);
}
},
);
},
);
},
);

class _CheckboxFilterChip extends StatelessWidget {
Expand Down
43 changes: 18 additions & 25 deletions examples/travel_app/lib/src/catalog/date_input_chip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,33 +128,26 @@ final dateInputChip = CatalogItem(
]
''',
],
widgetBuilder:
({
required data,
required id,
required buildChild,
required dispatchEvent,
required context,
required dataContext,
required getComponent,
}) {
final datePickerData = _DatePickerData.fromMap(data as JsonMap);
final notifier = dataContext.subscribeToString(datePickerData.value);
final path = datePickerData.value?['path'] as String?;
widgetBuilder: (context) {
final datePickerData = _DatePickerData.fromMap(context.data as JsonMap);
final notifier = context.dataContext.subscribeToString(
datePickerData.value,
);
final path = datePickerData.value?['path'] as String?;

return ValueListenableBuilder<String?>(
valueListenable: notifier,
builder: (context, currentValue, child) {
return _DateInputChip(
initialValue: currentValue,
label: datePickerData.label,
onChanged: (newValue) {
if (path != null) {
dataContext.update(DataPath(path), newValue);
}
},
);
return ValueListenableBuilder<String?>(
valueListenable: notifier,
builder: (buildContext, currentValue, child) {
return _DateInputChip(
initialValue: currentValue,
label: datePickerData.label,
onChanged: (newValue) {
if (path != null) {
context.dataContext.update(DataPath(path), newValue);
}
},
);
},
);
},
);
47 changes: 19 additions & 28 deletions examples/travel_app/lib/src/catalog/information_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,27 @@ final informationCard = CatalogItem(
]
''',
],
widgetBuilder:
({
required data,
required id,
required buildChild,
required dispatchEvent,
required context,
required dataContext,
required getComponent,
}) {
final cardData = _InformationCardData.fromMap(
data as Map<String, Object?>,
);
final imageChild = cardData.imageChildId != null
? buildChild(cardData.imageChildId!)
: null;
widgetBuilder: (context) {
final cardData = _InformationCardData.fromMap(
context.data as Map<String, Object?>,
);
final imageChild = cardData.imageChildId != null
? context.buildChild(cardData.imageChildId!)
: null;

final titleNotifier = dataContext.subscribeToString(cardData.title);
final subtitleNotifier = dataContext.subscribeToString(
cardData.subtitle,
);
final bodyNotifier = dataContext.subscribeToString(cardData.body);
final titleNotifier = context.dataContext.subscribeToString(cardData.title);
final subtitleNotifier = context.dataContext.subscribeToString(
cardData.subtitle,
);
final bodyNotifier = context.dataContext.subscribeToString(cardData.body);

return _InformationCard(
imageChild: imageChild,
titleNotifier: titleNotifier,
subtitleNotifier: subtitleNotifier,
bodyNotifier: bodyNotifier,
);
},
return _InformationCard(
imageChild: imageChild,
titleNotifier: titleNotifier,
subtitleNotifier: subtitleNotifier,
bodyNotifier: bodyNotifier,
);
},
);

class _InformationCard extends StatelessWidget {
Expand Down
113 changes: 53 additions & 60 deletions examples/travel_app/lib/src/catalog/input_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,71 +118,64 @@ final inputGroup = CatalogItem(
],
name: 'InputGroup',
dataSchema: _schema,
widgetBuilder:
({
required data,
required id,
required buildChild,
required dispatchEvent,
required context,
required dataContext,
required getComponent,
}) {
final inputGroupData = _InputGroupData.fromMap(
data as Map<String, Object?>,
);
widgetBuilder: (context) {
final inputGroupData = _InputGroupData.fromMap(
context.data as Map<String, Object?>,
);

final notifier = dataContext.subscribeToString(
inputGroupData.submitLabel,
);
final notifier = context.dataContext.subscribeToString(
inputGroupData.submitLabel,
);

final children = inputGroupData.children;
final actionData = inputGroupData.action;
final name = actionData['name'] as String;
final contextDefinition =
(actionData['context'] as List<Object?>?) ?? <Object?>[];
final children = inputGroupData.children;
final actionData = inputGroupData.action;
final name = actionData['name'] as String;
final contextDefinition =
(actionData['context'] as List<Object?>?) ?? <Object?>[];

return Card(
color: Theme.of(context).colorScheme.primaryContainer,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Wrap(
runSpacing: 16.0,
spacing: 8.0,
children: children.map(buildChild).toList(),
),
const SizedBox(height: 16.0),
ValueListenableBuilder<String?>(
valueListenable: notifier,
builder: (context, submitLabel, child) {
return ElevatedButton(
onPressed: () {
final resolvedContext = resolveContext(
dataContext,
contextDefinition,
);
dispatchEvent(
UserActionEvent(
name: name,
sourceComponentId: id,
context: resolvedContext,
),
);
},
child: Text(submitLabel ?? ''),
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Colors.white,
return Card(
color: Theme.of(context.buildContext).colorScheme.primaryContainer,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Wrap(
runSpacing: 16.0,
spacing: 8.0,
children: children.map(context.buildChild).toList(),
),
const SizedBox(height: 16.0),
ValueListenableBuilder<String?>(
valueListenable: notifier,
builder: (builderContext, submitLabel, child) {
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The child parameter in the builder function is unused. According to Dart conventions, it's best to replace unused parameters with an underscore (_) to improve code clarity.

Suggested change
builder: (builderContext, submitLabel, child) {
builder: (builderContext, submitLabel, _) {

return ElevatedButton(
onPressed: () {
final resolvedContext = resolveContext(
context.dataContext,
contextDefinition,
);
context.dispatchEvent(
UserActionEvent(
name: name,
sourceComponentId: context.id,
context: resolvedContext,
),
);
},
),
],
child: Text(submitLabel ?? ''),
style: ElevatedButton.styleFrom(
backgroundColor: Theme.of(
builderContext,
).colorScheme.primary,
foregroundColor: Colors.white,
),
);
},
),
),
);
},
],
),
),
);
},
);
Loading
Loading