2

I'm trying to create a custom Alert dialogue using this package rflutter_alert . But when return the Alert it gives me this error

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

Update:

here i created a custom widget of dialogue


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
  }

and here is my other file where i'm creating a button


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


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

and updateProduct(); on this mehtod calling the custom class dialogue, but it's not showing , i want to do this something in this way.

please help how to do this.

TimeToCode
  • 1,458
  • 3
  • 18
  • 60

2 Answers2

1

you missing one closing ) bracket after ).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
  }

Complete src code:

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
}
Jahidul Islam
  • 11,435
  • 3
  • 17
  • 38
  • error still occur! – TimeToCode Nov 16 '21 at 07:07
  • also you can try to remove the `Container`, does it return same issue – Jahidul Islam Nov 16 '21 at 07:08
  • it gives me this error then ```A value of type 'Future' can't be returned from the function '_onAlertButtonsPressed' because it has a return type of 'Widget'``` – TimeToCode Nov 16 '21 at 07:10
  • remove the `Widget` keyword infront of `_onAlertButtonsPressed ` and updated my answer please have a look it worked my machine – Jahidul Islam Nov 16 '21 at 07:11
  • it works, but how can i access it in others files, i mean to say should create stateless widget and then passing this method? because i can't access ```_onAlertButtonsPressed``` it in other files. – TimeToCode Nov 16 '21 at 07:18
  • `InkWell(onTap: (){ _onAlertButtonsPressed(context,"test","title",(){},(){}); }, child: Text("test")),` Call from other files and added complete code and if you call it other files you need to make it public by removing the under score like `onAlertButtonsPressed` – Jahidul Islam Nov 16 '21 at 07:19
  • please check my updated code, i want to do something like that. – TimeToCode Nov 16 '21 at 07:44
  • call the `DialogueTwoButton` from onTap function like `InkWell(onTap:(){DialogueTwoButton(// with properties) }) – Jahidul Islam Nov 16 '21 at 07:50
  • not working!! I did that on ```DialogueTwoButton``` on this too. – TimeToCode Nov 16 '21 at 10:04
  • removing the underscore from ```_onAlertButtonsPressed``` works! – TimeToCode Nov 16 '21 at 10:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239269/discussion-between-jahidul-islam-and-timetocode). – Jahidul Islam Nov 16 '21 at 10:18
1

Try below code hope its helpful to you. remove Container and Widget

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();
  }
Ravindra S. Patil
  • 11,757
  • 3
  • 13
  • 40