prompt static method

Future<DialogResult?> prompt(
  1. BuildContext context, {
  2. required String title,
  3. String? label,
  4. TextField? textField,
  5. String leftButton = "Cancel",
  6. String rightButton = "OK",
  7. VoidCallback? onTapedLeft,
  8. VoidCallback? onTapedRight,
  9. bool barrierDismissible = false,
  10. bool? dark,
})

输入条件的回调对话框

  • title 对话框标题
  • label 输入框Label
  • textField 自定义输入框
  • leftButton 左边按钮名称
  • rightButton 右边按钮名称
  • onTapedLeft 左边按钮点击事件,自定义处理。需注意返回的类型DialogResult
  • onTapedRight 右边按钮点击事件,自定义处理。需注意返回的类型DialogResult
  • barrierDismissible 对话框空白区域点击关闭是否支持,默认支持点击空白区域可以关闭对话框。
  • dark 是否强制使用黑夜模式。

Implementation

static Future<DialogResult?> prompt(
  BuildContext context, {
  required String title,
  String? label,
  TextField? textField,
  String leftButton = "Cancel",
  String rightButton = "OK",
  VoidCallback? onTapedLeft,
  VoidCallback? onTapedRight,
  bool barrierDismissible = false,
  bool? dark,
}) {
  bool _dark = dark ?? (Theme.of(context).brightness == Brightness.dark);
  Color _barrierColor = _dark ? Color(0xaa000000) : Color(0x9effffff);
  Color _backgroundColor = _dark ? Colors.black45 : Colors.white60;
  Color _fontColor = _dark ? Colors.white : Colors.black;
  return showDialog<DialogResult>(
    context: context,
    barrierColor: _barrierColor,
    barrierDismissible: barrierDismissible,
    builder: (BuildContext context) {
      print("Dialog build....");
      final TextEditingController editingController = TextEditingController();
      return Dialog(
        backgroundColor: _backgroundColor,
        shape: const RoundedRectangleBorder(borderRadius: _borderRadius),
        child: TernaryContainer(
          header: Container(
            alignment: Alignment.center,
            padding: const EdgeInsets.only(
                top: 25.0, bottom: 10.0, left: 20.0, right: 20.0),
            child: Text(
              title,
              style: TextStyle(
                fontSize: 18.0,
                shadows: kElevationToShadow[4],
                color: _fontColor,
              ),
            ),
          ),
          content: Padding(
            padding:
                const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
            child: CustomTextInput(
              label: label,
              style: TextStyle(
                color: _fontColor,
              ),
              labelStyle: TextStyle(
                color: _fontColor,
              ),
              editingController: editingController,
            ),
          ),
          footer: Column(
            children: [
              Divider(
                color: Colors.grey,
                height: 2,
              ),
              Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      child: Container(
                        alignment: Alignment.center,
                        child: TextButton(
                          onPressed: onTapedLeft ??
                              () {
                                Navigator.of(context).pop(
                                    DialogResult(status: false, code: 200));
                              },
                          child: Text(
                            leftButton,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                fontSize: 16.0,
                                color: Theme.of(context).primaryColor),
                          ),
                        ),
                      ),
                    ),
                    Container(
                        height: 48,
                        child: const VerticalDivider(
                          color: Colors.grey,
                          width: 2,
                        )),
                    Expanded(
                      child: Container(
                        alignment: Alignment.center,
                        child: TextButton(
                          onPressed: onTapedRight ??
                              () {
                                if (editingController.text.isEmpty) {
                                  // ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("右边的按钮被按到了")));
                                  return;
                                }
                                Navigator.of(context).pop(DialogResult(
                                    status: true,
                                    code: 200,
                                    msg: "返回输入内容",
                                    data: editingController.text));
                              },
                          child: Text(
                            rightButton,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                fontSize: 16.0,
                                color: Theme.of(context).primaryColor),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              // SizedBox(height: 10.0,),
            ],
          ),
        ),
      );
    },
  );
}