2

Am trying to change the color of my container using the setState Method in onPress but the app crashes with an error of setState isnt defined.And i want to ask for help.Any help provider will be appreciated.Thank you

import 'package:flutter/material.dart';
import 'AllContainers.dart';
import 'ColumnContainer.dart';
import 'AllConstants.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

enum AgeStatus { child, old }

class MyHomePage extends StatefulWidget {
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  AgeStatus? ageStatus;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Practising_Cards'),
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: Row(
              children: [
                Expanded(
                  child: AllContainers(
                    onPress: () {
                      setState(() {
                        ageStatus = AgeStatus.child;
                      });
                    },
                    colors: ageStatus == AgeStatus.child
                        ? activeColor
                        : deactiveColor,
                    mycard: MyColumnItems(FontAwesomeIcons.mars, 'FEMALE'),
                  ),
                ),
                Expanded(
                  child: AllContainers(
                    colors: ageStatus == deactiveColor
                        ? activeColor
                        : deactiveColor,
                    onPress: () {
                      setState(() {
                        ageStatus = AgeStatus.old;
                      });
                    },
                    mycard: MyColumnItems(FontAwesomeIcons.mars, 'FEMALE'),
                  ),
                )
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: 5),
            width: double.infinity,
            height: 50,
            color: Colors.red,
          ),
        ],
      ),
    );
  }
}

this is the Container class

import 'package:flutter/material.dart';
import 'NewMain.dart';

class AllContainers extends StatelessWidget {
  final Color colors;
  final Widget mycard;
  final Function onPress;

  AllContainers(
      {required this.colors, required this.mycard, required this.onPress});

  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPress(),
      child: Container(
        child: mycard,
        margin: EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: colors,
          borderRadius: BorderRadius.circular(20),
        ),
      ),
    );
  }
}

I tried creating a function with the setState in it in the State and passed the function to my onPress method and that also didnt work.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • Perhaps [The argument type 'Function' can't be assigned to the parameter type 'void Function()](https://stackoverflow.com/q/64484113/10157127) – Md. Yeasin Sheikh Jan 14 '23 at 02:04

2 Answers2

0

AllContainers' property onTap has type Function.

But I think your intention is VoidCallback type.

So, you have to change the type of onTap property from Function to VoidCallback

And you have to pass not onPress() but onPress.

class AllContainers extends StatelessWidget {
  final Color colors;
  final Widget mycard;

  // final Function onPress;
  final VoidCallback onPress;

  AllContainers({
    required this.colors,
    required this.mycard,
    required this.onPress,
  });

  Widget build(BuildContext context) {
    return GestureDetector(
      // onTap: onPress(),
      onTap: onPress,
      child: Container(
        child: mycard,
        margin: EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: colors,
          borderRadius: BorderRadius.circular(20),
        ),
      ),
    );
  }
}
debug_ing
  • 476
  • 3
  • 13
0

You should use final VoidCallback onPress inside your AllContainers instead of final Function onPress this is the class you should use to solve your problem.

class AllContainers extends StatelessWidget {
  final Color colors;
  final Widget myCard;
  final VoidCallback onPress;

  const AllContainers(
      {super.key,
      required this.colors,
      required this.myCard,
      required this.onPress});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPress,
      child: Container(
        margin: const EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: colors,
          borderRadius: BorderRadius.circular(20),
        ),
        child: myCard,
      ),
    );
  }
}

please note that to use annotations and use camelCase in your coding. happy coding...

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Amirali Eric Janani
  • 1,477
  • 3
  • 10
  • 20
  • you're welcome dear Emmanuel, I'm here for explaining your problem without any expectation.we should help each other in programming world not having bad competition. happy coding my friend. – Amirali Eric Janani Jan 13 '23 at 19:31