Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active February 16, 2023 16:56
Show Gist options
  • Save slightfoot/3b8851b4c5c60a4e9f6b4ef02fdbf486 to your computer and use it in GitHub Desktop.
Save slightfoot/3b8851b4c5c60a4e9f6b4ef02fdbf486 to your computer and use it in GitHub Desktop.
Fractional Flex Widget - by Simon Lightfoot - 08/05/2020
// MIT License
//
// Copyright (c) 2020 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 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class FractionalFlex extends SingleChildRenderObjectWidget {
const FractionalFlex({
Key key,
this.minFraction = 0.5,
this.maxFraction = 0.5,
@required Widget child,
}) : assert(minFraction != null),
assert(maxFraction != null),
assert(minFraction <= maxFraction),
super(key: key, child: child);
final double minFraction;
final double maxFraction;
@override
RenderObject createRenderObject(BuildContext context) {
return RenderFractionalFlex(this);
}
@override
void updateRenderObject(BuildContext context, RenderFractionalFlex renderObject) {
renderObject.widget = this;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DoubleProperty('minFraction', minFraction));
properties.add(DoubleProperty('maxFraction', maxFraction));
}
}
class RenderFractionalFlex extends RenderProxyBox {
RenderFractionalFlex(
this._widget, {
RenderBox child,
}) : super(child);
FractionalFlex _widget;
FractionalFlex get widget => _widget;
set widget(FractionalFlex value) {
if (_widget == value) return;
_widget = value;
markNeedsLayout();
}
@override
void performLayout() {
if (child != null) {
final parent = this.parent as RenderBox;
assert(parent is RenderFlex);
assert(parent.constraints.hasBoundedWidth);
final parentWidth = parent.constraints.maxWidth;
final childWidth = child.getMaxIntrinsicWidth(constraints.maxHeight);
child.layout(
constraints.tighten(
width: childWidth.clamp(
parentWidth * _widget.minFraction,
parentWidth * _widget.maxFraction,
),
),
parentUsesSize: true,
);
size = child.size;
} else {
performResize();
}
}
}
@alardizabal
Copy link

You could cascade L54-55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment