79252082

Date: 2024-12-04 17:06:47
Score: 0.5
Natty:
Report link

@Ivo's comment, in my question, provided me with a solution in which:

  1. the user-provided callback takes a context parameter, e.g. void myOnTap(BuildContext context){ ... }

  2. You do not pass directly this callback to each basic Widget's onTap property but you provide it like this: onTap: (){ myOnTap(context); } (and context is now available as we are inside a build() method).

For example,

// untested pseudocode
class MyComplexWidget extends StatelessWidget {
  final void Function(BuildContext)? onTapForA;
  final void Function(BuildContext)? onTapForB;

  const MyComplexWidget({super.key, this.onTapForA, this.onTapForB});

  @override
  build(BuildContext context) {
    return Column(children: [
      InkWell( // basic widget A
        ...
        onTap: () {
          onTapForA!(context);
        },
      ),
      InkWell( // basic widget B
        ...
        onTap: () {
          onTapForB!(context);
        },
      )
    ]);
  } // build
}

Thank you for your help

Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Whitelisted phrase (-0.5): Thank you for your help
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Ivo's
  • Self-answer (0.5):
Posted by: bliako