How to update an already working widget from the outside

I have a menu widget where I select themes, BottomBar, etc. It also has an AppBar, in which I placed the search action and made it active only for a specific widget. The search data is in the TextEditingController. I want to add such a listener from another widget that would receive data about the listener change.

final TextEditingController _filter = TextEditingController();
Widget get searchWidget => IconButton(onPressed: (){

setState(() {
  if(toggle) {
    searchBar = ListTile(
        leading: /**/,
        title: TextField(
          controller: _filter,
          /**/),
          style: /**/,
        ),
      ),
    );
  } else {
    searchBar = title;
    _filter.clear();
  }
    toggle = !toggle;
});
},
  icon: toggle ? const Icon(Icons.search) : const Icon(Icons.cancel),
);

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    actions: [
      _pages.elementAt(_selectedIndex) is Prices ? searchWidget : Container(),
    ],

      title: searchBar
  ),
  body: /**/,

  bottomNavigationBar: /**/,
  ),
);

@override
void initState()
{
  super.initState();
  _filter.addListener(() {
    (prices as Prices).filterShares(_filter.text);
  });
}

bool toggle = true;

I understand that the StatefulWidget is constantly updated and at runtime you cannot transfer something to it. I just don't understand how I can transfer the value from the search to the state of the Price widget down.

class Prices extends StatefulWidget {
Prices({Key? key}) : super(key: key);

void filterShares(String text){
  for (var element in _filterListeners) {
    element(text);
  }
}

final List<Function(String str)> _filterListeners = []; // I understand that here the list is reset to zero when redrawing

void _addListener(Function(String str) listener){
  _filterListeners.add(listener);
}

@override
State<Prices> createState() => _PricesState();
}

How to properly pass data to the widget when it is already displayed?

Upvotes: 0

Views: 233

Answers (0)

Related Questions