首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颜色变化时不呈现的颤振

颜色变化时不呈现的颤振
EN

Stack Overflow用户
提问于 2018-08-27 17:10:18
回答 1查看 1.1K关注 0票数 0

我对飞镖很陌生。我编写了一个在GridView中显示瓷砖的应用程序。每个瓷砖都被编号和着色,并被分配一个onTap处理程序。onTap处理程序将对平铺的索引进行debugPrint并调用setState()。setState()将强制使用Color.white中显示的抽头块重新呈现GridView。

在调试模式下执行时,设备上的显示如预期的那样。当一个瓷砖被点击时,debugPrint消息将显示在调试控制台中,但是所点击的瓷砖的颜色不会更改为Color.white。

我检查了瓷砖的再生时间(当瓷砖被敲打时)。正如预期的那样,所有未开发的瓷砖都分配了它们原来的瓷砖颜色。抽头瓷砖被指定为Color.white。但是,瓷砖颜色的变化(即被点击的瓷砖颜色从原来的颜色变为Color.white)似乎没有被检测到,并且应用程序不会用一个白色的瓷砖重新绘制。

应用程序代码如下。请告诉我做错了什么。

代码语言:javascript
复制
// ignore_for_file: camel_case_types
// ignore_for_file: constant_identifier_names
// ignore_for_file: non_constant_identifier_names

import 'package:flutter/material.dart';

const List _normal_colors = [
  Colors.indigo,
  Colors.green,
  Colors.amber,
  Colors.orange,
  Colors.red,
  Colors.blue,
  Colors.cyan,
  Colors.yellow,
  Colors.purple,
]; // _normal_colors

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

class Tiled_Surface extends StatefulWidget {
  Tiled_Surface({Key key}) : super(key: key);

  @override // Tiled_Surface
  Tiled_Surface_State createState() => Tiled_Surface_State();
}

class Tiled_Surface_State extends State<Tiled_Surface> {
  List<GridTile> _grid_tiles = <GridTile>[];
  int _tapped = -1;

  void on_tile_tapped(int index) {
    debugPrint("You tapped on item $index");
    setState(() {
      _tapped = index;
    });
  } // on_tile_tapped

  GridTile new_surface_tile(Color tile_color, int index) {
    GridTile tile = GridTile(
      child: GestureDetector(
        onTap: () => on_tile_tapped(index),
        child: Container(
          child: Center(
            child: Text (
              index.toString(),
              style: TextStyle(
                fontSize: 24.0,
                color: _tapped == index ?
                           Colors.black :
                           Colors.white,),
            )
          ),
          decoration: BoxDecoration(
            color: _tapped == index ?
                       Colors.white :
                       tile_color,
            shape: BoxShape.rectangle,
          ),
        ),
      )
    );
  return (tile);
  } // new_surface_tile

  List<GridTile> surface_tiles(colors) {
    _grid_tiles.clear();
    for (int i = 0; (i < colors.length); i++) {
      _grid_tiles.add(new_surface_tile(colors[i], i));
    }
    return (_grid_tiles);
  } // surface_tiles

  @override // Tiled_Surface_State
  Widget build(BuildContext context) {
    return GridView.count(
      shrinkWrap: true,
      crossAxisCount: 3,
      childAspectRatio: 1.0,
      padding: const EdgeInsets.all(4.0),
      mainAxisSpacing: 4.0,
      crossAxisSpacing: 4.0,
      children: surface_tiles(_normal_colors),
    );
  }
} // class Tiled_Surface_State

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Widget tiled_surface = Container(
      child: Column(
        children: [
          Tiled_Surface(),
        ],
      ),
    );

    return MaterialApp(
      title: 'Tiled Surface Demo',
      home: Scaffold(
        appBar: AppBar(title: Text('Tiled Surface Demo'),
                       actions: <Widget>[
          IconButton(
            tooltip: "Replay",
            icon: Icon(Icons.replay),
            onPressed: () {
// TODO       _reinitialize_tiles ( );
            },
          ),
        ] ),
        body: Column(
          children: [
            tiled_surface,
          ],
        ),
      ),
    );
  }
}

这个问题不同于建议的答案,因为它处理的是ListView的一个私有类,而这个问题处理的是一个GridView。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-27 21:20:46

如前所述,here之所以会出现这个问题,是因为您没有遵循不可更改的原则。

问题的根源在于您的surface_tiles方法。而不是创建新的列表,而是重用旧的列表。

修复方法非常简单:

代码语言:javascript
复制
List<GridTile> surface_tiles(colors) {
  _grid_tiles = List<Widget>();
  // push items to grid_tiles

而不是

代码语言:javascript
复制
List<GridTile> surface_tiles(colors) {
  _grid_tiles.clear()
  // push items to grid_tiles
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52044021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档