Flutter 的 Center widget 可用來做置中的呈現。

其建構子如下:

Center({Key key, double widthFactor, double heightFactor, Widget child })

屬性如下:

NameTypeDescription
alignmentAlignmentGeometryHow to align the child.
childWidgetThe widget below this widget in the tree.
hashCodeintThe hash code for this object.
heightFactordoubleIf non-null, sets its height to the child’s height multiplied by this factor.
keyKeyControls how one widget replaces another widget in the tree.
runtimeTypeTypeA representation of the runtime type of the object.
widthFactordoubeIf non-null, sets its width to the child’s width multiplied by this factor.

方法如下:

NameReturn TypeDescription
createElement()SingleChildRenderObjectElementRenderObjectWidgets always inflate to a RenderObjectElement subclass.
createRenderObject(BuildContext context)RenderPositionedBoxCreates an instance of the RenderObject class that this RenderObjectWidget represents, using the configuration described by this RenderObjectWidget.
debugDescribeChildren()ListReturns a list of DiagnosticsNode objects describing this node’s children.
debugFillProperties(DiagnosticPropertiesBuilder description)void
didUnmountRenderObject(RenderObject renderObject)voidA render object previously associated with this widget has been removed from the tree. The given RenderObject will be of the same type as returned by this object’s createRenderObject.
noSuchMethod(Invocation invocation)dynamicInvoked when a non-existent method or property is accessed.
toDiagnosticsNode({String name, DiagnosticsTreeStyle style })DiagnosticsNodeReturns a debug representation of the object that is used by debugging tools and by toStringDeep.
toString({DiagnosticLevel minLevel: DiagnosticLevel.debug })StringReturns a string representation of this object.
toStringDeep({String prefixLineOne: ‘’, String prefixOtherLines, DiagnosticLevel minLevel: DiagnosticLevel.debug })StringReturns a string representation of this node and its descendants.
toStringShallow({String joiner: ‘, ‘, DiagnosticLevel minLevel: DiagnosticLevel.debug })StringReturns a one-line detailed description of the object.
toStringShort()StringA short, textual description of this widget.
updateRenderObject(BuildContext context, RenderPositionedBox renderObject)voidCopies the configuration described by this RenderObjectWidget to the given RenderObject, which will be of the same type as returned by this object’s createRenderObject.

使用上只要將元件放置於 Center widget 的 child 屬性。

import 'package:flutter/material.dart';

void main() {
runApp(
new Center(
child: new Container(
color: Colors.blue,
width: 48.0,
height: 48.0,
),
),
);
}

1.png

該元件即會被置中處理。

2.png