Skip to content

Add copyWith feature to PaintΒ #142871

@bramp

Description

@bramp

Use case

When painting to a canvas I find myself reusing very similar Paint object with just one field tweaked. For example, multiple Paints with the same stroke/strokeWidth, but with a different colour. Making a decision to change the strokeWidth could be error prone. For example:

  final item1Stroke = Paint()
    ..color = Colors.red
    ..style = PaintingStyle.stroke
    ..isAntiAlias = false
    ..strokeWidth = 10;

  final item2Stroke = Paint()
    ..color = Colors.green
    ..style = PaintingStyle.stroke
    ..isAntiAlias = false
    ..strokeWidth = 10;

  final item3Stroke = Paint()
    ..color = Colors.blue
    ..style = PaintingStyle.stroke
    ..isAntiAlias = false
    ..strokeWidth = 10;

Proposal

Instead add a copyWith method, for example:

  final defaultStroke = Paint()
    ..style = PaintingStyle.stroke
    ..isAntiAlias = false
    ..strokeWidth = 10;

  final item1Fill = defaultStroke.copyWith(
    color: Colors.red
  );
  final item2Fill = defaultStroke.copyWith(
    color: Colors.blue
  );
  final item3Fill = defaultStroke.copyWith(
    color: Colors.green
  );

This proposal was filed previously: #40497 but that issue was closed without explaination, and at that time had 21 thumbs up (if that's any kind of signal).

Metadata

Metadata

Assignees

Labels

P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterengineflutter/engine related. See also e: labels.r: fixedIssue is closed as already fixed in a newer versionteam-engineOwned by Engine teamtriaged-engineTriaged by Engine team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions