Skip to content

Describe how alignment work on DecorationImage #11589

@chrisfanght

Description

@chrisfanght

Issue

I'm using DecorationImage with FractionalOffset to crop a piece of image from my PNG file but the result seems to be shifted from the original pixels.

The original image is 100*800 resolution with each color block of size 100 * 50. The code is trying to crop each color block from the picture asset but it had some alignment problem.

The devicePixelRatio on my test emulator is 1.5 and the offset seems to be right after
offset = offset * 1.006
which doesn't seem to be directly related to the devicePixelRatio

Code

class PNGReaderState extends State<PNGReader>{
  double _blockWidth = 100.0;
  double _blockHeight = 50.0;
  double _stripWidth = 100.0;
  double _stripHeight = 800.0;
  final _blockOffsets = [
    0.0,
    50.0,
    100.0,
    150.0,
    200.0,
    250.0,
    300.0,
    350.0,
    400.0,
    450.0,
    500.0,
    550.0,
    600.0,
    650.0,
    700.0,
    750.0
  ];

 Widget _colorBlock(double offset) {
    return new Column(
      children: <Widget>[
        new Divider(
          height: 8.0,
        ),
        new Container(
          width: _blockWidth,
          height: _blockHeight,
          decoration: new BoxDecoration(
            image: new DecorationImage(
              fit: BoxFit.none,
              alignment: new FractionalOffset.fromOffsetAndSize(
                new Offset(
                  0.0,
                  offset,
                ),
                new Size(
                  _stripWidth,
                  _stripHeight,
                ),
              ),
              image: new Image.asset('images/test.png').image,
            ),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Test'),
      ),
      body: new ListView(
        children: _blockOffsets
            .map((offset) => _colorBlock(offset))
            .toList(),
      ),
    );
  }
}

Images

Result after cropping image
Image of emulator
Original Image
Image of color strip

Metadata

Metadata

Assignees

Labels

d: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.waiting for PR to land (fixed)A fix is in flight

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions