在flutter 中对 Expanded 使用 来平分布局占用比例(至关于Android里面的LinearLayout 控件中设置权重),可是在使用中遇到一些坑,而后就开始了踩坑之旅...........css
Widget _columnExpanded(){
return new Column(
children: <Widget>[
new Expanded(child: new Text("有Expanded一半")),
new Text("无Expanded一半"),
],
);
}
复制代码
Widget _columnMoreExpanded(){
return new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Expanded(
child: new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,"
"有Expanded一半,有Expanded一半有Expanded一半,有Expanded一半,有Expanded一半",
maxLines: 3,
)
),
new Text("无Expanded一半"),
],
);
}
复制代码
/*
* Expanded 嵌套Column控件 而后Column 控件里面放两个 Text 会出现
*
*/
Widget _columnExpandedMoreWidget(){
return new Column(
children: <Widget>[
new Expanded(
child: new Column(
children: <Widget>[
new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
],
)
),
new Text("无Expanded一半"),
],
);
}
复制代码
Widget _columnExpandedMoreRowWidget(){
return new Column(
children: <Widget>[
new Expanded(
child: new Row(
children: <Widget>[
new Expanded(child: new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 2,
overflow: TextOverflow.ellipsis,
)),
new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
],
)
),
new Text("无Expanded一半"),
],
);
}
复制代码
看到超出屏幕,因而将 Row 中的两个 Text 控件都加上 Expanded ,代码以下:es6
Widget _columnExpandedMoreRowWidget1(){
return new Column(
children: <Widget>[
new Expanded(
child: new Row(
children: <Widget>[
new Expanded(child: new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 2,
overflow: TextOverflow.ellipsis,
)),
new Expanded(child: new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 1,
overflow: TextOverflow.ellipsis,
)),
],
)
),
new Text("无Expanded一半"),
],
);
}
复制代码
超出屏幕的提示消除。如图 npm
Widget _columnExpandedMoreRowWidget1(){
return new Column(
children: <Widget>[
new Expanded(
child: new Row(
children: <Widget>[
new Expanded(child: new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 2,
overflow: TextOverflow.ellipsis,
)),
new Expanded(child: new Text("有Expanded一半",
maxLines: 1,
overflow: TextOverflow.ellipsis,
)),
],
)
),
new Text("无Expanded一半"),
],
);
}
复制代码
平分的确算是平分了,可是没有达到效果,又修改: 代码以下:vim
Widget _columnExpandedMoreRowWidget1(){
return new Column(
children: <Widget>[
new Expanded(
child: new Row(
children: <Widget>[
new Expanded(child: new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 2,
overflow: TextOverflow.ellipsis,
)),
new Text("有Expanded一半有Expanded一半有Expanded一半",
maxLines: 2,
overflow: TextOverflow.ellipsis,
),
],
)
),
new Text("无Expanded一半"),
],
);
}
复制代码
在 Row 中去掉一个 Expanded 设置,就变成了 api
在Row中 Column 的第四种 说明了 Row 一行的排列出现的问题,仍是 Text 不换行bash
Widget _rowExpandedMoreWidget(){
return new Row(
children: <Widget>[
new Expanded(
child: new Row(
children: <Widget>[
new Expanded(child: new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 2,
overflow: TextOverflow.clip,
),),
new Expanded(child: new Text("有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,有Expanded一半,",
maxLines: 2,
overflow: TextOverflow.clip,
),),
],
)
),
new Text("无Expanded一半"),
],
);
}
复制代码
经过以上列子 说明 Row 中 要横向的两个控件 要么平分 ,要么 Row 中的另一个控件宽度不能太长,否则超出边界不能显示,在Column中始终能自动换行app