容器
flutter 中的 Container Widget
类似于 HTML
中的 DIV
。
var container = new Container( // grey box
child: new Text(
"Lorem ipsum",
style: new TextStyle(
fontSize: 24.0
fontWeight: FontWeight.w900,
fontFamily: "Georgia",
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
样式
设置样式使用 TextStyle Widget
中,有一个style的属性,在这里可以设置样式。
TextStyle bold24Roboto = new TextStyle(
color: Colors.white,
fontSize: 24.0,
fontWeight: FontWeight.w900,
);
居中组件
在 flutter 中组件要居中使用 Center widget
var container = new Container( // grey box
child: new Center(
child: new Text(
"Lorem ipsum",
style: bold24Roboto,
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
设置容器宽度
要指定Container的宽度,请设置其width属性。 这是一个固定的宽度,不像CSS中max-width属性,它可以设置容器宽度最大值。要在Flutter中模拟这种效果,请使用容器的constraints属性。 创建一个新的BoxConstraints来设置minWidth或maxWidth。
对于嵌套容器,如果父级的宽度小于子级宽度,则子级容器将自行调整大小以匹配父级。
var container = new Container( // grey box
child: new Center(
child: new Container( // red box
child: new Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: new BoxDecoration(
color: Colors.red[400],
),
padding: new EdgeInsets.all(16.0),
width: 240.0, //max-width is 240.0
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
控制位置和大小
更深度可以控制 widget
位置、大小和背景上执行更复杂的操作。
设置绝对位置
默认情况下,widget
相对于其父 widget
定位。
要将 widget
的绝对位置指定为x-y坐标,请将其嵌套在 Positioned Widget
中, 该 widget 又嵌套在 Stack widget
中。
var container = new Container( // grey box
child: new Stack(
children: [
new Positioned( // red box
child: new Container(
child: new Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: new BoxDecoration(
color: Colors.red[400],
),
padding: new EdgeInsets.all(16.0),
),
left: 24.0,
top: 24.0,
),
],
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
处理形状
圆角
要给矩形添加圆角请使用BoxDecoration对象的borderRadius属性 。 创建一个新的BorderRadius对象,给该对象指定一个的半径(会四舍五入)。
var container = new Container( // grey box
child: new Center(
child: new Container( // red circle
child: new Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: new BoxDecoration(
color: Colors.red[400],
borderRadius: new BorderRadius.all(
const Radius.circular(8.0),
),
),
padding: new EdgeInsets.all(16.0),
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
添加阴影
在CSS中,您可以使用box-shadow属性来快速指定阴影偏移和模糊。
在Flutter中,每个属性和值都单独指定。使用BoxDecoration的boxShadow属性创建BoxShadow列表。 您可以定义一个或多个BoxShadow,它们可以叠加出自定义阴影深度、颜色等。
var container = new Container( // grey box
child: new Center(
child: new Container( // red box
child: new Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: new BoxDecoration(
color: Colors.red[400],
boxShadow: <BoxShadow>[
new BoxShadow (
color: const Color(0xcc000000),
offset: new Offset(0.0, 2.0),
blurRadius: 4.0,
),
new BoxShadow (
color: const Color(0x80000000),
offset: new Offset(0.0, 6.0),
blurRadius: 20.0,
),
],
),
padding: new EdgeInsets.all(16.0),
),
),
width: 320.0,
height: 240.0,
decoration: new BoxDecoration(
color: Colors.grey[300],
),
margin: new EdgeInsets.only(bottom: 16.0),
);
圆和椭圆
在CSS中制作一个圆需要将矩形的四条边的border-radius设置为50%。
虽然BoxDecoration的borderRadius属性支持此方法, 但Flutter为此提供了一个shape属性, 值为BoxShape枚举 。
var container = new Container( // grey box
child: new Center(
child: new Container( // red circle
child: new Text(
"Lorem ipsum",
style: bold24Roboto,
textAlign: TextAlign.center,
),
decoration: new BoxDecoration(
color: Colors.red[400],
shape: BoxShape.circle,
),
padding: new EdgeInsets.all(16.0),
width: 160.0,
height: 160.0,
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
操作文本
调整文本间距
在CSS中,通过分别给出letter-spacing和word-spacing属性的长度值,指定每个字母或单词之间的空白间距。长度单位可以是px,pt,cm,em等。
在Flutter中,您将空白区域指定为Text的TextStyle的letterSpacing和wordSpacing属性, 值为逻辑像素(允许为负值)
var container = new Container( // grey box
child: new Center(
child: new Container( // red box
child: new Text(
"Lorem ipsum",
style: new TextStyle(
color: Colors.white,
fontSize: 24.0,
fontWeight: FontWeight.w900,
letterSpacing: 4.0,
),
),
decoration: new BoxDecoration(
color: Colors.red[400],
),
padding: new EdgeInsets.all(16.0),
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
转换文本
在HTML / CSS中,您可以使用text-transform属性执行简单大小写转换
在Flutter中,使用 dart:core库中的String 类的方法来转换Text的内容。
var container = new Container( // grey box
child: new Center(
child: new Container( // red box
child: new Text(
"Lorem ipsum".toUpperCase(),
style: bold24Roboto,
),
decoration: new BoxDecoration(
color: Colors.red[400],
),
padding: new EdgeInsets.all(16.0),
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
进行内联格式更改
Text widget控件,可以用相同的格式显示文本。 要显示使用多个样式的文本(在本例中为带有重点的单个单词),请改用RichText。 它的text属性可以指定一个或多个可单独设置样式的 TextSpanwidget
。
在以下示例中,“Lorem” 位于具有默认(继承)文本样式的 TextSpan
小部件中,“ipsum” 位于具有自定义样式的单独TextSpan中。
var container = new Container( // grey box
child: new Center(
child: new Container( // red box
child: new RichText(
text: new TextSpan(
style: bold24Roboto,
children: <TextSpan>[
new TextSpan(text: "Lorem "),
new TextSpan(
text: "ipsum",
style: new TextStyle(
fontWeight: FontWeight.w300,
fontStyle: FontStyle.italic,
fontSize: 48.0,
),
),
],
),
),
decoration: new BoxDecoration(
backgroundColor: Colors.red[400],
),
padding: new EdgeInsets.all(16.0),
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);
创建文本摘要
摘录显示段落中文本的最初行,并且通常使用省略号处理溢出文本。在HTML / CSS中,摘要不能超过一行。截断多行需要一些JavaScript代码。
在Flutter中,使用Text小部件的 maxLines
属性来指定要包含在摘要中的行数,以及用于处理溢出文本的属性 overflow
。
var container = new Container( // grey box
child: new Center(
child: new Container( // red box
child: new Text(
"Lorem ipsum dolor sit amet, consec etur",
style: bold24Roboto,
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
decoration: new BoxDecoration(
backgroundColor: Colors.red[400],
),
padding: new EdgeInsets.all(16.0),
),
),
width: 320.0,
height: 240.0,
color: Colors.grey[300],
);