Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Widget from within its own callback (onPressed) in Dart/Flutter

Tags:

flutter

dart

I have the following code:

  @override
  Widget build(BuildContext context) {
    return new Container(
      height: 72.0, // in logical pixels
      padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 8.0),
      decoration: new BoxDecoration(color: Colors.white),
      // Row is a horizontal, linear layout.
      child: new MaterialButton(
        child: new Text(
          _sprinkler.name,
          style: new TextStyle(color: Colors.white)
          ),
        splashColor: Colors.blueAccent,
        color: Colors.blue[800],
        onPressed: () {
          print("onTap(): tapped" + _sprinkler.name);
        },
      ),
    );
  }

onPressed(), I want to change the Buttons style - to represent sprinkler activity.

Therefore, I would need to acces the MaterialButton Widget itself.

But how to I access it from within the callback?

Thanks a lot in advance, and sorry for the n00b question, I am new to Dart and Flutter ;)

like image 269
wzr1337 Avatar asked Aug 02 '17 20:08

wzr1337


People also ask

How do I show widgets from another widget in Flutter?

In Flutter, the overlay lets you print visual elements on top of other widgets by inserting them into the overlay's stack. You insert a widget into the overlay using an OverlayEntry and you use Positioned and AnimatedPositioned to choose where the entry is positioned within the overlay.

How do you pass a callback in Flutter?

Implementation Step 1: Add Firebase to Flutter Add Firebase to your Flutter app | Firebase Follow this guide to add Firebase products to a Flutter app. Firebase supports frameworks like Flutter on a best-effort… firebase.google.com Step 2: Add the dependencies Add dependencies to pubspec. yaml file.


3 Answers

You could make some of the properties a variable. Then you can call setState() in your onPressed() to change the property variable.
This example shows how to change your text color of the button by using this method:

  Color textColor = Colors.white;
  @override
  Widget build(BuildContext context) {
    return new Container(
      height: 72.0, // in logical pixels
      padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 8.0),
      decoration: new BoxDecoration(color: Colors.white),
      // Row is a horizontal, linear layout.
      child: new MaterialButton(
        child: new Text(
          _sprinkler.name,
          style: new TextStyle(color: textColor)
          ),
        splashColor: Colors.blueAccent,
        color: Colors.blue[800],
        onPressed: () {
          this.setState(() {
            textColor = Colors.red;
          })
        },
      ),
    );
  }
like image 127
Bram Vanbilsen Avatar answered Nov 15 '22 09:11

Bram Vanbilsen


You probably want to use a StatefulWidget, something like this:

class MyWidget extends StatefulWidget {
  _MyWidgetState createState() => new _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
  Color c = Colors.blue.shade500;

  Widget build() => new MaterialButton(
    color: c,
    onPressed: () => setState(() {
      c = Colors.red.shade500;
    }),
  );
}
like image 41
Zev Avatar answered Nov 15 '22 10:11

Zev


Thanks for your comments. The correct solution is actualy what you recommended and looks like so:

class SprinklerListItem extends StatefulWidget {
  // This class is the configuration for the state. It holds the
  // values (in this nothing) provided by the parent and used by the build
  // method of the State. Fields in a Widget subclass are always marked "final".
  final Sprinkler _sprinkler;
  SprinklerListItem(this._sprinkler);


  @override
  _SprinklerListItemState createState() {
    return new _SprinklerListItemState(this._sprinkler);
  }
}


class _SprinklerListItemState extends State<SprinklerListItem> {
  final Sprinkler _sprinkler;

  _SprinklerListItemState(this._sprinkler);

  Color textColor = Colors.white;
  Color bgColor = Colors.blue[800];
  @override
  Widget build(BuildContext context) {
    return new Container(
      height: 72.0, // in logical pixels
      padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 8.0),
      decoration: new BoxDecoration(color: Colors.white),
      // Row is a horizontal, linear layout.
      child: new MaterialButton(
        child: new Text(
          _sprinkler.name,
          style: new TextStyle(color: textColor)
          ),
        splashColor: Colors.blueAccent,
        color: bgColor,
        onPressed: () {
          this.setState(() {
            textColor = Colors.grey;
            bgColor = Colors.red;
          });
        },
      ),
    );
  }
}
like image 23
wzr1337 Avatar answered Nov 15 '22 10:11

wzr1337