Home > Back-end >  why do i get Undefined name 'context'. Try correcting the name to one that is defined, or
why do i get Undefined name 'context'. Try correcting the name to one that is defined, or

Time:05-25

I'm trying to get a bottomModalSheet to pop up when I tap on an IconButton , but I keep on getting 'undefined name 'context' when I run my code

 Widget mealSize() {
  return Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: [
      const Text(
        " size",
        style: TextStyle(
          fontWeight: FontWeight.bold,
          fontSize: 16,
        ),
      ),
      IconButton(
        icon: const Icon(
          Icons.arrow_drop_down_circle,
        ),
        iconSize: 20,
        onPressed: () {
          showModalBottomSheet(
            context: context,
            builder: (BuildContext context) {
              return buildSheet();
            },
          );
        },
      ),
    ],
  );
}

Widget buildSheet() => Column(
      mainAxisSize: MainAxisSize.min,
      children: const [Icon(Icons.add_circle), Text("Add Size variant")],
    );

CodePudding user response:

Because your context is missing here:

          showModalBottomSheet(
            context: context,
            builder: (BuildContext context) {
              return buildSheet();
            },
          );

It's not defined as the error says.

CodePudding user response:

Look at where you're defining mealsize(). There's no context being passed in, so where is the showModalBottomSheet going to get the context from? You have to pass one in to mealsize()

Widget mealsize({required BuildContext context}){
  // Do stuff here.
}
  • Related