auto_layout.dart 2.52 KB
Newer Older
1 2 3 4 5
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/rendering.dart';
6
import 'package:meta/meta.dart';
7 8 9 10

import 'framework.dart';

export 'package:flutter/rendering.dart' show
11
    AutoLayoutRect,
12 13
    AutoLayoutDelegate;

14
/// A widget that uses the cassowary constraint solver to automatically size and position children.
15
class AutoLayout extends MultiChildRenderObjectWidget {
16
  /// Creates a widget that uses the cassowary constraint solver to automatically size and position children.
17 18 19 20 21 22
  AutoLayout({
    Key key,
    this.delegate,
    List<Widget> children: const <Widget>[]
  }) : super(key: key, children: children);

23 24 25
  /// The delegate that generates constraints for the layout.
  ///
  /// If the delgate is null, the layout is unconstrained.
26 27
  final AutoLayoutDelegate delegate;

28
  @override
29
  RenderAutoLayout createRenderObject(BuildContext context) => new RenderAutoLayout(delegate: delegate);
30

31
  @override
32
  void updateRenderObject(BuildContext context, RenderAutoLayout renderObject) {
33 34 35 36
    renderObject.delegate = delegate;
  }
}

37 38 39 40 41 42
/// A widget that provides constraints for a child of an [AutoLayout] widget.
///
/// An [AutoLayoutChild] widget must be a descendant of an [AutoLayout], and
/// the path from the [AutoLayoutChild] widget to its enclosing [AutoLayout]
/// must contain only [StatelessWidget]s or [StatefulWidget]s (not other kinds
/// of widgets, like [RenderObjectWidget]s).
43
class AutoLayoutChild extends ParentDataWidget<AutoLayout> {
44 45 46 47 48 49 50 51 52
  /// Creates a widget that provides constraints for a child of an [AutoLayout] widget.
  ///
  /// The object identity of the [rect] argument must be unique among children
  /// of a given [AutoLayout] widget.
  AutoLayoutChild({
    AutoLayoutRect rect,
    @required Widget child
  }) : rect = rect,
       super(key: rect != null ? new ObjectKey(rect) : null, child: child);
53

54 55 56 57 58 59
  /// The constraints to use for this child.
  ///
  /// The object identity of the [rect] object must be unique among children of
  /// a given [AutoLayout] widget.
  ///
  /// If null, the child's size and position are unconstrained.
60
  final AutoLayoutRect rect;
61

62
  @override
63 64 65 66 67
  void applyParentData(RenderObject renderObject) {
    assert(renderObject.parentData is AutoLayoutParentData);
    final AutoLayoutParentData parentData = renderObject.parentData;
    // AutoLayoutParentData filters out redundant writes and marks needs layout
    // as appropriate.
68
    parentData.rect = rect;
69 70
  }
}