简体   繁体   English

颤动中凸起按钮的 OnPressed 中的颜色变化

[英]Color Change in the OnPressed of the RaisedButton in flutter

"When you select a color from the drop-down menu, the drop-down button widget's title shows the selected color. Then, when you tap one of the four buttons, only that particular button's background color will change to the selected background color." “当您从下拉菜单中选择一种颜色时,下拉按钮小部件的标题会显示选定的颜色。然后,当您点击四个按钮之一时,只有该特定按钮的背景颜色会更改为选定的背景颜色。 ” This is what I want to do it.这就是我想要做的。

在此处输入图片说明

This picture shows the screen.这张图显示了屏幕。 There are 4 color options in the DropdownButton. DropdownButton 中有 4 种颜色选项。 In the beginning, I made maps for the get "Colors.black" etc. Then I wrote a function called "change" and this function for the color palettes.一开始,我为 get "Colors.black" 等制作了地图。然后我编写了一个名为 "change" 的函数,这个函数用于调色板。

But I confused about where to call this function.但是我对在哪里调用这个函数感到困惑。 onPressed part of the RaisedButtons is empty right now. RaisedButtons 的 onPressed 部分现在是空的。 In the first RaisedButton,在第一个 RaisedButton 中,

_favIconColor = ;

That part will be equals to the new color.那部分将等于新颜色。 But I couldn't call the function anywhere.但我无法在任何地方调用该函数。

This is my whole code:这是我的全部代码:

import 'package:flutter/material.dart';

void main () => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}



class _MyAppState extends State<MyApp> {

String renk;
String decoration;
String x;
List<DropdownMenuItem> frommenuitems = [

DropdownMenuItem(child: Text('Black'),value: 'Black'),
DropdownMenuItem(child: Text('Green'),value: 'Green'),
DropdownMenuItem(child: Text('Orange'),value: 'Orange'),
DropdownMenuItem(child: Text('Blue'),value: 'Blue')];

final Map<String, int> renkmap = {  
  'Black': 0,  
  'Green': 1,  
  'Orange': 2,  
  'Blue': 3};

final Map<String, List> formulas = {  
  '0': [Colors.black],  
  '1': [Colors.green],  
  '2': [Colors.orange],  
  '3': [Colors.blue]};

void change(renk) {

  int newcolor = renkmap[renk];
  var result = formulas[newcolor];

}
Color _favIconColor = Colors.black; //for the set the RaisedButton color to the black in the beginning
@override

Widget build(BuildContext context) {
  final TextStyle header = TextStyle (fontSize: 30, color: Colors.red[500], fontWeight: FontWeight.bold);
  final TextStyle buttontext = TextStyle (fontSize: 25, color: Colors.white, fontWeight: FontWeight.bold);

  return MaterialApp(
    title: 'Color Changing',
    home: Scaffold(
  
    body: Container(
      child: Column(
        children: <Widget> [
          Spacer(), //flex property
          Text('Select a color', style: header),

          DropdownButton(items: frommenuitems, hint: Text('Black', style: TextStyle(color: Colors.black)),
           value: renk,
           onChanged: (value) {
             setState(() {
               renk = value;
               change(renk);  
             });
           }
           ),

           Spacer(), 
           ButtonTheme(
             minWidth: 2000.0,
             height: 100.0, 
             child: RaisedButton(child: Text('Change Color', style: buttontext), color: _favIconColor, 
             onPressed: () {
               setState(() {
                 
                 //_favIconColor = ;
               });

             })),


             ButtonTheme(
             minWidth: 2000.0,
             height: 100.0, 
             child: RaisedButton(child: Text('Change Color', style: buttontext), color: Colors.black, 
             onPressed: () {

             })),

             ButtonTheme(
             minWidth: 2000.0,
             height: 100.0, 
             child: RaisedButton(child: Text('Change Color', style: buttontext), color: Colors.black, 
             onPressed: () {})),

             ButtonTheme(
             minWidth: 2000.0,
             height: 100.0, 
             child: RaisedButton(child: Text('Change Color', style: buttontext), color: Colors.black, 
             onPressed: () {}))
            ],
          ),
      ),
    ),
  );}}

you should do this你应该做这个

_favIconColor = Colors.black;


ButtonTheme(
             minWidth: 2000.0,
             height: 100.0, 
             child: RaisedButton(child: Text('Change Color', style: buttontext), color: _favIconColor, 
             onPressed: () {
               setState(() {
                 
                 _favIconColor =newColor ;// the one selected from the drop down menu
               });

             })),

the issue here is that every time you change the color all the Buttons will have the same color ( because of the setState ) if that ok for you , in case you want that every button has its own color you should consider using an array to know which button should change the color这里的问题是,每次更改颜色时,所有按钮都将具有相同的颜色(因为 setState),如果这对您来说没有问题,如果您希望每个按钮都有自己的颜色,您应该考虑使用数组来了解哪个按钮应该改变颜色

Ps : i don't know what is your change function is used for !! Ps:我不知道你的change函数是干什么用的!! it has no effect here unless it returns the color value or code depends on how you want to use it它在这里没有任何影响,除非它返回颜色值或代码取决于您想如何使用它

I'm sure there are lots of better solutions with more clean codes.我相信有很多更好的解决方案和更干净的代码。 But for a temporary workaround, you can try this:但是对于临时解决方法,您可以尝试以下操作:


import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String renk;
  String decoration;
  String x;
  List<DropdownMenuItem> frommenuitems = [
    DropdownMenuItem(child: Text('Black'), value: 'Black'),
    DropdownMenuItem(child: Text('Green'), value: 'Green'),
    DropdownMenuItem(child: Text('Orange'), value: 'Orange'),
    DropdownMenuItem(child: Text('Blue'), value: 'Blue')
  ];

  final Map<String, int> renkmap = {
    'Black': 0,
    'Green': 1,
    'Orange': 2,
    'Blue': 3
  };

  final Map<String, List> formulas = {
    '0': [Colors.black],
    '1': [Colors.green],
    '2': [Colors.orange],
    '3': [Colors.blue]
  };

  Color _favIconColor = Colors.black;

  List<Color> _favIconColorList = [
    Colors.black,
    Colors.black,
    Colors.black,
    Colors.black
  ];

  int selectedIndex = -1;

  //for the set the RaisedButton color to the black in the beginning
  @override
  Widget build(BuildContext context) {
    void change(renk) {
      int newcolor = renkmap[renk];
      var result = formulas[newcolor.toString()][0];
      /**
     * In here, you need to convert int to string, and take the first of the Color list
     */

      // In here, if an item selected, then only it should be change.
      if (selectedIndex == -1) {
        _favIconColorList[0] = result;
        _favIconColorList[1] = result;
        _favIconColorList[2] = result;
        _favIconColorList[3] = result;
      } else {
          _favIconColorList[selectedIndex] = result;    
      }
    }

    final TextStyle header = TextStyle(
        fontSize: 30, color: Colors.red[500], fontWeight: FontWeight.bold);
    final TextStyle buttontext = TextStyle(
        fontSize: 25, color: Colors.white, fontWeight: FontWeight.bold);

    return MaterialApp(
      title: 'Color Changing',
      home: Scaffold(
        body: Container(
          child: Column(
            children: <Widget>[
              Spacer(), //flex property
              Text('Select a color', style: header),

              DropdownButton(
                  items: frommenuitems,
                  hint: Text("Color", style: TextStyle(color: _favIconColor)),
                  value: renk,
                  onChanged: (value) {
                    setState(() {
                      renk = value;
                      change(renk);
                    });
                  }),

              Spacer(),
              ButtonTheme(
                  minWidth: 2000.0,
                  height: 100.0,
                  child: RaisedButton(
                      child: Text('Change Color', style: buttontext),
                      color: _favIconColorList[0],
                      onPressed: () {
                        setState(() {
                          selectedIndex = 0;
                          //_favIconColor = ;
                        });
                      })),

              ButtonTheme(
                  minWidth: 2000.0,
                  height: 100.0,
                  child: RaisedButton(
                      child: Text('Change Color', style: buttontext),
                      color: _favIconColorList[1],
                      onPressed: () {
                        setState(() {
                          selectedIndex = 1;
                        });
                      })),

              ButtonTheme(
                  minWidth: 2000.0,
                  height: 100.0,
                  child: RaisedButton(
                      child: Text('Change Color', style: buttontext),
                      color: _favIconColorList[2],
                      onPressed: () {
                        setState(() {
                          selectedIndex = 2;
                        });
                      })),

              ButtonTheme(
                  minWidth: 2000.0,
                  height: 100.0,
                  child: RaisedButton(
                      child: Text('Change Color', style: buttontext),
                      color: _favIconColorList[3],
                      onPressed: () {
                        setState(() {
                          selectedIndex = 3;
                        });
                      }))
            ],
          ),
        ),
      ),
    );
  }
}

You can define a _favIconColorList to store for each ButtonTheme item's color and a selectedIndex with a default value, -1.您可以定义一个 _favIconColorList 来存储每个 ButtonTheme 项目的颜色和一个 selectedIndex,默认值为 -1。 If it set to an index of ButtonTheme list by tapping one of them, then the color will set only the selected ButtonTheme.如果通过点击其中之一将其设置为 ButtonTheme 列表的索引,则颜色将仅设置选定的 ButtonTheme。 Otherwise, all of the ButtonTheme list will be change.否则,所有的 ButtonTheme 列表都会改变。

在此处输入图片说明

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

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