Home > Software design >  Is it possible to override what a core flutter Scaffold widget does?
Is it possible to override what a core flutter Scaffold widget does?

Time:06-07

A standard flutter Scaffold is built with the following class:

class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, RestorationMixin {
   ... 

And inside this ScaffoldState is a function that deals with tapping a status bar tap

  void _handleStatusBarTap() {
    final ScrollController? primaryScrollController = PrimaryScrollController.of(context);
    if (primaryScrollController != null && primaryScrollController.hasClients) {
      primaryScrollController.animateTo(
        0.0,
        duration: const Duration(milliseconds: 1000),
        curve: Curves.easeOutCirc,
      );
    }
  }

When implementing a Scaffold in my own flutter project however, I don't want the _handleStatusBarTap to fire or function the way stock flutter has it going.

Is there a way I can on the fly have my widget modify that widget to do for example the following:

  void _handleStatusBarTap() {
      debugPrint('Now I control the statusbartap!');
  }

I don't want to modify the core SDK files, which is why I was hoping there is a way to override the default functioning of a widget?

CodePudding user response:

hope you're doing well. I was trying to do what you're looking for and sadly I should say that is not possible to override that function _handleStatusBarTap() because it is a private function, but if you actually need to change it so bad what you can do is to create a full copy of the scaffold (which I don't recommend) yow will need to copy the ScaffoldState widget and create a public interface to the function or just delete it from the existence and override it yourself, then just create a copy of the Scaffold widget and assign the new state, like this

class CustomScaffold extends Scaffold {

 const CustomScaffold({Key? key}) : super(key: key);

  @override
  CustomScaffoldState createState() => CustomScaffoldState();
}

class CustomScaffoldState extends State<CustomScaffold> {
  // ...all the original ScaffoldState code

  // Here you go boy, this is your custom method
  _handleStatusBarTap() {}
}
  • Related