flutter - flutter -在另一个按钮上点击更改颜色

标签 flutter dart identifier

我有5x5网格的5xTableRow-> 5xFittedBox。通过点击右上角的按钮,我希望它可以随机排列数组中描述的颜色(参见图片)。

Array of boxes

我尝试使用“OnChanged”和“onPressed”,但是问题是我不知道如何单独访问每个元素

我读到我应该使用“setState”函数来强制 flutter 重绘某些元素,但是问题是我将所说的函数放在哪里?

还有一种简单的方法为小部件和子代提供某种标识符(例如,class =“something”或id =“something”)
关键是我想使用for循环为所有25个框重新着色,然后再将这25个框存储在数组中,以用于以后的条件检查。

var colorArr = [0xffd61745, 0xff3569bc, 0xffffffff, 0xfff4862a, 0xffeaed19, 0xff329f64, 
0xff000000];
//playArr is used to store values from 0-5 which corresponds each color => colorArr[playArr[n]]
var playArr = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; 
void shuffleBox() {
  for (var i = 0; i<playArr.length;i++) {
    playArr[i] = new Random().nextInt(6);
    print(playArr[i]);
  }
} 

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(
      centerTitle: true,
      title: Container(
        child: Text(
          'Table Widget',
          style: TextStyle(
            fontSize: 20.0,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      actions: <Widget>[
        IconButton(
          icon: Icon(Icons.shuffle),
          onPressed: shuffleBox,
        ),
      ],
    ),
    body: SingleChildScrollView(
      padding: EdgeInsets.only(top: 12),
      child: Table(
        border: null,
        defaultVerticalAlignment: TableCellVerticalAlignment.top,
        children: <TableRow>[
          TableRow(children: <Widget>[ 
            //this is what i want recolored and/or redrawn
            FittedBox(
              fit: BoxFit.contain,
              child: Container(
                margin: EdgeInsets.all(2),
                color: Color(a),
                width: 48.0,
                height: 48.0,
              ),
            ),
            FittedBox -> repeated 5x, then TableRow again

最佳答案

这是工作解决方案

final colorArr = [0xffd61745, 0xff3569bc, 0xffffffff, 0xfff4862a, 0xffeaed19, 0xff329f64, 
0xff000000];
//playArr is used to store values from 0-5 which corresponds each color => colorArr[playArr[n]]
var playArr = [];

  @override
 void  initState(){
   _genereateList();
   super.initState();
 }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        actions: [
          IconButton(icon: Icon(Icons.shuffle),onPressed: _shuffle,)
        ]
      ),
      body: Center(
        child : GridView.builder(
          itemCount: 25,
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 5),
          itemBuilder : (ctx, index)=>Card(
            child: Text(playArr[index].toString()),
            color: Color(colorArr[playArr[index]])
          )
        )
      ),
    );
  }

  _genereateList(){
    playArr = List.generate(25,(int index)=>Random().nextInt(colorArr.length-1));
    setState((){});
  }
  _shuffle(){
     playArr.shuffle(Random());
    setState((){});
  }

编辑:如果要更新单元格,只需更改其值即可。
假设您要在第3列第5原始处更改项目
update(int index){
setState((){
playArr[index] = 3; //Index of the new color you want to switch to
});
}

然后简单地这样称呼它
update(22); //3rd Column 5th raw

关于flutter - flutter -在另一个按钮上点击更改颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60317180/

相关文章:

Flutter单击后退按钮时如何执行

dart - 在更改 BottomNavigationBar 上的选项卡时禁用重建页面

django - Assets 的 Dart 绝对 URL。或者在单独的服务器/文件夹上提供 Assets

python - 如何检查标识符是 dunder 还是 class-private(即会被破坏)?

dart - Flutter 中的可滚动 ListView 与 Dart

ios - Flutter - SWIFT_VERSION 必须设置为支持的值

dart - 获取传递给 Dart 函数/构造函数调用的参数集合

c - 我得到 "greedy.c:17:1: error: expected identifier or ' (' {"有人可以帮忙吗?它指的是 int main(void) 之后的行

c++ - 创建飞镖游戏并出现两个错误 "too few argurments in function call"和 "game, identifier not found"

flutter - 如何在 flutter 中制作可滚动框?