1

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

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(); 在这个调用自定义类对话的方法上,但它没有显示,我想以这种方式做这件事。

请帮助如何做到这一点。

4

2 回答 2

1

您在之后缺少一个右)括号).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
  }

完整的src代码:

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
}
于 2021-11-16T07:03:34.787 回答
1

试试下面的代码希望它对你有帮助。删除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();
  }
于 2021-11-16T07:15:02.137 回答