在新的一周中,我们将以问答形式对Flutter官方文档进行另一种解释。第4部分介绍Flutter针对Web开发人员的比较风格。它完全致力于布局,而且没有以前的那么庞大。传统上,我建议所有对Flutter感兴趣的Web开发人员仔细研究一下,看看是否值得尝试该框架以及需要付出多少努力。
如果这里没有足够的信息,或者您在特定平台的本机开发方面有经验,那么我建议您研究其他部分:Flutter。第1部分。适用于Android开发人员Flutter。第2部分。面向iOS开发人员Flutter。第3部分。面向React NativeFlutter 开发人员。第4部分。针对Flutter Web开发人员。第5部分。对于Xamarin.Forms开发人员内容:
- 基本布局
- 位置和大小
- 表格
- 文本
基本布局
题:
如何设置文字样式和对齐方式?回答:
使用TextStyle。例:
HTML / CSS<div class="greybox">
Lorem ipsum
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Georgia;
}
扑var container = Container(
child: Text(
"Lorem ipsum",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.w900,
fontFamily: "Georgia",
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
题:
如何设置背景颜色?回答
使用BoxDecoration类。差异性
background-color
CSS中
的属性仅负责背景颜色。BoxDecoration负责更广泛的属性,例如圆角,磨边等。例
HTML / CSS<div class="greybox">
Lorem ipsum
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
}
扑var container = Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
width: 320,
height: 240,
decoration: BoxDecoration(
color: Colors.grey[300],
),
);
题:
如何将组件居中?回答
使用中心小部件。例
HTML / CSS<div class="greybox">
Lorem ipsum
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
扑var container = Container(
child: Center(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
题:
如何设置容器宽度?回答
使用属性width
。差异性
Flutter小部件具有width
固定的属性。要配置maxWidth
或minWidth
使用BoxConstraints小部件。例:
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
width: 100%;
max-width: 240px;
}
扑var container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
width: 240,
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
位置和大小
题
如何建立绝对位置?回答
使用Stack部件中的Positioned部件。附加信息
默认情况下,小部件位于父小部件内。例
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
position: relative;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
position: absolute;
top: 24px;
left: 24px;
}
扑var container = Container(
child: Stack(
children: [
Positioned(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
left: 24,
top: 24,
),
],
),
width: 320,
height: 240,
color: Colors.grey[300],
);
题
如何设置组件的旋转度?回答
使用“ 变换”小部件。例
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
transform: rotate(15deg);
}
扑var container = Container(
child: Center(
child: Transform(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
textAlign: TextAlign.center,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
alignment: Alignment.center,
transform: Matrix4.identity()
..rotateZ(15 * 3.1415927 / 180),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
题
如何缩放组件?回答
使用“ 变换”小部件。例
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
transform: scale(1.5);
}
扑var container = Container(
child: Center(
child: Transform(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
textAlign: TextAlign.center,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
alignment: Alignment.center,
transform: Matrix4.identity()
..scale(1.5),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
题
如何应用渐变?回答
使用BoxDecoration类及其属性gradient
。例
HTML / CSS 垂直线性渐变<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
padding: 16px;
color: #ffffff;
background: linear-gradient(180deg, #ef5350, rgba(0, 0, 0, 0) 80%);
}
扑var container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: const Alignment(0.0, -1.0),
end: const Alignment(0.0, 0.6),
colors: <Color>[
const Color(0xffef5350),
const Color(0x00ef5350)
],
),
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
HTML / CSS 水平线性渐变<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
padding: 16px;
color: #ffffff;
background: linear-gradient(90deg, #ef5350, rgba(0, 0, 0, 0) 80%);
}
扑var container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
gradient: LinearGradient(
begin: const Alignment(-1.0, 0.0),
end: const Alignment(0.6, 0.0),
colors: <Color>[
const Color(0xffef5350),
const Color(0x00ef5350)
],
),
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
表格
题
如何圆角?回答
使用BoxDecoration类及其属性borderRadius
。例
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
border-radius: 8px;
}
扑var container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
color: Colors.red[400],
borderRadius: BorderRadius.all(
const Radius.circular(8),
),
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
题
如何添加阴影?回答
使用BoxShadow类。附加信息
BoxShadow用作BoxDecorationboxShadow
类属性的一部分。例
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.8),
0 6px 20px rgba(0, 0, 0, 0.5);
}
扑var container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
),
decoration: BoxDecoration(
color: Colors.red[400],
boxShadow: [
BoxShadow (
color: const Color(0xcc000000),
offset: Offset(0, 2),
blurRadius: 4,
),
BoxShadow (
color: const Color(0x80000000),
offset: Offset(0, 6),
blurRadius: 20,
),
],
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
decoration: BoxDecoration(
color: Colors.grey[300],
),
margin: EdgeInsets.only(bottom: 16),
);
题
如何制作圆形和椭圆形?回答
使用BoxShapeenum
类。附加信息
BoxShape用作BoxDecoration类的shape属性的一部分。例
HTML / CSS<div class="greybox">
<div class="redcircle">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redcircle {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
text-align: center;
width: 160px;
height: 160px;
border-radius: 50%;
}
扑var container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: bold24Roboto,
textAlign: TextAlign.center,
),
decoration: BoxDecoration(
color: Colors.red[400],
shape: BoxShape.circle,
),
padding: EdgeInsets.all(16),
width: 160,
height: 160,
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
文本
题
如何调整文字之间的距离?回答
使用TextStyle类及其属性letterSpacing
和wordSpacing
。例
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
letter-spacing: 4px;
}
扑var container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum",
style: TextStyle(
color: Colors.white,
fontSize: 24,
fontWeight: FontWeight.w900,
letterSpacing: 4,
),
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
题
如何格式化部分文字?回答
使用RichText小部件和TextSpan类。例
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem <em>ipsum</em>
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
}
.redbox em {
font: 300 48px Roboto;
font-style: italic;
}
扑var container = Container(
child: Center(
child: Container(
child: RichText(
text: TextSpan(
style: bold24Roboto,
children: <TextSpan>[
TextSpan(text: "Lorem "),
TextSpan(
text: "ipsum",
style: TextStyle(
fontWeight: FontWeight.w300,
fontStyle: FontStyle.italic,
fontSize: 48,
),
),
],
),
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
题
如何限制长文本的显示?回答
借助小部件maxLines
和文本overflow
小部件。例
HTML / CSS<div class="greybox">
<div class="redbox">
Lorem ipsum dolor sit amet, consec etur
</div>
</div>
.greybox {
background-color: #e0e0e0;
width: 320px;
height: 240px;
font: 900 24px Roboto;
display: flex;
align-items: center;
justify-content: center;
}
.redbox {
background-color: #ef5350;
padding: 16px;
color: #ffffff;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
扑var container = Container(
child: Center(
child: Container(
child: Text(
"Lorem ipsum dolor sit amet, consec etur",
style: bold24Roboto,
overflow: TextOverflow.ellipsis,
maxLines: 1,
),
decoration: BoxDecoration(
color: Colors.red[400],
),
padding: EdgeInsets.all(16),
),
),
width: 320,
height: 240,
color: Colors.grey[300],
);
在Flutter 1.12发行版的2019年12月底,Web支持进入beta版!这是个好消息,这意味着在不久的将来,我们将越来越多地看到用Flutter编写的网站。因此,如果您仍然有想法,建议您尝试使用此框架,并希望本文对您有所帮助。今天就这些。希望Google不会破坏您的Chrome!