Last active
September 13, 2023 19:30
-
-
Save slightfoot/81176152d5c27df42e9049103de48a9a to your computer and use it in GitHub Desktop.
Scale - by Simon Lightfoot - 13th September 2023 - #HumpdayQandA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2023 Simon Lightfoot | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
import 'dart:ui' as ui; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
/// Example of using [Scale] widget. | |
void main() { | |
runApp( | |
MaterialApp( | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData.dark(useMaterial3: true), | |
home: const Material( | |
child: Scale( | |
scale: 4.0, | |
child: Padding( | |
padding: EdgeInsets.all(24.0), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: [ | |
Text('Testing'), | |
SizedBox(height: 8.0), | |
AspectRatio( | |
aspectRatio: 4 / 1, | |
child: Placeholder(color: Colors.white), | |
), | |
], | |
), | |
), | |
), | |
), | |
), | |
); | |
} | |
class Scale extends SingleChildRenderObjectWidget { | |
const Scale({ | |
super.key, | |
required this.scale, | |
required super.child, | |
}); | |
final double scale; | |
@override | |
RenderObject createRenderObject(BuildContext context) { | |
return RenderScale(scale: scale); | |
} | |
@override | |
void updateRenderObject( | |
BuildContext context, | |
covariant RenderScale renderObject, | |
) { | |
renderObject.scale = scale; | |
} | |
} | |
class RenderScale extends RenderProxyBox { | |
RenderScale({ | |
required double scale, | |
}) : _scale = scale; | |
double _scale; | |
double get scale => _scale; | |
set scale(double value) { | |
if (_scale == value) { | |
return; | |
} | |
_scale = value; | |
markNeedsLayout(); | |
} | |
double get inverseScale => 1.0 / _scale; | |
@override | |
void performLayout() { | |
final scaledConstraints = constraints * inverseScale; | |
child!.layout(scaledConstraints, parentUsesSize: true); | |
size = constraints.constrain(child!.size * _scale); | |
} | |
@override | |
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) { | |
return child?.hitTest(result, position: position * inverseScale) ?? false; | |
} | |
@override | |
void paint(PaintingContext context, Offset offset) { | |
final transform = Matrix4.diagonal3Values(_scale, _scale, 1.0); | |
if (layer is TransformLayer) { | |
final transformLayer = layer! as TransformLayer; | |
transformLayer.transform = transform; | |
} else { | |
layer = TransformLayer(transform: transform); | |
} | |
context.pushLayer(layer!, super.paint, offset); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment