简体   繁体   English

Flutter - 如何在单击时切换 RaisedButton 的颜色?

[英]Flutter - How do I toggle the color of a RaisedButton upon click?

I am trying to toggle the color of a raised button.我正在尝试切换凸起按钮的颜色。 Initially the button should be blue and when it is pressed it turns to grey.最初按钮应该是蓝色的,当它被按下时它会变成灰色。 Right now I have a bool value called pressAttention and it is set to false.现在我有一个名为 pressAttention 的 bool 值,它被设置为 false。 I am using this to initially set this the false.我正在使用它最初将其设置为 false。 When the button is pressed it toggles the pressAttention bool, but it seems that the widget is never updated again.当按下按钮时,它会切换 pressAttention 布尔值,但该小部件似乎永远不会再次更新。

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;
  }
}

} }

This button will need to be created in the build of a State of a StatefulWidget , and the State must have a member variable bool pressAttention = false;这个按钮需要在StatefulWidgetStatebuild中创建,并且 State 必须有一个成员变量bool pressAttention = false; . . As Edman suggests, you need to make state changes in a setState callback for the Widget to redraw.正如 Edman 所建议的,您需要在setState回调中更改状态,以便 Widget 重绘。

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

If you want the button to change color for the pressed state you just need to use the "highlightColor" property in RaisedButton如果您希望按钮为按下状态更改颜色,您只需要使用 RaisedButton 中的“highlightColor”属性

       RaisedButton(
          onPressed: () {},
          child: Text("Test"),
          highlightColor: YOUR_PRESSED_COLOR, //Replace with actual colors
          color: IDLE_STATE_COLOR,
        ),

Or you can use rxdart:或者你可以使用 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);
}

It is more complicated, but you can use this solution also with web services, firestore, db... And you can use with StatelessWidget and StatefulWidget.它更复杂,但您也可以将此解决方案与 Web 服务、firestore、db 一起使用……并且您可以与 StatelessWidget 和 StatefulWidget 一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter-单击后如何切换FlatButton的颜色? - Flutter - How do I toggle the color of a FlatButton upon click? 我如何在 Flutter 中使用 RGB 颜色? - How do I use RGB color in Flutter? 如果 RaisedButton.icon() 上的构造函数中的图标值为 null,如何保持图标禁用? - How do I keep icon disabled if the icon value in constructor is null on RaisedButton.icon()? Flutter:如何使用 RaisedButton 在 PageView 中导航或使用按钮移动 pageViewController - Flutter: How to navigate in PageView with a RaisedButton or move pageViewController with button 在Android Studio中以编程方式单击“切换按钮”时,如何更改“ Textview”的颜色? - how to change the color of “Textview” when i Click the “Toggle Button” in programmatically in Android Studio? 选择Flutter中的项目后如何更改卡片颜色? - How to change card color upon selection of an item in Flutter? 如何在 Flutter 中使用颜色样本的不同深浅度? - How do I use the different shades of a color swatch in Flutter? 如何在 flutter 标签栏中为未选择的标签添加背景颜色? - How do I add background color to unselected tabs in flutter tabbar? RaisedButton 不会改变背景和文本颜色 - RaisedButton not change background and text color 如何修复我的onclick侦听器,以便在单击标题时不必单击两次以触发事件? - How do I fix my onclick listener such that I don't have to click twice to fire an event upon header click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM