一篇好的基础文章


Text

  1. body: Center(
  2. child: Text(
  3. '文本内容',
  4. textAlign: TextAlign.center, //文本显示
  5. maxLines: 1, // 文本显示行数
  6. overflow: TextOverflow.ellipsis, //溢出内容怎么显示
  7. style: TextStyle(
  8. fontSize: 24, //字体大小
  9. color: Color.fromAGRB(255, 255, 100, 100), //字体颜色
  10. decoration: TextDecoration.underline, //下划线
  11. decorationStyle: TextDecorationStyle.dashed, //下划线(实线、虚线等)
  12. )
  13. )
  14. )

Container

  1. body: Center(
  2. child: Container(
  3. child: Text('暮光破晓', style: TextStyle(fontSize: 26.0, color: Color.fromARGB(255, 0, 0, 50))),
  4. alignment: Alignment.topLeft,
  5. width: 500.0,
  6. height: 400.0,
  7. // color: Colors.lightBlue,
  8. // padding: const EdgeInsets.all(10.0),
  9. padding: const EdgeInsets.fromLTRB(10.0, 100.0, 0.0, 0.0),
  10. margin: const EdgeInsets.all(10.0),
  11. decoration: new BoxDecoration(
  12. gradient: const LinearGradient( // 渐变色
  13. colors: [Colors.lightBlue, Colors.greenAccent, Colors.purple]
  14. ),
  15. border: Border.all(width: 5.0, color: Colors.red) // 边框
  16. ),
  17. ),
  18. )

Image

  1. body: Center(
  2. child: Container(
  3. child: new Image.network(
  4. 'https://images.pexels.com/photos/1784578/pexels-photo-1784578.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500',
  5. fit: BoxFit.cover,
  6. color: Colors.green, // 图片颜色混合
  7. colorBlendMode: BlendMode.darken, // 图片颜色模式
  8. repeat: ImageRepeat.noRepeat, // 图片和容器的重复关系
  9. ),
  10. width: 300.0,
  11. height: 500.0,
  12. color: Colors.green,
  13. )
  14. )

Offstage

Offstage 通过控制offstage来控制child是否显示。
Offstage的布局行为完全取决于其offstage参数

  • 当offstage为true(默认为true),当前控件不会被绘制在屏幕上,不会响应点击事件,也不会占用空间;
  • 当offstage为false,当前控件则跟平常用的控件一样渲染绘制;

注意:当Offstage不可见的时候,如果child有动画,应该手动停掉,Offstage并不会停掉动画。
Offstage并不是通过插入或者删除自己在widget tree中的节点,来达到显示以及隐藏的效果,而是通过设置自身尺寸、不响应hitTest以及不绘制,来达到展示与隐藏的效果。

  1. Column(
  2. children: <Widget>[
  3. new Offstage(
  4. offstage: offstage,
  5. child: Container(color: Colors.blue, height: 100.0),
  6. ),
  7. new CupertinoButton(
  8. child: Text("点击切换显示"),
  9. onPressed: () {
  10. setState(() {
  11. offstage = !offstage;
  12. });
  13. },
  14. ),
  15. ],
  16. )