Home > other >  I need help for Function "press" in Flutter app
I need help for Function "press" in Flutter app

Time:04-29

1.I extract to widget and write final Function press then i write onTap: press and i have mistake. How to solve this problem? 2.

class CharactersCards extends StatelessWidget {
  const CharactersCards({
    Key? key,
    required this.image,
    required this.title,
    required this.element,
    required this.press,
  }) : super(key: key);

final String image, title, element;
final Function press;

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      margin: EdgeInsets.only(
        left:  kDefaultPadding,
        top:  kDefaultPadding / 2,
        bottom: kDefaultPadding * 2.5,
      ),
      width: size.width * 0.4,
      child: Column(
        children: <Widget> [
          Image.asset(image),
          GestureDetector(
            onTap: press,
            child: Container(
              padding: EdgeInsets.all(kDefaultPadding / 2),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(10),
                  bottomRight: Radius.circular(10),

                ),

I wrote press: () {},

CodePudding user response:

option 1:

you can directly write your functionality inside ontap function like below

onTap: () {
//here you can write your functionality
},

option 2 :

you can wrote a simple function and call it inside ontap property like below

onTap: () {
yourFunctionName(),
}

void yourFunctionName() {
//write your functionality here
};

CodePudding user response:

If I'm getting your problem correcly, wrap your Container with InkWell or GestureDetector on call you press inside onTap.

GestureDetector(
    child: Container(
      margin: EdgeInsets.only(
        left:  kDefaultPadding,
        top:  kDefaultPadding / 2,
        bottom: kDefaultPadding * 2.5,
      ),
      width: size.width * 0.4,
      child: Column(
        children: <Widget> [
          Image.asset(image),
          GestureDetector(
            onTap: press,
            child: Container(
              padding: EdgeInsets.all(kDefaultPadding / 2),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                  bottomLeft: Radius.circular(10),
                  bottomRight: Radius.circular(10),

                ),

    onTap:press
)

CodePudding user response:

you should use this

final Function() press;

or use VoidCallback instead of Function() while declaring like this:

final VoidCallback press;
  • Related