Skip to content

No logic in createRenderObject() #155853

@nate-thegrate

Description

@nate-thegrate

Considering the test class from #155699:

class ThemedCard extends SingleChildRenderObjectWidget {
  const ThemedCard({super.key}) : super(child: const SizedBox.expand());

  @override
  RenderPhysicalShape createRenderObject(BuildContext context) {
    final CardThemeData cardTheme = CardTheme.of(context).data;

    return RenderPhysicalShape(
      clipper: ShapeBorderClipper(shape: cardTheme.shape ?? const RoundedRectangleBorder()),
      clipBehavior: cardTheme.clipBehavior ?? Clip.antiAlias,
      color: cardTheme.color ?? Colors.white,
      elevation: cardTheme.elevation ?? 0.0,
      shadowColor: cardTheme.shadowColor ?? Colors.black,
    );
  }

  @override
  void updateRenderObject(BuildContext context, RenderPhysicalShape renderObject) {
    final CardThemeData cardTheme = CardTheme.of(context).data;

    renderObject
      ..clipper = ShapeBorderClipper(shape: cardTheme.shape ?? const RoundedRectangleBorder())
      ..clipBehavior = cardTheme.clipBehavior ?? Clip.antiAlias
      ..color = cardTheme.color ?? Colors.white
      ..elevation = cardTheme.elevation ?? 0.0
      ..shadowColor = cardTheme.shadowColor ?? Colors.black;
  }
}

It's pretty great having the ability to hook up a RenderObjectWidget straight to an InheritedWidget, but having a single source of truth would help to mitigate future bugs.


Proposal: make RenderObject constructor arguments optional

class ThemedCard extends PhysicalShape {
  const MyWidget({super.key});

  @override
  void updateRenderObject(BuildContext context, RenderPhysicalShape renderObject) {
    // single source of truth!
  }
}

I should probably reiterate that this isn't strictly necessary for us to do, but it'd still be wonderful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectframeworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy