如何更改从颜色列表中选择的容器颜色?

问题描述 投票:0回答:1

我有一个待办事项应用程序,我想在添加该任务时选择文本容器的颜色。任务容器在另一个屏幕上,选择颜色的选项在另一个屏幕上,并且也使用Provider进行状态管理。

这里是一个屏幕上的任务容器:

Container(
          margin: EdgeInsets.symmetric(vertical: 15),
          padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
          alignment: Alignment.centerLeft,
          height: screenHeight * 0.1,
          width: screenWidth * 0.74,
          decoration: BoxDecoration(
            color: Colors.blue , // choosen color ill be here
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(10),
              topLeft: Radius.circular(10),
              bottomRight: Radius.circular(10),
              bottomLeft: Radius.circular(0),
            ),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.3),
                blurRadius: 6,
                offset: Offset(-3, 3),
              ),
            ],
          ),
          child: Text(
            widget.taskTile,
            style: TextStyle(
              decoration: isDone ? TextDecoration.lineThrough : null,
              decorationColor: Colors.black,
              decorationThickness: 3,
              fontSize: 18,
              color: Theme.of(context).primaryTextTheme.bodyText1.color,
            ),
          ),
        ),

这里有4个圆形容器是从中选择颜色的选项:

    int _selectedIndex;
    Form(
            key: _formKey,
            child: TextFormField(
              validator: (val) => val.isEmpty ? 'Enter task' : null,
              autofocus: true,
              onChanged: (val) {
                taskTitle = val;
              },
            ),
          ),
          SizedBox(height: 70),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              chooseContainerColor(Colors.red, 0),
              chooseContainerColor(Colors.amberAccent, 1),
              chooseContainerColor(Colors.teal, 2),
              chooseContainerColor(Colors.blue, 3),
            ],
          ),
          SizedBox(height: 60),
          FlatButton(
            padding: EdgeInsets.symmetric(vertical: 20),
            onPressed: () {
              addTask();
            },
            color: CustomColors().primaryColor,
            child: Text(
              'Add',
              style: TextStyle(fontSize: 20, color: Colors.white),
            ),
          ),
         Widget chooseContainerColor(Color color, int index){
          return GestureDetector(
          onTap: (){
          setState(() {
        _selectedIndex = index;
           });
       },
     child: Container(
     height: 50,
      width: 50,
      decoration: BoxDecoration(
       border: index == _selectedIndex ? Border.all(width: 5, color: Colors.black) :    Border.all(width: 0) ,
      shape: BoxShape.circle,
      color: color,
    ),
  ),
);

}

flutter dart flutter-layout dart-html
1个回答
0
投票

要解决此问题,您需要创建一个Color变量。每次调用onPressed时,请更改变量的值,然后调用setState()来告诉小部件更新UI。将颜色声明为变量:

Color containerColor=Colors.red;

在您的onPressed()中:

setState(() {
  containerColor= Colors.teal;
});

在您的容器的装饰中:

decoration: BoxDecoration(
   color: containerColor,
)
© www.soinside.com 2019 - 2024. All rights reserved.