performLayout
- Flutter course
- Expert
- Lesson 13/39
- 4 min
Position children with performLayout.
performLayout
performLayout is the method that computes the size and position of the render object's children. It uses the incoming constraints from the parent to perform layout, and distributes them to children via child.layout(constraints).
@override
void performLayout() {
size = constraints.biggest;
if (child != null) {
child!.layout(constraints);
}
}parentUsesSize
When child.layout(constraints, parentUsesSize: true) is called, the parent can read the child's size. If parentUsesSize: false, the parent does not depend on the child's size, which provides an extra optimization.
Practice Define a RenderBox with a child
Define MyContainer as a RenderBox with the RenderObjectWithChildMixin<RenderBox> mixin. In performLayout, set size = constraints.biggest and call child?.layout(constraints, parentUsesSize: true). In main, create a MyContainer instance and print its runtimeType.
Starter code
import 'package:flutter/rendering.dart';
// MyContainer sınıfını RenderBox with RenderObjectWithChildMixin<RenderBox> olarak tanımla
// performLayout'ta size'ı constraints biggest ata ve child varsa layout et
void main() {
// MyContainer örneği al ve runtimeType yazdır
}Sample solution
import 'package:flutter/rendering.dart';
class MyContainer extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
@override
void performLayout() {
size = constraints.biggest;
child?.layout(constraints, parentUsesSize: true);
}
}
void main() {
final container = MyContainer();
print(container.runtimeType);
}Expected output
MyContainer
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does child.layout do?
- Draws the child
- Deletes the child
- Sends constraints to the child and makes it lay out Correct answer
- Sets the child color
Explanation: child.layout(constraints) triggers the child's performLayout, passing the parent's bounds to the child; it does not deal with drawing or color.
2 child.layout call
child!.layout(____, parentUsesSize: true);
child!.layout(constraints, parentUsesSize: true);
Explanation: child.layout expects a BoxConstraints; size is a Size, paint is a Paint, and bounds is not a valid parameter name.
3 performLayout matches
| Term | Meaning |
|---|---|
performLayout |
Computes size and position |
layout |
Distributes constraints to child |
child |
Sub render object |
parentUsesSize |
Parent reads child size |