Flutter 项目实战 实现上传头像和个人资料 (五)

/  编辑个人资资料界面  /

 /  编辑资料导航栏  /

ValueListenableBuilder通过 ValueListenableBuilder 实现局部刷新 , 当用户点击导航栏保存按钮时展现一个提交资料的进度 (网络请求) , 这时只会刷新导航栏的保存按钮 , 不会对整个界面进行刷新 .

Widget saveWidget(final PEditIntro _pEditIntro,
    final ValueNotifier<bool> _valueNotifierSave,
    final Map<String, String> _editIntroMap) {
  return ValueListenableBuilder<bool>(
      builder: (BuildContext context, bool value, Widget? child) {
        return value
            ? Container(
                width: 180.w,
                alignment: Alignment.center,
                child: CircularProgressIndicator(
                  strokeWidth: 12.w,
                  backgroundColor: Colors.blue,
                  valueColor: const AlwaysStoppedAnimation<Color>(Colors.red),
                ),
              )
            : child!;
      },
      valueListenable: _valueNotifierSave,
      child: GestureDetector(
        onTap: () {
          _pEditIntro.editIntro(_editIntroMap);
        },
        child: Container(
          width: 180.w,
          color: Colors.red.withOpacity(0.0),
          alignment: Alignment.center,
          child: Text(
            '保存',
            style: TextStyle(fontSize: 40.sp),
          ),
        ),
      ));
}
AppBar(
          title: Text(
            "编辑资料",
            style: TextStyle(fontSize: 50.sp),
          ),
          centerTitle: true,
          backgroundColor: Colors.blue.withOpacity(0.8),
          elevation: 0.0,
          actions: [
            saveWidget(_pEditIntro, _valueNotifierSave, _editIntroMap),
          ],
        ),

 /  上传个人头像  /

在 pubspec.yaml 配置 image_picker 依赖 (any用版本号代替也可以)

 image_picker: any #选择手机图片

选择图片实现头像上传

 edit_intro_page.dart

...

onTap: () async {
                  /// 隐藏软键盘
                  FocusScope.of(context).requestFocus(FocusNode());
                  final ImagePicker _picker = ImagePicker();
                  final XFile? image =
                      await _picker.pickImage(source: ImageSource.gallery);
                  String imagePath = image!.path;
                  logV('上传头像地址:$imagePath');
                  _pEditIntro.uploadHead(imagePath);
                },

...

m_edit_intro.dart

@override
  uploadHead(String file, SuccessCallback s, FailureCallback f) async {
    /// 获取要上传图片的名称
    var _fileName = file.substring(file.lastIndexOf("/") + 1, file.length);
    MultipartFile _fromFile = await MultipartFile.fromFile(
      file,
      filename: _fileName,
    );
    HttpManager().upLoad(
      url: '/upload/file',
      tag: tag,
      data: FormData.fromMap({
        'file': _fromFile,
        'type': 'head',
      }),
      options: Options(
        contentType: "multipart/form-data",
      ),
      successCallback: (data) {
        s(data);
      },
      errorCallback: (data) {
        f(data);
      },
    );
  }

http_manager.dart

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

️ 邪神

你自己看着办,你喜欢打赏我就赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值