Skip to content

Commit

Permalink
chore: changed signatures of the builders to have the BuildContext as…
Browse files Browse the repository at this point in the history
… the first parameter, matching the Flutter style
  • Loading branch information
pavanpodila committed Apr 4, 2024
1 parent 5170f15 commit fc7f840
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 49 deletions.
13 changes: 11 additions & 2 deletions packages/sanity/flutter_sanity_portable_text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -479,5 +479,14 @@ class MyApp extends StatelessWidget {
## Exploring further

There are several other features which have been excluded from the examples
above. You can look at the properties of `PortableConfig` for more customization
opportunities, including changing the base styles.
above, such as:

- Custom block styles
- Custom Block containers
- Item padding inside a `PortableText` widget
- Indents for list items
- Changing the base style
- Changing the default block and mark styles

You can look at the properties of `PortableConfig` for more customization
opportunities.
19 changes: 19 additions & 0 deletions packages/sanity/flutter_sanity_portable_text/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,25 @@ void main() {
// Registering a custom mark
_registerCustomMark();

// Registering a custom block style
_registerCustomBlockStyle();

runApp(const MyApp());
}

void _registerCustomBlockStyle() {
PortableTextConfig.shared.styles['custom-style'] =
(final BuildContext context, final TextStyle base) {
final theme = Theme.of(context);

return base.copyWith(
color: theme.colorScheme.primary,
fontSize: 18,
fontWeight: FontWeight.bold,
);
};
}

void _registerCustomMark() {
PortableTextConfig.shared.markDefs['custom-mark'] = MarkDefDescriptor(
schemaType: 'custom-mark',
Expand Down Expand Up @@ -189,6 +205,9 @@ class MyApp extends StatelessWidget {
Span(text: '.'),
],
),
_textBlock('Custom Styles', style: 'custom-style'),
_textBlock('Report errors for missing styles...'),
_textBlock('Unregistered Styles', style: 'unregistered-style'),
_textBlock('And not to forget...the unsung'),
for (final index in [1, 2, 3, 4, 5, 6])
_textBlock('H$index', style: 'h$index'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class PortableTextBlock extends StatelessWidget {
);

final builder = config.blockContainers[model.style] ??
config.blockContainers['default']!;
final child = builder(content, context);
config.blockContainers['__default__']!;

final child = builder(context, content);

final leftPadding =
model.listItem == null ? 0.0 : config.listIndent * (model.level ?? 0);
Expand All @@ -55,15 +56,21 @@ class PortableTextBlock extends StatelessWidget {

final baseStyle = config.baseStyle(context) ?? theme.textTheme.bodyLarge!;

final styleBuilder = config.styles[model.style];
if (styleBuilder == null) {
return WidgetSpan(
child: ErrorView(message: 'Missing style for ${model.style}'));
}

var style =
config.styles[model.style]?.call(baseStyle, context) ?? baseStyle;
config.styles[model.style]?.call(context, baseStyle) ?? baseStyle;

final pendingMarkDefs = <MarkDef>[];
for (final mark in span.marks) {
/// Standard marks (aka annotations)
final builder = config.styles[mark];
if (builder != null) {
style = builder(style, context);
style = builder(context, style);
continue;
}

Expand Down Expand Up @@ -122,11 +129,9 @@ You can rely on TextStyles instead for custom styling.''';

if (errorText != null) {
return WidgetSpan(
child: GestureDetector(
child: ErrorView(
message: errorText,
asBlock: false,
),
child: ErrorView(
message: errorText,
asBlock: false,
),
);
}
Expand All @@ -138,26 +143,28 @@ You can rely on TextStyles instead for custom styling.''';
final textStyle = PortableTextConfig.shared.baseStyle(context);

switch (model.listItem) {
case ListItemType.bullet:
case ListItemType.number:
return TextSpan(
text: '${(model.listItemIndex ?? 0) + 1}. ',
style: textStyle,
);

case ListItemType.square:
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Icon(Icons.circle, size: 8),
child: Icon(Icons.check_box_outline_blank, size: 8),
),
style: textStyle,
);
case ListItemType.number:
return TextSpan(
text: '${(model.listItemIndex ?? 0) + 1}. ',
style: textStyle,
);

default:
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Icon(Icons.check_box_outline_blank, size: 8),
child: Icon(Icons.circle, size: 8),
),
style: textStyle,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import '../flutter_sanity_portable_text.dart';

/// A function that builds a text style for a Portable Text block or span.
typedef TextStyleBuilder = TextStyle Function(
TextStyle base,
BuildContext context,
TextStyle base,
);

/// A function that builds a widget for a Portable block container.
typedef BlockContainerBuilder = Widget Function(Widget, BuildContext);
typedef BlockContainerBuilder = Widget Function(BuildContext, Widget);

/// A function that builds a widget for a Portable block item.
typedef BlockWidgetBuilder = Widget Function(
Expand Down Expand Up @@ -86,68 +86,68 @@ class PortableTextConfig {
/// The default text styles used by the shared instance of [PortableTextConfig].
static final Map<String, TextStyleBuilder> defaultStyles = {
'h1': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
Theme.of(context).textTheme.headlineLarge!,
'h2': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
Theme.of(context).textTheme.headlineMedium!,
'h3': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
Theme.of(context).textTheme.headlineSmall!,
'h4': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
Theme.of(context).textTheme.titleLarge!,
'h5': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
Theme.of(context).textTheme.titleMedium!,
'h6': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
Theme.of(context).textTheme.titleSmall!,
'normal': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
Theme.of(context).textTheme.bodyMedium!,
'em': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
base.copyWith(fontStyle: FontStyle.italic),
'strong': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
base.copyWith(fontWeight: FontWeight.bold),
'blockquote': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
base.copyWith(color: Theme.of(context).colorScheme.primary),
'strike-through': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
base.copyWith(
Expand All @@ -157,8 +157,8 @@ class PortableTextConfig {
]),
),
'underline': (
final TextStyle base,
final BuildContext context, [
final BuildContext context,
final TextStyle base, [
final MarkDef? mark,
]) =>
base.copyWith(
Expand All @@ -171,8 +171,8 @@ class PortableTextConfig {

/// The default block containers used by the shared instance of [PortableTextConfig].
static final Map<String, BlockContainerBuilder> defaultBlockContainers = {
'default': (final Widget child, final BuildContext context) => child,
'blockquote': (final Widget child, final BuildContext context) {
'__default__': (final BuildContext context, final Widget child) => child,
'blockquote': (final BuildContext context, final Widget child) {
final theme = Theme.of(context);

return Container(
Expand Down
4 changes: 2 additions & 2 deletions packages/system/vyuh_feature_system/lib/feature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ final feature = FeatureDescriptor(
return style.copyWith(
color: theme.colorScheme.primary,
decorationColor: theme.colorScheme.inversePrimary,
decorationStyle: TextDecorationStyle.double,
decorationStyle: TextDecorationStyle.dashed,
decoration: TextDecoration.underline,
);
},
Expand All @@ -133,7 +133,7 @@ final feature = FeatureDescriptor(
textAlign: TextAlign.left,
),
Icon(
Icons.arrow_forward,
Icons.chevron_right,
size: 16,
color: Theme.of(context).colorScheme.primary,
),
Expand Down

0 comments on commit fc7f840

Please sign in to comment.