Skip to content

Arrow function is formatted with first line on next line if whole body does not fit on one line #1649

Closed as duplicate of#1545
@navaronbracke

Description

@navaronbracke

While the new formatting style is a step in the right direction, I have seen it do something odd for arrow functions.

Considering the following settings:

environment:
  sdk: ">=3.7.0 <4.0.0"

formatter:
  page_width: 120

The following code is formatted with the first line of the arrow function on the next line, instead of the line it was at with the old style, even though that first line would definitely fit on one line.
This is a bit of a disruptive change, as it introduces an odd indentation for code that is otherwise fine to read.
I am aware that this is opinionated, but I think that fixing this is probably good for readability.
I have also seen frustration from other devs about this change, so maybe we can correct it?

The "workaround" is just to use a block body in this case, but some people probably prefer arrow functions over block bodies.

Actual

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: controller,
      builder: (context, child) {
        if (validator(context, controller.value.text) != null) {
          return const SizedBox.shrink();
        }

        return PlatformAwareWidget(
          android:
              (context) => IconButton(
                color: Theme.of(context).colorScheme.primary,
                icon: const Icon(Icons.check),
                onPressed: onTap,
              ),
          ios: (context) => CupertinoIconButton(icon: CupertinoIcons.checkmark_alt, onPressed: onTap),
        );
      },
    );
  }

Expected

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: controller,
      builder: (context, child) {
        if (validator(context, controller.value.text) != null) {
          return const SizedBox.shrink();
        }

        return PlatformAwareWidget(
          android: (context) => IconButton(
            color: Theme.of(context).colorScheme.primary,
            icon: const Icon(Icons.check),
            onPressed: onTap,
          ),
          ios: (context) => CupertinoIconButton(icon: CupertinoIcons.checkmark_alt, onPressed: onTap),
        );
      },
    );
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions