feast_wine_manage_edit_page.dart 12 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/wine_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 FeastWineManageEditPage extends StatefulWidget {
  final WineSunModel wineSunModel;
  FeastWineManageEditPage({
    Key key,
    this.wineSunModel,
  }) : super(key: key);

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

class _FeastWineManageEditPageState extends State<FeastWineManageEditPage> {
  TextEditingController wineNameC = TextEditingController(); //酒水名称
  FocusNode wineF = FocusNode();
  TextEditingController winePriceC = TextEditingController(); //酒水价格
  FocusNode winePriceF = FocusNode();
  TextEditingController winePerC = TextEditingController(); //酒水规格
  FocusNode winePerF = FocusNode();
  bool canBargaining = true; //是否议价
  ScrollController controller = ScrollController();
  FeastModel feastModel;

  @override
  void initState() {
    super.initState();
    Utils.changeStatusColor(color: MyColours.color_438E93);
    if (widget.wineSunModel != null) {
      wineNameC.text = widget.wineSunModel.wineName;
      winePriceC.text = widget.wineSunModel.price;
      winePerC.text = widget.wineSunModel.spec;
      canBargaining = widget.wineSunModel.bargaining == '1' ? true : false;
    }
  }

  _setWine() async {
    Map<String, String> params = {
      "sid": Provider.of<UserModel>(context, listen: false).user.sellerid,
      'wine_name': wineNameC.text,
      'price': winePriceC.text,
      'spec': winePerC.text,
      'bargaining': canBargaining ? '1' : '0'
    };
    if (widget.wineSunModel != null) {
      params['id'] = widget.wineSunModel.id;
    }
    bool success = await feastModel.setWine(context, params);
    if (success) {
      Utils.showToast('添加成功');
      bus.emit('feast_wine_list');
      Navigator.pop(context);
      // _getWine(isShowLoading: true);
    }
  }

  ///请输入
  ///输入框
  Widget _textfield(
      String title,
      String hintText,
      TextEditingController editingController,
      FocusNode focusNode,
      String subTitle,
      {TextInputType inputType = TextInputType.text}) {
    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.wineSunModel != null && title == '酒水名称') {
                      wineNameC.text = value;
                      wineNameC.value = TextEditingValue(
                          text: wineNameC.text,
                          selection: TextSelection.fromPosition(TextPosition(
                            affinity: TextAffinity.downstream,
                            offset: wineNameC.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.wineSunModel == null ? '添加酒水' : wineNameC.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('酒水名称', '请输入', wineNameC, wineF, ''),
                            _textfield(
                                '酒水单价', '请输入', winePriceC, winePriceF, '元',
                                inputType: TextInputType.numberWithOptions()),
                            _textfield('酒水规格', '请输入', winePerC, winePerF, ''),
                            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 (wineNameC.text.trim().length == 0) {
                            Utils.showToast('请输入酒水名称');
                            return;
                          }
                          if (winePriceC.text.trim().length == 0) {
                            Utils.showToast('请输入酒水单价');
                            return;
                          }
                          if (winePerC.text.trim().length == 0) {
                            Utils.showToast('请输入酒水规格');
                            return;
                          }
                          _setWine();
                        },
                        content: '保存',
                        hasLeftBorder: true,
                        isSelect: true))
              ],
            )
          ],
        ));
  }
}