ronjb / selectable

A Flutter widget that enables text selection over all the text widgets it contains
MIT License
18 stars 13 forks source link

selectionChangedListener() not getting called #16

Closed dinasorz closed 1 year ago

dinasorz commented 1 year ago

_selectionChangedListener() not getting called despite changing the selected text?

class _MyHomePageState extends State<MyHomePage> {
  final _selectionController = SelectableController();

  void _selectionChangedListener() {
    print('Changed 1');  //Not called
    setState(() {
      print('Changed 2'); //Not called
    });
  }

  @override
  void initState() {
    super.initState();
    if (kIsWeb) {
      // On web, disable the browser's context menu since this example uses a custom context menu
      BrowserContextMenu.disableContextMenu();
    }
    _selectionController.addListener(_selectionChangedListener);
  }

  @override
  void dispose() {
    if (kIsWeb) {
      BrowserContextMenu.enableContextMenu();
    }
    _selectionController
      ..removeListener(_selectionChangedListener)
      ..dispose();
    super.dispose();
  }

  // The rest of the code...
}
dinasorz commented 1 year ago

I apologize I found the answer just after posting the issue (like always :") )

For anyone stuck at this too, It turns out that we must set selectionController to Selectable widget too

return Selectable(
selectionController: _selectionController,
...
)