Home > Software design >  How to return Alert in widget in flutter
How to return Alert in widget in flutter

Time:11-17

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.

CodePudding user response:

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
}

CodePudding user response:

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();
  }
  • Related