0

I want to use the ´bool DrawMode´ inside my ChangeDrawModeState class.

I need Something like ´GridState.drawMode´ but that does not work(drawMode is defined in GridState).

In the end I need to change the variable drawMode, if the RaisedButton gets Pressed. I'm not sure how to do this, cause the setState isnt working in the ChangeDrawModeState class as well. But isn't there a simple way to build a Button which turns a bool from True to false(or the other way around)?

class ChangeDrawMode extends StatefulWidget{
  @override
  ChangeDrawModeState createState(){
    return new ChangeDrawModeState();
  }
}

class ChangeDrawModeState<ChangeDrawMode>{
  @override
  Widget build(BuildContext context) {
    return new RaisedButton(
      child: new Text('Change Mode'),
      textColor: Colors.white,
      color: GridState.drawMode ? Colors.grey : Colors.blue,//HERE
      onPressed: () =>setState(() => drawMode = !drawMode) //and HERE drawMode does not work
    );
  }
}

class Grid extends StatefulWidget {
  @override
  GridState createState() {
    return new GridState();
  }
}

class GridState extends State<Grid> {
  bool drawMode = false;
  final Set<int> selectedIndexes = Set<int>();
  final key = GlobalKey();
  final Set<_Foo> _trackTaped = Set<_Foo>();

  _detectTapedItem(PointerEvent event) {
    final RenderBox box = key.currentContext.findRenderObject();
    final result = BoxHitTestResult();
    Offset local = box.globalToLocal(event.position);
    if (box.hitTest(result, position: local)) {
      for (final hit in result.path) {
        /// temporary variable so that the [is] allows access of [index]
        final target = hit.target;
        if (target is _Foo /*&&  !_trackTaped.contains(target)*/) {
          _trackTaped.add(target);
          _selectIndex(target.index);
        }
      }
    }
  }

  _selectIndex(int index) {
    setState(
    () {
      if(selectedIndexes.contains(index)&&drawMode==false){       
        selectedIndexes.remove(index);     
      }
      else if(drawMode==true){
        selectedIndexes.add(index);
      }

    });
  }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

You can use InheritedWidget to update data and therefore widget state from any part of the app

Here is an example

Andrii Shpek
  • 167
  • 2
  • 10