如何 return 在 flutter 的 widget 中提醒

How to return Alert in widget in flutter

我正在尝试使用此包 rflutter_alert 创建自定义警报对话框。但是当 return 警报时,它给了我这个错误

The argument type 'Future<bool?>' can't be assigned to the parameter type 'Widget?'.

更新:

我在这里创建了一个自定义对话小部件


class DialogueTwoButton extends StatelessWidget {
  DialogueTwoButton(
      {Key? key,
      context,
      required this.text1,
      required this.text2,
      required this.onpres1,
      required this.onpress2})
      : super(key: key);
  final String text1;
  final String text2;
  final Function onpres1;
  final Function onpress2;

  @override
  Widget build(BuildContext context) {
    return _onAlertButtonsPressed(context, text1, text2, onpres1, onpress2);
  }

  var alertStyle = AlertStyle(
    animationType: AnimationType.fromTop,
    isCloseButton: false,
    isOverlayTapDismiss: false,
    descStyle: GoogleFonts.montserrat(color: Colors.black, fontSize: 18),
    titleStyle: GoogleFonts.montserrat(
      color: Colors.red,
    ),
  );

  _onAlertButtonsPressed(context, desc, title, onPressYes, onPressNo) {
    return Alert(
      context: context,
      style: alertStyle,
      title: title,
      desc: desc,
      buttons: [
        DialogButton(
            child: Text(
              "Yes",
              style: GoogleFonts.montserrat(color: Colors.white, fontSize: 18),
            ),
            onPressed: onPressYes,
            color: HexColor("#5344ed")),
        DialogButton(
          child: Text(
            "No",
            style: GoogleFonts.montserrat(color: Colors.white, fontSize: 18),
          ),
          onPressed: onPressNo,
          color: HexColor("#5344ed"),
        )
      ],
    ).show(); // here need to change
  }

这是我创建按钮的另一个文件


 updateProduct() {
    DialogueTwoButton(
      onpres1: () {},
      onpress2: () {},
      text1: 'df',
      text2: 'dsf',
    );


 bottomButton(context, () {
            updateProduct();
          }, "Update Product"),

updateProduct(); 在此方法上调用自定义 class 对话,但未显示,我想以这种方式执行此操作。

请帮助如何做到这一点。

您在 ).show()

之后缺少一个 ) 右括号
_onAlertButtonsPressed(context,desc,title,onPressYes,onPressNo) {
   return Alert(
      context: context,
      style: alertStyle,
      title: title,
      desc: desc,
      buttons: [
        DialogButton(
            child: Text(
              "Yes",
              style: GoogleFonts.montserrat(color: Colors.white, fontSize: 18),
            ),
            onPressed: onPressYes,
            color: HexColor("#5344ed")),
        DialogButton(
            child: Text(
              "No",
              style: GoogleFonts.montserrat(color: Colors.white, fontSize: 18),
            ),
            onPressed: onPressNo,
            color: HexColor("#5344ed"),
            )
      ],
    ).show(); // here need to change
  }

完整的源代码:

import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyHomePage(),
        ),
      ),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> {

TextEditingController _textEditingController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("title"),
      ),
      body: Column(
        children: [
          InkWell(onTap: (){
            _onAlertButtonsPressed(context,"test","title",(){},(){});
          }, child: Text("test")),
         
        ],
      ),
    );
  }
}


 _onAlertButtonsPressed(context,String desc,String title,onPressYes,onPressNo) {
  return Alert(
    context: context,
    //style: alertStyle,
    title: title,
    desc: desc,
    buttons: [
      DialogButton(
          child: Text(
            "Yes",
            //style: GoogleFonts.montserrat(color: Colors.white, fontSize: 18),
          ),
          onPressed: onPressYes,
          //color: HexColor("#5344ed")
           ),
      DialogButton(
        child: Text(
          "No",
         // style: GoogleFonts.montserrat(color: Colors.white, fontSize: 18),
        ),
        onPressed: onPressNo,
       // color: HexColor("#5344ed"),
      )
    ],
  ).show(); // here need to change
}

试试下面的代码希望对你有帮助。删除 ContainerWidget

onAlertButtonsPressed(context, desc, title, onPressYes, onPressNo) {
    return Alert(
      context: context,
      style: alertStyle,
      title: title,
      desc: desc,
      buttons: [
        DialogButton(
          child: Text(
            "Yes",
          ),
          onPressed: onPressYes,
        ),
        DialogButton(
          child: Text(
            "No",
          ),
          onPressed: onPressNo,
        )
      ],
    ).show();
  }