feast_otherset_manage_edit_page.dart 12.4 KB
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:one_key/widgets/v_empty_view.dart';
import 'package:provider/provider.dart';

import '../../../models/otherset_model.dart';
import '../../../provider/feast_model.dart';
import '../../../provider/user_model.dart';
import '../../../res/colors.dart';
import '../../../route/navigator_util.dart';
import '../../../utils/dialog_util.dart';
import '../../../utils/event_bus.dart';
import '../../../utils/screen_util.dart';
import '../../../utils/utils.dart';
import '../../../widgets/h_empty_view.dart';
import '../../../widgets/round_button.dart';
import '../../../widgets/system_app_bar.dart';

/**
 * 其他配置添加
 */
class FeastOthersetManageEditPage extends StatefulWidget {
  final OthersetSunModel othersetSunModel;
  FeastOthersetManageEditPage({
    Key key,
    this.othersetSunModel,
  }) : super(key: key);

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

class _FeastOthersetManageEditPageState
    extends State<FeastOthersetManageEditPage> {
  TextEditingController othersetNameC = TextEditingController(); //其他配置名称
  FocusNode othersetF = FocusNode();
  TextEditingController othersetPriceC = TextEditingController(); //其他配置价格
  FocusNode othersetPriceF = FocusNode();
  TextEditingController othersetPerC = TextEditingController(); //其他配置规格
  FocusNode othersetPerF = FocusNode();
  bool canBargaining = true; //是否议价
  ScrollController controller = ScrollController();

  FeastModel feastModel;

  @override
  void initState() {
    super.initState();
    Utils.changeStatusColor(color: MyColours.color_438E93);
    if (widget.othersetSunModel != null) {
      othersetNameC.text = widget.othersetSunModel.otherName;
      othersetPriceC.text = widget.othersetSunModel.price;
      othersetPerC.text = widget.othersetSunModel.spec;
      canBargaining = widget.othersetSunModel.bargaining == '1' ? true : false;
    }
  }

  _setOthersetList() async {
    Map<String, String> params = {
      "sid": Provider.of<UserModel>(context, listen: false).user.sellerid,
      'class_name': othersetNameC.text,
      'price': othersetPriceC.text,
      'spec': othersetPerC.text,
      'bargaining': canBargaining ? '1' : '0'
    };
    if (widget.othersetSunModel != null) {
      params['id'] = widget.othersetSunModel.id;
    }
    bool success = await feastModel.setOthersetList(context, params);
    if (success) {
      Utils.showToast('添加成功');
      bus.emit('feast_otherset_list');
      Navigator.pop(context);
      // _getWine(isShowLoading: true);
    }
  }

  ///请输入
  ///输入框
  Widget _textfield(
      String title,
      String hintText,
      TextEditingController editingController,
      FocusNode focusNode,
      String subTitle,
      {TextInputType inputType = TextInputType.number}) {
    return Container(
      height: w(48),
      margin: EdgeInsets.only(top: w(12)),
      decoration: BoxDecoration(
          border: Border(
              bottom:
                  BorderSide(width: h(0.5), color: MyColours.color_E7E7E7))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: w(81),
            height: w(48),
            alignment: Alignment.center,
            child: Text(
              title,
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(32),
                  color: MyColours.color_E5000000),
            ),
          ),
          HEmptyView(32),
          Expanded(
            child: Container(
                // height: ScreenUtil().setHeight(120),
                height: w(48),
                padding: EdgeInsets.only(top: w(4)),
                child: TextField(
                  controller: editingController,
                  // textAlign: TextAlign.right,
                  // textAlignVertical: TextAlignVertical.bottom,
                  maxLines: 1,
                  inputFormatters: [],

                  // //光标颜色
                  // cursorColor:
                  //     widget.isSelect ? Colors.white : MyColours.color_199095,
                  //输入文本的样式
                  style: TextStyle(
                      fontSize: ScreenUtil().setSp(32),
                      color: MyColours.color_515151),
                  focusNode: focusNode,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(borderSide: BorderSide.none),
                    hintText: hintText,
                    hintStyle: TextStyle(
                      color: hintText == '请输入'
                          ? MyColours.color_999999
                          : MyColours.color_515151,
                      fontSize: ScreenUtil().setSp(32),
                    ),
                    contentPadding: EdgeInsets.zero,
                  ),
                  onChanged: (value) {
                    if (widget.othersetSunModel != null && title == '配置名称') {
                      othersetNameC.text = value;
                      othersetNameC.value = TextEditingValue(
                          text: othersetNameC.text,
                          selection: TextSelection.fromPosition(TextPosition(
                            affinity: TextAffinity.downstream,
                            offset: othersetNameC.text.length,
                          )));
                      setState(() {});
                    }
                  },
                  onEditingComplete: () {
                    FocusScope.of(context)
                        .requestFocus(FocusNode()); //点击空白处隐藏键盘
                  },
                  keyboardType: inputType,
                )),
          ),
          Container(
            padding: EdgeInsets.only(left: w(16), right: w(12)),
            child: Text(
              subTitle,
              style: TextStyle(
                  color: MyColours.color_000000,
                  fontSize: ScreenUtil().setSp(32)),
            ),
          ),
        ],
      ),
    );
  }

  Widget _callButton(
      {GestureTapCallback onPress,
      content,
      hasLeftBorder = true,
      isSelect = false}) {
    return InkWell(
      onTap: onPress,
      child: Container(
        height: h(48),
        decoration: BoxDecoration(
            color: isSelect ? MyColours.color_438E93 : MyColours.color_ffffff,
            border: Border(
                bottom: BorderSide(
                    color: MyColours.color_438E93,
                    width: ScreenUtil().setWidth(2)),
                top: BorderSide(
                    color: MyColours.color_438E93,
                    width: ScreenUtil().setWidth(2)),
                left: hasLeftBorder
                    ? BorderSide(
                        color: MyColours.color_438E93,
                        width: ScreenUtil().setWidth(2))
                    : BorderSide.none,
                right: BorderSide(
                    color: MyColours.color_438E93,
                    width: ScreenUtil().setWidth(2)))),
        child: Align(
          alignment: Alignment.center,
          child: Text(
            content,
            style: TextStyle(
                color:
                    isSelect ? MyColours.color_F6F6F6 : MyColours.color_438E93,
                fontSize: ScreenUtil().setSp(36)),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    feastModel = Provider.of<FeastModel>(context, listen: true);
    return Scaffold(
        backgroundColor: MyColours.color_ffffff,
        appBar: SystemAppBar(
          widget.othersetSunModel == null ? '添加配置' : othersetNameC.text,
          needWhite: true,
          bgColor: MyColours.color_438E93,
          titleColor: MyColours.color_F6F6F6,
          onTap: () {
            Navigator.pop(context);
          },
        ),
        body: Column(
          children: [
            Expanded(
                child: Padding(
                    padding: EdgeInsets.only(
                      left: w(16),
                    ),
                    child: SingleChildScrollView(
                        controller: controller,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            _textfield(
                                '配置名称', '请输入', othersetNameC, othersetF, '',
                                inputType: TextInputType.text),
                            _textfield('配置单价', '请输入', othersetPriceC,
                                othersetPriceF, '元'),
                            _textfield(
                                '配置规格', '请输入', othersetPerC, othersetPerF, '',
                                inputType: TextInputType.text),
                            Container(
                              height: w(48),
                              margin: EdgeInsets.only(top: w(12)),
                              decoration: BoxDecoration(
                                  border: Border(
                                      bottom: BorderSide(
                                          width: h(0.5),
                                          color: MyColours.color_E7E7E7))),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Container(
                                    width: w(81),
                                    height: w(48),
                                    alignment: Alignment.center,
                                    child: Text(
                                      '是否议价',
                                      style: TextStyle(
                                          fontSize: ScreenUtil().setSp(32),
                                          color: MyColours.color_E5000000),
                                    ),
                                  ),
                                  Spacer(),
                                  InkWell(
                                      onTap: () {
                                        setState(() {
                                          canBargaining = !canBargaining;
                                        });
                                      },
                                      child: Container(
                                          padding: EdgeInsets.only(
                                              left: w(16), right: w(16)),
                                          child: Image.asset(
                                            Utils.getImgPath(canBargaining
                                                ? 'swich_on'
                                                : 'swich_off'),
                                            width: w(44),
                                          ))),
                                ],
                              ),
                            ),
                          ],
                        )))),
            Row(
              children: [
                Expanded(
                    child: _callButton(
                        onPress: () {
                          Navigator.pop(context);
                        },
                        content: '返回',
                        hasLeftBorder: true,
                        isSelect: false)),
                Expanded(
                    child: _callButton(
                        onPress: () {
                          if (othersetNameC.text.trim().length == 0) {
                            Utils.showToast('请输入配置名称');
                            return;
                          }
                          if (othersetPriceC.text.trim().length == 0) {
                            Utils.showToast('请输入配置单价');
                            return;
                          }
                          if (othersetPerC.text.trim().length == 0) {
                            Utils.showToast('请输入配置规格');
                            return;
                          }
                          _setOthersetList();
                        },
                        content: '保存',
                        hasLeftBorder: true,
                        isSelect: true))
              ],
            )
          ],
        ));
  }
}