matatoMatata94
matatoMatata94

Reputation: 107

Extracted Button into own class as stateless Widget, now AlertDialog() doesn't show up anymore

I have extracted a button as a stateless widget (AddToFavoritesButton). With tap on that button I want an AlertDialog to show up. After Extraction it doesn't work anymore. No Error Message.

I think I'm using the showAddFavoritePopup the wrong way, because there is no error and that might be a sign that the AlertDialog pops up but not in my view, if that makes sense :D

This is inside the Page where the Button shows:

AddToFavoritesButton(
  titleController: widget.titleController,
  inputNumber: inputNumber,
  context: context,
  showAddFavoritePopup: () {
    showAddFavoritePopup(context, widget.titleController);
  },
),

This is the AlertDialog:

void showAddFavoritePopup(
      BuildContext context, TextEditingController titleController) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text("Favorit hinzufügen"),
          content: SingleChildScrollView(
            child: Column(
              children: [
                TextField(
                  controller: titleController,
                  decoration: const InputDecoration(labelText: "Titel"),
                ),
                // SizedBox(height: 10),
                Text("Nummer: $inputNumber"),
              ],
            ),
          ),
          actions: <Widget>[
            TextButton(
              child: const Text("Abbrechen"),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text("Hinzufügen"),
              onPressed: () {
                widget.controller
                    .addFavorite(titleController.text, inputNumber);
                Navigator.of(context).pop();
              },
            ),
          ],
        );
      },
    );
  }

And this is my extracted Widget:

class AddToFavoritesButton extends StatelessWidget {
  final TextEditingController titleController;
  final String inputNumber;
  final VoidCallback showAddFavoritePopup;
  final BuildContext context;

  const AddToFavoritesButton({
    required this.titleController,
    required this.inputNumber,
    required this.showAddFavoritePopup,
    required this.context,
  });

  @override
  Widget build(BuildContext context) {
    return ElevatedButton.icon(
      onPressed: () {
        showAddFavoritePopup;
      },
      icon: const Icon(Icons.favorite),
      label: const Text('Favorit hinzufügen'),
      style: ElevatedButton.styleFrom(
        foregroundColor: Colors.white,
        backgroundColor: Colors.blue,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(8.0),
        ),
      ),
    );
  }
}

Upvotes: 0

Views: 32

Answers (1)

Cabdirashiid
Cabdirashiid

Reputation: 1587

Round brackets () call the function

Do:

onPressed: () {
  showAddFavoritePopup();
},

Upvotes: 1

Related Questions