简体   繁体   English

如何在 Flutter 中更新 showmodalsheet 中的小部件颜色?

[英]How to update the widget color in showmodalsheet in Flutter?

I'm new to flutter I want my widget border color to change on pressed I try it using setState() but it won't affect at all is there some other function to use in order to change my widget color in flutter?我是 flutter 的新手 我希望我的小部件边框颜色在按下时改变 我尝试使用 setState() 但它根本不会影响是否有其他一些功能可以使用以在 flutter 中更改我的小部件颜色? if so can someone show me an example of how to use it?如果可以,有人可以告诉我如何使用它的示例吗? Thank You hope someone will help谢谢你希望有人会帮助

showModalBottomSheet( backgroundColor: Color(0xff086375), context: context, showModalBottomSheet(背景颜色:颜色(0xff086375),上下文:上下文,

    isScrollControlled: true,
    builder: (BuildContext context){
      return Container(
          padding: const EdgeInsets.symmetric(horizontal: 18),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              SizedBox(
                height: 8.0,
              ),
              Padding(
                padding: EdgeInsets.only(
                    bottom: MediaQuery.of(context).viewInsets.bottom),
                child: Column(
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Text(
                          "PHP ",
                          style:
                              TextStyle(color: Colors.white, fontSize: 20),
                        ),
                        Container(
                          width: 40,
                          child: TextField(
                            cursorColor: Colors.white,
                            cursorWidth: 2,
                            keyboardType: TextInputType.number,
                            style: TextStyle(color: Colors.white),
                            decoration: InputDecoration.collapsed(
                                hintText: '0.00',
                                hintStyle: TextStyle(
                                    color: Colors.white70, fontSize: 20)),
                            autofocus: true,
                          ),
                        ),
                      ],
                    ),
                    Container(
                      child: Text(
                        "PAYMENT FOR " + selectedExpense,
                        style: TextStyle(color: Colors.white70),
                      ),
                    ),
                    Container(
                      color: Color(0xff086375),
                      height: 40,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                          Container(
                            child: IconButton(
                              icon: Icon(FontAwesomeIcons.utensils),
                              color: Colors.orange,
                              onPressed: () {
                                setState(() {
                                  selectedExpense = "FOOD";
                                  isFoodseleted = true;
                                  isTransportationseletced = false;
                                  isGroceriesselected = false;
                                  isAccomodationselected = false;
                                  isDrinksselected = false;
                                  isOhterselected = false;
                                });
                              },
                            ),
                            width: 50,
                            height: 40,
                            decoration: BoxDecoration(
                              border: Border(
                                  bottom: BorderSide(
                                      width: 5,
                                      color: isFoodseleted
                                          ? Colors.white
                                          : Color(0xff086375))),
                            ),
                          ),
                          Container(
                            child: IconButton(
                              icon: Icon(FontAwesomeIcons.bus),
                              color: Colors.yellow,
                              onPressed: () {
                                setState(() {
                                  selectedExpense = "TRANSPORTATION";
                                  isFoodseleted = false;
                                  isTransportationseletced = true;
                                  isGroceriesselected = false;
                                  isAccomodationselected = false;
                                  isDrinksselected = false;
                                  isOhterselected = false;
                                });
                              },
                            ),
                            width: 50,
                            height: 40,
                            decoration: BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                        width: 5,
                                        color: isTransportationseletced
                                            ? Colors.white
                                            : Color(0xff086375)))),
                          ),
                          Container(
                            child: IconButton(
                              icon: Icon(FontAwesomeIcons.shoppingCart),
                              color: Colors.lightBlue,
                              onPressed: () {
                                setState(() {
                                  selectedExpense = "GROCERIES";
                                  isFoodseleted = false;
                                  isTransportationseletced = false;
                                  isGroceriesselected = true;
                                  isAccomodationselected = false;
                                  isDrinksselected = false;
                                  isOhterselected = false;
                                });
                              },
                            ),
                            width: 50,
                            height: 40,
                            decoration: BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                        width: 5,
                                        color: isGroceriesselected
                                            ? Colors.white
                                            : Color(0xff086375)))),
                          ),
                          Container(
                            child: IconButton(
                              icon: Icon(FontAwesomeIcons.bed),
                              color: Colors.green,
                              onPressed: () {
                                setState(() {
                                  selectedExpense = "ACCOMODATION";
                                  isFoodseleted = false;
                                  isTransportationseletced = false;
                                  isGroceriesselected = false;
                                  isAccomodationselected = true;
                                  isDrinksselected = false;
                                  isOhterselected = false;
                                });
                              },
                            ),
                            width: 50,
                            height: 40,
                            decoration: BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                        width: 5,
                                        color: isAccomodationselected
                                            ? Colors.white
                                            : Color(0xff086375)))),
                          ),
                          Container(
                            child: IconButton(
                              icon: Icon(FontAwesomeIcons.glassMartini),
                              color: Colors.red,
                              onPressed: () {
                                setState(() {
                                  selectedExpense = "DRINKS";
                                  isFoodseleted = false;
                                  isTransportationseletced = false;
                                  isGroceriesselected = false;
                                  isAccomodationselected = false;
                                  isDrinksselected = true;
                                  isOhterselected = false;
                                });
                              },
                            ),
                            width: 50,
                            height: 40,
                            decoration: BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                        width: 5,
                                        color: isDrinksselected
                                            ? Colors.white
                                            : Color(0xff086375)))),
                          ),
                          Container(
                            child: IconButton(
                              icon: Icon(FontAwesomeIcons.plusCircle),
                              color: Colors.amber,
                              onPressed: () {
                                setState(() {
                                  selectedExpense = "OTHERS";
                                  isFoodseleted = false;
                                  isTransportationseletced = false;
                                  isGroceriesselected = false;
                                  isAccomodationselected = false;
                                  isDrinksselected = false;
                                  isOhterselected = true;
                                });
                              },
                            ),
                            width: 50,
                            height: 40,
                            decoration: BoxDecoration(
                                border: Border(
                                    bottom: BorderSide(
                                        width: 5,
                                        color: isOhterselected
                                            ? Colors.white
                                            : Color(0xff086375)))),
                          )
                        ],
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(height: 10),
            ],
          ),
        );

    }




    )

Just change all your IconButtonMethods as follow,只需如下更改所有 IconButtonMethods,

IconButton(
                              icon: Icon(FontAwesomeIcons.glassMartini),
                              color: isDrinksselected
                                            ? Colors.white
                                            : Color(0xff086375),
                              onPressed: () {
                              // statements
                              }
)

You are adding your color change condition to root container.您正在将颜色更改条件添加到根容器。

I think you want to change the color of the icon on the press event.我认为您想更改新闻事件中图标的颜色。

So, your ternary operator ("?:") condition for color change should be for your IconButton color instead of container.因此,您的三元运算符 ("?:") 颜色更改条件应该针对您的 IconButton 颜色而不是容器。

Just add it on all the IconButton color property and you're good to go.只需将它添加到所有 IconButton 颜色属性上即可。

Here is an example that you can refer:这是您可以参考的示例:

ValueNotifier<Color> is used to dispatch change and ValueListenableBuilder<Color> is used to consume change. ValueNotifier<Color>用于分派更改,而ValueListenableBuilder<Color>用于使用更改。

在此处输入图像描述

Run code on DartPad 在 DartPad 上运行代码

Refer following code:参考以下代码:

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(home: MyApp()),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showModalBottomSheet(
            context: context,
            builder: bottomSheetContent,
          );
        },
      ),
    );
  }
}

Widget bottomSheetContent(BuildContext context) {
  final List<Color> _colors = <Color>[
    Colors.redAccent,
    Colors.blueAccent,
    Colors.greenAccent,
    Colors.orangeAccent,
  ];
  final ValueNotifier<Color> _colorNotifier = ValueNotifier(_colors.first);
  return ValueListenableBuilder(
    valueListenable: _colorNotifier,
    builder: (BuildContext context, Color value, Widget child) {
      return Container(
        height: 300,
        color: value,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Card(
                child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: widgets(value),
            )),
            colors(_colors, _colorNotifier),
          ],
        ),
      );
    },
  );
}

Row widgets(Color color) {
  return Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: <Widget>[
      Text(
        'Text',
        style: TextStyle(color: color),
      ),
      FloatingActionButton(
        backgroundColor: color,
        onPressed: () {},
        child: Icon(Icons.add),
      ), 
      Switch(onChanged: (bool value) {}, value: true, activeColor: color,)
    ],
  );
}

Row colors(List<Color> _colors, ValueNotifier<Color> _colorNotifier) {
  return Row(
    children: _colors
        .map((color) => Expanded(
              child: InkWell(
                  onTap: () {
                    _colorNotifier.value = color;
                  },
                  child: Container(
                    decoration:
                        BoxDecoration(border: Border.all(), color: color),
                    height: 100,
                  )),
            ))
        .toList(),
  );
}

maybe you can apply this to your solution:也许您可以将其应用于您的解决方案:

Color color = new Color(0xffb74093);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        color: color,
        child: Container(
          width: MediaQuery.of(context).size.width,
          height: 200,
          child: Column(
            children: <Widget>[
              FlatButton(
                color: Colors.blue,
                onPressed: (){
                  setColor(Colors.blue);

                }
              ),

              FlatButton(
                color: Colors.red,
                onPressed: (){
                  setColor(Colors.red);
                }
              ),
            ],
          ),
        ),
      ),
    );
  }


  Color setColor(Color newColor){
    setState(() {
      color = newColor;
    });
  }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM