dart 函数在ElevatedButton中不执行onPressed

dpiehjr4  于 2023-10-13  发布在  其他
关注(0)|答案(3)|浏览(130)

我有一个可重用的按钮小部件,当我传入ElevatedButton的onPressed函数时,它不会被执行。
注意,我使用onPressed导航的其他地方也可以工作
我使用onPressed和工作的地方

BlackButton(
          text: 'Create offer',
          onPressed: () => Navigator.pushNamed(context, 'createOfferFighter'), //this works
        ),

Place where I execute a function -此小部件位于有状态父小部件中

BlackRoundedButton(
                    isLoading: false 
                    text: submitButton,
                    onPressed: () =>
                        createOffer() //doesn't print anything
                  ),

函数的作用是:

void createOffer() {
    print('casfas');
  }

可重复使用的按钮小部件:

class BlackButton extends StatelessWidget {
  final Function onPressed;

  final String text;

  const BlackButton({Key? key, required this.onPressed, required this.text})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 230,
      height: 60,
      child: Container(
        decoration: BoxDecoration(boxShadow: [containerShadowRed]),
        child: ElevatedButton(
            style: ButtonStyle(
              backgroundColor: const MaterialStatePropertyAll(Colors.black),
              shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                const RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
              ),
            ),
            onPressed: () => onPressed(),
            child: Text(
              text,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
            )),
      ),
    );
  }
}
z31licg0

z31licg01#

试试这个代码

import 'package:flutter/material.dart';

class BlackButton extends StatelessWidget {
  final VoidCallback onPressed;

  final String text;

  const BlackButton({Key? key, required this.onPressed, required this.text}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 230,
      height: 60,
      child: Container(
        decoration: BoxDecoration(boxShadow: [containerShadowRed]),
        child: ElevatedButton(
            style: ButtonStyle(
              backgroundColor: const MaterialStatePropertyAll(Colors.black),
              shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                const RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
              ),
            ),
            onPressed: onPressed,
            child: Text(
              text,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
            )),
      ),
    );
  }
}
aor9mmx1

aor9mmx12#

这应该打印出一个新的Offer():

import 'package:flutter/material.dart';

class BlackButton extends StatelessWidget {
  final Function() onPressed;

  final String text;

  const BlackButton({Key? key, required this.onPressed, required this.text})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 230,
      height: 60,
      child: Container(
        decoration: const BoxDecoration(boxShadow: []),
        child: ElevatedButton(
            style: ButtonStyle(
              backgroundColor: const MaterialStatePropertyAll(Colors.black),
              shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                const RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
              ),
            ),
            onPressed: onPressed,
            child: Text(
              text,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
            )),
      ),
    );
  }
}
eoigrqb6

eoigrqb63#

我缺少了一对(),我有我的自定义按钮小部件

Widget build(BuildContext context) {
    return SizedBox(
      width: 230,
      height: 60,
      child: Container(
        decoration: BoxDecoration(boxShadow: [containerShadowRed]),
        child: ElevatedButton(
            style: ButtonStyle(
              backgroundColor: const MaterialStatePropertyAll(Colors.black),
              shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                const RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
              ),
            ),
            onPressed: () => onPressed(), //() were missing next to onPressed
            child: Text(
              text,
              style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
            )),
      ),
    );
  }
}

相关问题