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


    /  编辑个人资资料界面  /

     /  编辑资料导航栏  /

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

    1. Widget saveWidget(final PEditIntro _pEditIntro,
    2. final ValueNotifier _valueNotifierSave,
    3. final Map _editIntroMap) {
    4. return ValueListenableBuilder(
    5. builder: (BuildContext context, bool value, Widget? child) {
    6. return value
    7. ? Container(
    8. width: 180.w,
    9. alignment: Alignment.center,
    10. child: CircularProgressIndicator(
    11. strokeWidth: 12.w,
    12. backgroundColor: Colors.blue,
    13. valueColor: const AlwaysStoppedAnimation(Colors.red),
    14. ),
    15. )
    16. : child!;
    17. },
    18. valueListenable: _valueNotifierSave,
    19. child: GestureDetector(
    20. onTap: () {
    21. _pEditIntro.editIntro(_editIntroMap);
    22. },
    23. child: Container(
    24. width: 180.w,
    25. color: Colors.red.withOpacity(0.0),
    26. alignment: Alignment.center,
    27. child: Text(
    28. '保存',
    29. style: TextStyle(fontSize: 40.sp),
    30. ),
    31. ),
    32. ));
    33. }
    1. AppBar(
    2. title: Text(
    3. "编辑资料",
    4. style: TextStyle(fontSize: 50.sp),
    5. ),
    6. centerTitle: true,
    7. backgroundColor: Colors.blue.withOpacity(0.8),
    8. elevation: 0.0,
    9. actions: [
    10. saveWidget(_pEditIntro, _valueNotifierSave, _editIntroMap),
    11. ],
    12. ),

     /  上传个人头像  /

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

     image_picker: any #选择手机图片

    选择图片实现头像上传

     edit_intro_page.dart

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

    m_edit_intro.dart

    1. @override
    2. uploadHead(String file, SuccessCallback s, FailureCallback f) async {
    3. /// 获取要上传图片的名称
    4. var _fileName = file.substring(file.lastIndexOf("/") + 1, file.length);
    5. MultipartFile _fromFile = await MultipartFile.fromFile(
    6. file,
    7. filename: _fileName,
    8. );
    9. HttpManager().upLoad(
    10. url: '/upload/file',
    11. tag: tag,
    12. data: FormData.fromMap({
    13. 'file': _fromFile,
    14. 'type': 'head',
    15. }),
    16. options: Options(
    17. contentType: "multipart/form-data",
    18. ),
    19. successCallback: (data) {
    20. s(data);
    21. },
    22. errorCallback: (data) {
    23. f(data);
    24. },
    25. );
    26. }

    http_manager.dart

    
    
  • 相关阅读:
    Java for循环语句
    sql常用基础语句
    让您了解GPS北斗卫星时间同步系统(NTP时钟同步)重要性
    JavaScript+css实现的计时器动画素材html页面前端源码
    Codeforces Round #803 (Div. 2) C. 3SUM Closure
    一步一图带你深入剖析 JDK NIO ByteBuffer 在不同字节序下的设计与实现
    FindMy技术智能防丢物品
    C++实现查询本机信息并且上报
    SpringBoot+Vue项目自媒体社区平台
    ctf-pikachu-file_inclusion
  • 原文地址:https://blog.csdn.net/u013491829/article/details/126560724