Android 颤振-如何在单击时切换升起按钮的颜色?

Android 颤振-如何在单击时切换升起按钮的颜色?,android,button,dart,widget,flutter,Android,Button,Dart,Widget,Flutter,我正在尝试切换一个凸起按钮的颜色。最初,按钮应为蓝色,按下时变为灰色。现在我有一个bool值,名为pressAttention,它被设置为false。我用它来初始设置为false。当按下该按钮时,它将切换pressAttention bool,但该小部件似乎再也不会更新 new RaisedButton( child: new Text("Attention"), textColor: Colors.white,

我正在尝试切换一个凸起按钮的颜色。最初,按钮应为蓝色,按下时变为灰色。现在我有一个bool值,名为pressAttention,它被设置为false。我用它来初始设置为false。当按下该按钮时,它将切换pressAttention bool,但该小部件似乎再也不会更新

new RaisedButton(
                  child: new Text("Attention"),
                  textColor: Colors.white,
                  shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0)),
                  color: pressAttention?Colors.grey:Colors.blue,
                  onPressed: () => doSomething("Attention"),
                )

void doSomething(String buttonName){
if(buttonName == "Attention"){
  if(pressAttention = false){
    pressAttention = true;
  } else {
    pressAttention = false;
  }
}

}

此按钮需要在
状态窗口小部件的
状态的
构建中创建,并且该状态必须有一个成员变量
bool pressAttention=false。正如Edman所建议的,您需要在
setState
回调中更改状态,以便重新绘制小部件

new RaisedButton(
  child: new Text('Attention'),
  textColor: Colors.white,
  shape: new RoundedRectangleBorder(
    borderRadius: new BorderRadius.circular(30.0),
  ),
  color: pressAttention ? Colors.grey : Colors.blue,
  onPressed: () => setState(() => pressAttention = !pressAttention),
);

如果希望按钮更改按下状态的颜色,只需使用RaisedButton中的“highlightColor”属性

       RaisedButton(
          onPressed: () {},
          child: Text("Test"),
          highlightColor: YOUR_PRESSED_COLOR, //Replace with actual colors
          color: IDLE_STATE_COLOR,
        ),
或者您可以使用rxdart:

import 'package:rxdart/rxdart.dart';

...

bool presssedFavorite = false;
final _pressAttention = BehaviorSubject<bool>();
Observable<bool> get coursesStream => _pressAttention.stream;

...

StreamBuilder(stream: _pressAttention,
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData)
  presssedFavorite = snapshot.data;

  return RawMaterialButton(
      onPressed: (){
        _addToFavorites(context);
      },
      child: Padding(
        padding: EdgeInsets.all(5),
        child: Icon(
          presssedFavorite ? Icons.favorite : Icons.favorite_border,
          color: Colors.red,
          size: 17,
        ),
      ),
    );
  },
),

void _addToFavorites(BuildContext context) async{
  //my code...
  _pressAttention.sink.add(!presssedFavorite);
}
import'包:rxdart/rxdart.dart';
...
bool pressedfavorite=false;
最后按注意=行为主体();
可观察的get-CourseStream=>\u-pressAttention.stream;
...
StreamBuilder(流:注意,
生成器:(BuildContext上下文,异步快照){
if(snapshot.hasData)
按sedFavorite=snapshot.data;
返回原材料按钮(
已按下:(){
_addToFavorites(上下文);
},
孩子:填充(
填充:边缘设置。全部(5),
子:图标(
按“收藏”图标。收藏:图标。收藏\u边框,
颜色:颜色,红色,
尺码:17,
),
),
);
},
),
void\u addToFavorites(BuildContext上下文)异步{
//我的代码。。。
_按Attention.sink.add(!按sedFavorite);
}

它更复杂,但您也可以将此解决方案用于web服务、firestore、db。。。您可以使用无状态Widget和StatefulWidget。

您是在调用setState吗?我不这么认为,我对Fatter和dart非常陌生。当您告诉它发生了变化时,Fatter只会重新绘制内容,一种方法是使用setState。我建议你看看网站上的教程,例如。