【问题标题】:How to change the color of the cardview on button click in flutter?如何在颤动中单击按钮时更改卡片视图的颜色?
【发布时间】:2021-05-20 12:40:54
【问题描述】:

我有几个按钮,我需要根据按钮单击设置卡片视图的颜色。 基本上,我正在为我的帖子添加一个主题,所以在预览页面上,我需要允许用户通过单击按钮来选择他们选择的颜色。

截图: CardView Page with Buttons

按键代码:

               Padding(
                        padding: const EdgeInsets.only(top: 10),
                        child: Row(
                          children: [
                            Expanded(
                              child: MaterialButton(
                                onPressed: () {},
                                color: Colors.yellow,
                                shape: CircleBorder(),
                              ),
                            ),
                            Expanded(
                              child: MaterialButton(
                                onPressed: () {},
                                color: Colors.orange,
                                shape: CircleBorder(),
                              ),
                            ),
                            Expanded(
                              child: MaterialButton(
                                onPressed: () {},
                                color: Colors.brown,
                                shape: CircleBorder(),
                              ),
                            ),
                            Expanded(
                              child: MaterialButton(
                                onPressed: () {},
                                color: Colors.blue,
                                shape: CircleBorder(),
                              ),
                            ),
                            Expanded(
                              child: MaterialButton(
                                onPressed: () {},
                                color: Colors.green,
                                shape: CircleBorder(),
                              ),
                            ),
                            Expanded(
                              child: MaterialButton(
                                onPressed: () {},
                                color: Colors.black,
                                shape: CircleBorder(),
                              ),
                            ),
                          ],
                        ),
                      ),

卡片浏览代码:

                Card(
                          child: Container(
                            decoration: BoxDecoration(
                                color: Colors.blue,
                                border: Border.all(
                                    color: MyColors.black, width: 1.5),
                                borderRadius: BorderRadius.circular(10)),
                            child: Column(
                              children: [
                                Row(
                                  children: [
                                    Padding(
                                      padding: const EdgeInsets.all(8.0),
                                      child: CircleAvatar(
                                        backgroundImage:
                                            CachedNetworkImageProvider(
                                                _profileurl),
                                      ),
                                    ),
                                    Text(_username),
                                  ],
                                ),
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Obx(
                                    () => Text(
                                      pollDataController1.question.value,
                                      style: TextType.boldHeading,
                                    ),
                                  ),
                                ),
                                Obx(
                                  () => ClipRRect(
                                    borderRadius: BorderRadius.circular(10),
                                    child: Image.asset(
                                      (pollImageController.imageDisplay.value),
                                      width: 320,
                                      height: 170,
                                      fit: BoxFit.cover,
                                    ),
                                  ),
                                ),
                                Padding(
                                  padding: const EdgeInsets.symmetric(
                                      vertical: 10.0),
                                  child: Column(
                                    children: [
                                      Column(
                                        children: [
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Obx(
                                                () => Text(
                                                  pollDataController1
                                                          .op1.value +
                                                      "  ",
                                                  style: TextStyle(
                                                    color: MyColors.offBlack,
                                                    fontSize: 16.0,
                                                  ),
                                                ),
                                              ),
                                              Obx(
                                                () => Text(pollDataController1
                                                    .op1Emoji.value),
                                              ),
                                              SizedBox(
                                                width: 25.0,
                                              ),
                                              Obx(
                                                () => Text(
                                                  pollDataController1
                                                          .op2.value +
                                                      "  ",
                                                  style: TextStyle(
                                                    color: MyColors.offBlack,
                                                    fontSize: 16.0,
                                                  ),
                                                ),
                                              ),
                                              Obx(
                                                () => Text(pollDataController1
                                                    .op2Emoji.value),
                                              ),
                                            ],
                                          ),
                                        ],
                                      ),
                                      SizedBox(height: 13.0),
                                      Column(
                                        children: [
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.center,
                                            children: [
                                              Obx(
                                                () => Text(
                                                  pollDataController1
                                                          .op3.value +
                                                      "  ",
                                                  style: TextStyle(
                                                    color: MyColors.offBlack,
                                                    fontSize: 16.0,
                                                  ),
                                                ),
                                              ),
                                              Obx(
                                                () => Text(pollDataController1
                                                    .op3Emoji.value),
                                              ),
                                              SizedBox(
                                                width: 25.0,
                                              ),
                                              Obx(
                                                () => Text(
                                                  pollDataController1
                                                          .op4.value +
                                                      "  ",
                                                  style: TextStyle(
                                                    color: MyColors.offBlack,
                                                    fontSize: 16.0,
                                                  ),
                                                ),
                                              ),
                                              Obx(
                                                () => Text(pollDataController1
                                                    .op4Emoji.value),
                                              ),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),

【问题讨论】:

  • 我认为你可以为主题颜色声明一个字符串并指定默认颜色。每次用户按下按钮时,根据用户输入更新字符串参数。将此字符串用作卡片中的背景颜色。

标签: flutter dart cardview


【解决方案1】:

您可以使用全局变量来表示颜色,并使用函数在点击每个圆形按钮时更改颜色。

所以一个例子是:

Color cardBackgroundColor = Colors.white;
  
void changeColor(Color changeToColor) {
        setState(() {
            cardBackgroundColor = changeToColor;
         });
  }

然后在您的按钮代码中像这样使用它:

Expanded(
         child: MaterialButton(
         onPressed: changeColor(Colors.yellow),
         color: Colors.yellow,
         shape: CircleBorder(),
         ),
      ),

并在 Cardview 代码中将其更改为:

Card(child: Container(
                 decoration: BoxDecoration(
                 color: cardBackgroundColor,
                 border: Border.all(
                 color: MyColors.black, width: 1.5),
                 borderRadius: BorderRadius.circular(10)),

这将是最快的方法,尽管它可能不是最干净的。

【讨论】:

    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 2019-05-20
    • 2018-10-05
    • 2023-04-05
    相关资源
    最近更新 更多