base_info.dart 15.3 KB
import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:one_key/models/upload_img_model.dart';
import 'package:one_key/pages/online_check/progress.dart';
import 'package:one_key/provider/base_info_provider.dart';
import 'package:one_key/provider/upload_img_provider.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/event_bus.dart';
import 'package:one_key/utils/upload_image_util.dart';
import 'package:one_key/utils/utils.dart';
import 'package:one_key/widgets/h_empty_view.dart';
import 'package:one_key/widgets/round_button.dart';
import 'package:one_key/widgets/system_app_bar.dart';
import 'package:one_key/widgets/v_empty_view.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';

class BaseInfoPage extends StatefulWidget {
  BaseInfoPage({Key key}) : super(key: key);

  @override
  _BaseInfoPageState createState() => _BaseInfoPageState();
}

class _BaseInfoPageState extends State<BaseInfoPage> {
  ScrollController _scrollController;
  TextEditingController _nameController = TextEditingController();
  TextEditingController _phoneController = TextEditingController();
  TextEditingController _positionController = TextEditingController();

  FocusNode _nameFocusNode = FocusNode();
  FocusNode _phoneFocusNode = FocusNode();
  FocusNode _positionFocusNode = FocusNode();

  final picker = ImagePicker();
  // OnlineCheckEditProvider _onlineCheckEditProvider;
  UploadImageProvider _uploadImageProvider;
  BaseIndoProvider _baseIndoProvider;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _uploadImageProvider.touxiang.process = 0.01;
      _uploadImageProvider.touxiang.qiniuUrl = '';
      _uploadImageProvider.erweima.process = 0.01;
      _uploadImageProvider.erweima.qiniuUrl = '';
      _uploadImageProvider.imgSizeTouxiang = 64;
      _uploadImageProvider.imgSizeErweima = 22;
      _getQiniuInfo();
      _getCustomerActivityBaseinfo();
    });
  }

  _getCustomerActivityBaseinfo() async {
    Map<String, String> params = {
      'sellerid': Provider.of<UserModel>(context, listen: false).user.sellerid,
      "uid": Provider.of<UserModel>(context, listen: false).user.openid,
    };
    bool success =
        await _baseIndoProvider.getCustomerActivityBaseinfo(context, params);
    if (success) {
      _nameController.text = _baseIndoProvider.baseInfoSunModel.name;
      _uploadImageProvider.touxiang.qiniuUrl =
          _baseIndoProvider.baseInfoSunModel.photo;
      _uploadImageProvider.erweima.qiniuUrl =
          _baseIndoProvider.baseInfoSunModel.qrcode;
      _phoneController.text = _baseIndoProvider.baseInfoSunModel.phone;
      _positionController.text = _baseIndoProvider.baseInfoSunModel.position;
      _uploadImageProvider.imgSizeErweima = 64;
      setState(() {});
    }
  }

  ///获取七牛云token
  ///
  _getQiniuInfo() async {
    await _uploadImageProvider.getQiniuInfo(context, {});
  }

  //客户经理添加/修改基本信息
  _setCustomerActivityBaseinfo() async {
    FocusScope.of(context).requestFocus(FocusNode()); //点击空白处隐藏键盘
    Map<String, String> params = {
      'sellerid': Provider.of<UserModel>(context, listen: false).user.sellerid,
      "uid": Provider.of<UserModel>(context, listen: false).user.openid,
      "name": _nameController.text.trim(),
      "photo": _uploadImageProvider.touxiang.qiniuUrl,
      "phone": _phoneController.text.trim(),
      "position": _positionController.text,
      "qrcode": _uploadImageProvider.erweima.qiniuUrl,
    };
    bool isSuccess =
        await _baseIndoProvider.setCustomerActivityBaseinfo(context, params);
    if (isSuccess) {
      //成功之后跳回前一个页面
      // bus.emit('refresh_coupon_list');
      Utils.showToast('保存成功');
      EventBus().emit('changeStatusColor');
      Navigator.pop(context, true);
    }
  }

  ///输入框
  Widget _textfield(String title, String hintText,
      TextEditingController editingController, FocusNode focusNode) {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            child: Text(
              title,
              style: TextStyle(
                  color: MyColours.color_333333,
                  fontWeight: FontWeight.bold,
                  fontSize: ScreenUtil().setSp(34)),
            ),
          ),
          HEmptyView(80),
          Container(
            width: ScreenUtil().setWidth(231 * 2),
            child: TextField(
              controller: editingController,
              textAlign: TextAlign.right,
              textAlignVertical: TextAlignVertical.bottom,
              maxLines: 1,
              // //光标颜色
              // cursorColor:
              //     widget.isSelect ? Colors.white : MyColours.color_199095,
              //输入文本的样式
              style: TextStyle(
                fontSize: ScreenUtil().setSp(32),
              ),
              focusNode: focusNode,
              decoration: InputDecoration(
                border: OutlineInputBorder(borderSide: BorderSide.none),
                hintText: hintText,
                hintStyle: TextStyle(
                  color: MyColours.color_999999,
                  fontSize: ScreenUtil().setSp(32),
                ),
                enabledBorder: new UnderlineInputBorder(
                  borderSide: BorderSide.none,
                ),
                focusedBorder: new UnderlineInputBorder(
                  borderSide: BorderSide.none,
                ),
              ),
              onChanged: (value) {},
              onEditingComplete: () {
                FocusScope.of(context).requestFocus(FocusNode()); //点击空白处隐藏键盘
              },
              keyboardType: TextInputType.text,
            ),
          )
        ],
      ),
    );
  }

  ///#相机拍照,单图选择
  Future _getFromPictureOrCamera(
      String title, UploadImgModel imgModel, imageSource) async {
    final pickedFile = await picker.getImage(source: imageSource);
    setState(() {
      if (pickedFile != null) {
        imgModel.file = File(pickedFile.path);
        imgModel.process = 0.01;
      } else {
        print('No image selected.');
      }
    });
    if (pickedFile != null) {
      title == '头像'
          ? _uploadImageProvider.imgSizeTouxiang = 64
          : _uploadImageProvider.imgSizeErweima = 64;
      UploadImageUtil().startUploadOnly(context, File(pickedFile.path),
          title == '头像' ? 0 : 1, true, _uploadImageProvider);
    }
  }

  ///图片   头像和二维码
  Widget _imageWidget(
      String title, UploadImgModel model, double imgSize, double radius) {
    return InkWell(
        onTap: () {
          Utils.getPickPermission(context, () {
            _getFromPictureOrCamera(title, model, ImageSource.gallery);
          }, PermissionGroup.photos);
        },
        child: Container(
          height: ScreenUtil().setWidth(imgSize * 2),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                title,
                textAlign: TextAlign.left,
                style: TextStyle(
                    color: MyColours.color_333333,
                    fontWeight: FontWeight.bold,
                    fontSize: ScreenUtil().setSp(34)),
              ),
              Spacer(),
              Stack(
                children: [
                  ClipRRect(
                    borderRadius:
                        BorderRadius.circular(ScreenUtil().setWidth(radius)),
                    child: model.file == null
                        ? model.qiniuUrl != null
                            ? CachedNetworkImage(
                                imageUrl: model.qiniuUrl,
                                fit: BoxFit.cover,
                                width: ScreenUtil().setWidth(imgSize * 2),
                                height: ScreenUtil().setWidth(imgSize * 2),
                              )
                            : Image.asset(
                                Utils.getImgPath(
                                    title == '头像' ? 'touxiang' : 'erweima'),
                                fit: BoxFit.cover,
                                width: ScreenUtil().setWidth(imgSize * 2),
                                height: ScreenUtil().setWidth(imgSize * 2),
                              )
                        : Image.file(
                            model.file,
                            fit: BoxFit.cover,
                            width: ScreenUtil().setWidth(imgSize * 2),
                            height: ScreenUtil().setWidth(imgSize * 2),
                          ),
                  ),
                  model.file == null && model.qiniuUrl == null
                      ? Offstage(
                          offstage: model.process == 1,
                          child: Container(
                            width: ScreenUtil().setWidth(imgSize * 2),
                            height: ScreenUtil().setWidth(imgSize * 2),
                            decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(radius),
                                color: Color.fromRGBO(0, 0, 0, 0.2)),
                            child: Center(
                              child: Text('上传中',
                                  style:
                                      TextStyle(color: MyColours.color_333333)),
                            ),
                          ),
                        )
                      : Container(),
                ],
              ),
              HEmptyView(30),
              Image.asset(
                "assets/arr_right_gray.png",
                width: ScreenUtil().setWidth(14),
                height: ScreenUtil().setWidth(22),
              )
            ],
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    _uploadImageProvider =
        Provider.of<UploadImageProvider>(context, listen: true);
    _baseIndoProvider = Provider.of<BaseIndoProvider>(context, listen: true);
    return Scaffold(
        backgroundColor: MyColours.color_fefefe,
        appBar: SystemAppBar(
          '填写信息',
          onTap: () {
            EventBus().emit('changeStatusColor');
            Navigator.of(context).pop();
          },
        ),
        body: InkWell(
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode()); //点击空白处隐藏键盘
            },
            child: Column(children: [
              Expanded(
                child: SingleChildScrollView(
                    controller: _scrollController,
                    physics: BouncingScrollPhysics(),
                    child: Padding(
                        padding: EdgeInsets.all(ScreenUtil().setWidth(40)),
                        child: Column(
                          children: [
                            _imageWidget('头像', _uploadImageProvider.touxiang,
                                _uploadImageProvider.imgSizeTouxiang, 64),
                            VEmptyView(30),
                            Container(
                              height: ScreenUtil().setWidth(2),
                              color: MyColours.color_efefef,
                              margin: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(108 * 2)),
                            ),
                            VEmptyView(10),
                            _textfield('姓名', '请填写您的姓名', _nameController,
                                _nameFocusNode),
                            VEmptyView(10),
                            Container(
                              height: ScreenUtil().setWidth(2),
                              color: MyColours.color_efefef,
                              margin: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(108 * 2)),
                            ),
                            VEmptyView(10),
                            _textfield('电话', '请填写您的联系电话', _phoneController,
                                _phoneFocusNode),
                            VEmptyView(10),
                            Container(
                              height: ScreenUtil().setWidth(2),
                              color: MyColours.color_efefef,
                              margin: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(108 * 2)),
                            ),
                            VEmptyView(10),
                            _textfield('职位', '请填写您在餐厅中担任的职位',
                                _positionController, _positionFocusNode),
                            VEmptyView(10),
                            Container(
                              height: ScreenUtil().setWidth(2),
                              color: MyColours.color_efefef,
                              margin: EdgeInsets.only(
                                  left: ScreenUtil().setWidth(108 * 2)),
                            ),
                            VEmptyView(40),
                            _imageWidget('微信二维码', _uploadImageProvider.erweima,
                                _uploadImageProvider.imgSizeErweima, 0),
                            VEmptyView(30),
                          ],
                        ))),
              ),
              VEmptyView(20),
              Padding(
                  padding: EdgeInsets.fromLTRB(ScreenUtil().setWidth(40), 0,
                      ScreenUtil().setWidth(40), ScreenUtil().setWidth(40)),
                  child: RoundButton(
                    onPressed: () {
                      if (_uploadImageProvider.touxiang.qiniuUrl
                              .trim()
                              .length ==
                          0) {
                        Utils.showToast('请上传头像');
                        return;
                      }
                      if (_nameController.text.trim().length == 0) {
                        Utils.showToast('请填写您的姓名');
                        return;
                      }
                      if (_phoneController.text.trim().length == 0) {
                        Utils.showToast('请填写您的联系电话');
                        return;
                      }
                      if (_positionController.text.trim().length == 0) {
                        Utils.showToast('请填写您在餐厅中担任的职位');
                        return;
                      }
                      if (_uploadImageProvider.erweima.qiniuUrl.trim().length ==
                          0) {
                        Utils.showToast('请上传头像');
                        return;
                      }

                      //接口
                      _setCustomerActivityBaseinfo();
                    },
                    text: '保存',
                    fontSize: 34,
                    isBold: false,
                    height: ScreenUtil().setWidth(80),
                    width: double.infinity,
                    radius: ScreenUtil().setWidth(10),
                  ))
            ])));
  }
}