'Idiomatic way to change aspects of the active scaffold from within Flutter TabView child

Essentially, I'm trying to devise a way for a child of a TabView to customise aspects of the active Scaffold instance, such as the floating action button, application bar and so on.

Using the code snippet below, the idea is for_ATabState to set the floatingActionButton of the Scaffold instance to a custom widget that it controls.

class MasterWidget extends StatefulWidget {
  @override
  _MasterWidgetState createState() => _MasterWidgetState();

  Widget? setFloatingActionButton() {
    // how to access state and invoke `setState`?
  }
}

class _MasterWidgetState extends State<MasterWidget> {
  @override
  Widget build(BuildContext context) {
    final tabs = [ATab()];

    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: activeTab.buildFloatingActionButton(),
      body: DefaultTabController(
        length: tabs.length,
        child: TabBarView(
          children: tabs,
          controller: tabController,
        ),
      ),
    );
  }
}

class ATab extends StatefulWidget {
  @override
  _ATabState createState() => _ATabState();
}

class _ATabState extends State<ATab> {
  @override
  Widget build(BuildContext context) {
    // Doesn't work:
    // context.findAncestorWidgetOfExactType<Scaffold>()?.floatingActionButton = AFloatingActionButton();
    // context.findAncestorWidgetOfExactType<MasterWidget>()?.setFloatingActionButton(AFloatingActionButton());

    return SomeWidget();
  }
}

Here's what I tried:

  1. Try to context.findAncestorWidgetOfExactType<Scaffold>() in _ATabState and somehow set Scaffold's floatingActionButton attribute; unfortunately there does not seem to be a setter available.

  2. Try to context.findAncestorWidgetOfExactType<MasterWidget>() in _ATabState but then I'm not able to access the state where the rendering takes place.

What's the approach applicable here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source