flutter 圆角、圆形头像封装工具类

可使用圆角,圆形头像,可设置边框颜色,更多的需求自己加

import 'package:flutter/material.dart';

///头像处理类
class AvatarUtil extends StatelessWidget {
  final String imgUrl;
  final double width;
  final double height;
  final double padding;
  final BoxShape boxShape;
  final double radius;
  final Color boxBroadColor;

  AvatarUtil(
    this.imgUrl, {
    this.width = 0,
    this.height = 0,
    this.padding = 0,
    this.boxShape = BoxShape.circle,
    this.radius = 0,
    this.boxBroadColor = Colors.white,
  });

  @override
  Widget build(BuildContext context) {

    _getChildWidget() {
      return boxShape == BoxShape.circle
          ? CircleAvatar(
              backgroundImage: NetworkImage(imgUrl),
            )
          : ClipRRect(
              borderRadius: BorderRadius.circular(radius),
              child: Image.network(
                imgUrl,
                fit: BoxFit.cover,
              ),
            );
    }
    _getBorderRadius(){
      return boxShape == BoxShape.circle
          ? BoxDecoration(
        shape: boxShape,
        color: boxBroadColor,
      )
          : BoxDecoration(
        shape: boxShape,
        color: boxBroadColor,
        borderRadius: BorderRadius.circular(radius),
      );
    }
    return Container(
        width: width,
        height: height,
        child: _getChildWidget(),
        padding: EdgeInsets.all(padding),
        decoration:_getBorderRadius());
  }
}

使用:

flutter 圆角、圆形头像封装工具类_第1张图片

效果图:

flutter 圆角、圆形头像封装工具类_第2张图片flutter 圆角、圆形头像封装工具类_第3张图片

flutter 圆角、圆形头像封装工具类_第4张图片

你可能感兴趣的:(flutter)