Flutter——最详细的Image(图像)使用教程

Image简介

用于显示一张图片,可以从文件、内存、网络、资源。可以指定适应方式、样式、颜色混合模式、重复模式等。

常用属性

属性 作用
fit 图片的适应模式
alignment 图片的对齐模式

获取资源文件和网络图片

class ImageWidget extends StatelessWidget {
  final assetsImagePath = "assets/images/flutter_mark_logo.png";
  final netImageUrl =
      "https://flutterchina.club/images/homepage/header-illustration.png";

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        _loadFromAssets(),
        _loadFromNet(),
      ],
    );
  }

  Widget _loadFromAssets() => Wrap(
        spacing: 10,
        children: <Widget>[
          Image.asset(assetsImagePath, height: 80, width: 80),
        ],
      );

  Widget _loadFromNet() => Image.network(netImageUrl, height: 80);
}

Flutter——最详细的Image(图像)使用教程

属性:fit:BoxFit.values

class FitImage extends StatefulWidget {
  @override
  _FitImageState createState() => _FitImageState();
}

class _FitImageState extends State<FitImage> {
  @override
  Widget build(BuildContext context) {
    var imageLi = BoxFit.values
        .toList()
        .map((mode) => Column(children: <Widget>[
              Container(
                  margin: EdgeInsets.all(5),
                  width: 100,
                  height: 80,
                  color: Colors.grey.withAlpha(88),
                  child: Image(
                      image: AssetImage("assets/images/avaver.jpeg"),
                      fit: mode)),
              Text(mode.toString().split(".")[1])
            ]))
        .toList();

    return Wrap(
      children: [...imageLi],
    );
  }
}

Flutter——最详细的Image(图像)使用教程

属性:Alignment


class AlignmentImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var alignment = [
      Alignment.center,
      Alignment.centerLeft,
      Alignment.centerRight,
      Alignment.topCenter,
      Alignment.topLeft,
      Alignment.topRight,
      Alignment.bottomCenter,
      Alignment.bottomLeft,
      Alignment.bottomRight
    ]; //测试数组
    var imgLi = alignment
        .map((alignment) => //生成子Widget列表
            Column(children: <Widget>[
              Container(
                  margin: EdgeInsets.all(5),
                  width: 90,
                  height: 60,
                  color: Colors.grey.withAlpha(88),
                  child: Image(
                    image: AssetImage("assets/images/avaver.jpeg"),
                    alignment: alignment,
                  )),
              Text(alignment.toString())
            ]))
        .toList();
    var imageAlignment = Wrap(children: imgLi);
    return imageAlignment;
  }
}

Flutter——最详细的Image(图像)使用教程

图片重复模式

class RepeatImage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Wrap(
      children: ImageRepeat.values
          .toList()
          .map((mode) => Column(children: <Widget>[
                Container(
                    margin: EdgeInsets.all(5),
                    width: 150,
                    height: 60,
                    color: Colors.red,
                    child: Image(
                        image: AssetImage("assets/images/avaver.jpeg"),
                        repeat: mode)),
                Text(mode.toString().split(".")[1])
              ]))
          .toList(),
    );
  }
}

Flutter——最详细的Image(图像)使用教程

上一篇:JS21. 使用原生JS封装一个公共的Alert插件(HTML5: Shadow Dom)


下一篇:numpy的convolve方法的运算逻辑