如何在Flutter中创建条件

xzv2uavs  于 2023-01-06  发布在  Flutter
关注(0)|答案(2)|浏览(155)

有两个文件有类似的代码,这是添加和更新区域功能,我决定使它只有一个,使用条件。
您可以看到这两个特性具有相似的小部件,只是文本不同。

添加区域要素

更新区域要素

在我的例子中,当用户想要添加区域时,UI将显示添加区域功能。当用户想要更新区域时,UI将显示更新区域功能,但它只来自一个代码库。
有可能提出那种条件吗?
这里我复制粘贴了整个添加区域功能的代码。我不需要复制粘贴更新区域,因为它有类似的代码。

class AddAreaItem extends StatefulWidget {
  const AddAreaItem({super.key, this.isUpdate = false});
  final bool isUpdate;

  @override
  State<AddAreaItem> createState() => _AddAreaItemState();
}

class _AddAreaItemState extends State<AddAreaItem> {
  //--------- selectedCategory_1 variable
  String selectedCategoryArea = '';

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 366,
      width: 514,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          SizedBox(
            height: 25,
          ),
          SizedBox(
            width: 434,
            height: 42,
            child: Wrap(
              alignment: WrapAlignment.spaceBetween,
              children: [
                Text(
                  widget.isUpdate ? 'Add Area' : 'Update Area',
                  style: heading2(
                    color: ColorName.blackPrimary,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(top: 10),
                  child: InkWell(
                    child: SvgPicture.asset(
                      Assets.icons.closeIcon.path,
                      width: 16,
                      height: 16,
                    ),
                    onTap: () => {
                      Navigator.pop(context),
                    },
                  ),
                )
              ],
            ),
          ),

          //----------- TextField Code Area dan Category
          SizedBox(
            width: 435,
            child: Wrap(
              alignment: WrapAlignment.spaceAround,
              children: [
                Wrap(
                  direction: Axis.vertical,
                  children: [
                    Text(
                      'Area Code',
                      style: body1(
                        color: ColorName.blackPrimary,
                      ),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    SizedBox(
                      width: 126,
                      height: 60,
                      child: TextField(
                        style: body1(color: ColorName.blackPrimary),
                        cursorColor: ColorName.blackPrimary,
                        decoration: InputDecoration(
                          hintText: 'Area Code',
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.circular(10),
                          ),
                          focusedBorder: const OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(10)),
                            borderSide: BorderSide(
                              color: ColorName.grey,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
                Wrap(
                  direction: Axis.vertical,
                  children: [
                    Text(
                      'Category Area',
                      style: body1(
                        color: ColorName.blackPrimary,
                      ),
                    ),
                    const SizedBox(
                      height: 10,
                    ),
                    Container(
                      width: 288,
                      height: 56,
                      decoration: ShapeDecoration(
                        shape: RoundedRectangleBorder(
                          side: BorderSide(
                              style: BorderStyle.solid, color: ColorName.grey),
                          borderRadius: BorderRadius.all(
                            Radius.circular(10),
                          ),
                        ),
                      ),
                      child: DropdownButton<String>(
                        icon: Padding(
                          padding: const EdgeInsets.only(right: 10, top: 8),
                          child: SvgPicture.asset(
                            Assets.icons.dropdownIcon.path,
                            fit: BoxFit.scaleDown,
                          ),
                        ),
                        style: body1(color: ColorName.blackPrimary),
                        items: <String>[
                          'Block',
                          'Fining Line',
                        ].map((String value) {
                          return DropdownMenuItem(
                            value: value,
                            child: Text(value),
                          );
                        }).toList(),
                        hint: Padding(
                          padding: const EdgeInsets.only(top: 8, left: 10),
                          child: Text(
                              style: body1(color: ColorName.grey),
                              selectedCategoryArea.isEmpty
                                  ? 'Category Area'
                                  : selectedCategoryArea),
                        ),
                        borderRadius: BorderRadius.circular(10),
                        underline: const SizedBox(),
                        isExpanded: true,
                        onChanged: (value) {
                          if (value != null) {
                            setState(() {
                              selectedCategoryArea = value;
                            });
                          }
                        },
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),

          //----------- Tombol Add Area dan Cancel
          SizedBox(
            width: 425,
            child: Wrap(
              alignment: WrapAlignment.spaceBetween,
              children: [
                SizedBox(
                  width: 183,
                  height: 60,
                  child: OutlinedButton(
                    style: ButtonStyle(
                      shape: MaterialStateProperty.all(
                        RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10),
                        ),
                      ),
                      side: MaterialStateProperty.all(
                        const BorderSide(
                          color: ColorName.darkBlue,
                        ),
                      ),
                    ),
                    child: Text(
                      'Cancel',
                      style: subtitle1(
                        color: ColorName.darkBlue,
                      ),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ),

                //------------- Tombol add area
                SizedBox(
                  width: 183,
                  height: 60,
                  child: OutlinedButton(
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(
                        ColorName.darkBlue,
                      ),
                      shape: MaterialStateProperty.all(
                        RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10),
                        ),
                      ),
                      side: MaterialStateProperty.all(
                        const BorderSide(
                          color: ColorName.darkBlue,
                        ),
                      ),
                    ),
                    child: Text(
                      widget.isUpdate ? 'Add Area' : 'Update Area',
                      style: subtitle1(
                        color: ColorName.white,
                      ),
                    ),
                    onPressed: () {
                      if (widget.isUpdate) {
                        AddAreaSuccess().showCustomDialog(context);
                      } else {
                        UpdateAreaSuccess().showCustomDialog(context);
                      }
                    },
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(
            height: 25,
          ),
        ],
      ),
    );
  }
}
jgwigjjp

jgwigjjp1#

首先,你需要定义你从哪里知道屏幕是更新屏幕还是添加屏幕。

class AddAreaItem extends StatefulWidget {
  final bool isUpdate;
  const AddAreaItem({super.key, this.isUpdate=false});//IT MEANS THAT isUpdate is FALSE by default, but you can define it while creating AddAreaItem
  ...
}

然后在你的小部件中,你可以这样做:

Text(
  widget.isUpdate?'Update Area':'Add Area',
  //if it is inside Stateless widget then you use isUpdate? instead of widget.isUpdate
  style: subtitle1(
  color: ColorName.white,
  ),
),

在像onPressed这样的函数中,你可以这样使用:

onPressed: (){
   if(widget.isUpdating){
     AddAreaSuccess().showCustomDialog(context);
   }else{...}
}
uinbv5nw

uinbv5nw2#

你可以在呈现这个小部件时在属性中传递这两个值。然后检查属性是否为空,那么你正在尝试添加新数据。但是如果那些属性包含值,那么它正在更新数据。
就像这样

class AreaWidget extends StatefulWidget {
  const AreaWidget({super.key, this.areaCode, this.areaName});
  final int? areaCode;
  final String? areaName;

  @override
  State<AreaWidget> createState() => _AreaWidgetState();
}

class _AreaWidgetState extends State<AreaWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(widget.areaCode == null ? "Add Area" : "Upate Area"),
    );
  }
}

然后可以检查值并根据值进行更改。甚至在函数内部也可以调用特定的添加或更新相关代码。

相关问题