2017-08-02 44 views
0

我有以下代碼:如何從自己的回調(onPressed)內的飛鏢/撲訪問了Widget

@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(),我想改變按鈕的風格 - 代表灑水活動。

因此,我需要訪問MaterialButton Widget本身。

但是,如何從回調中訪問它?

非常感謝提前,並抱歉的n00b問題,我是新來的DART和顫振;)

回答

1

感謝您的意見。正確的解決方法是actualy你推薦什麼,看起來像這樣:

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; 
      }); 
     }, 
    ), 
    ); 
    } 
} 
1

你可能想使用StatefulWidget,是這樣的:

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; 
    }), 
); 
} 
2

你可以使某些屬性成爲一個變量。然後,您可以在onPressed()中撥打setState()更改屬性變量。
這個例子說明如何使用這種方法來改變按鈕的文本顏色:

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; 
      }) 
     }, 
    ), 
    ); 
    } 
+0

我得到以下錯誤: '[鏢該方法「的setState」不爲類「定義_SprinklerListItem'.' – wzr1337

+0

這是一個'StatefulWidget'? –