列表磁贴颤动的交替背景颜色

时间:2021-04-25 07:03:24

标签: flutter listview dart

Sample Image

从示例图像中,您可以看到 listTilelistView 上充气的儿童有不同的背景颜色。它的工作方式是第一项变为白色,第二项变为灰色,第三项变为白色,第四项变为灰色,依此类推。这意味着偶数和奇数项目的颜色会有所不同但保持一致。

我已经知道如何设置 backgroundColor 但我可以使用什么样的算法来实现这样的目标?

1 个答案:

答案 0 :(得分:0)

如果您使用列表视图构建器,则可以使用 itemBuilder 中的索引在背景颜色之间交替。

#include <stdio.h>
#include <stdlib.h>

typedef struct linkedlist * int_list;
#define new_int_list() (struct linkedlist*) malloc(sizeof(struct linkedlist))

struct node
{
    int value;
    struct node *next;
};

struct linkedlist 
{
    size_t length;
    struct node *head;
    struct node *tail;
};

void append(struct linkedlist *list, int value)
{
    struct node *newnode = (struct node*) malloc(sizeof(struct node));
    newnode->value = value;
    newnode->next = NULL;

    if(list->head==NULL)
    {
        list->head = newnode;
        list->tail = newnode;
        list->tail->next = NULL;
        list->length = 1;
    }
    else{
        list->tail->next = newnode;
        list->tail = newnode;
        list->tail->next = NULL;
        list->length++;
    }
}

void remove_index_raw(const char* srcs, size_t line, struct linkedlist *list, size_t index)
{
    if(list->length==0) return;
    else if(index<0 || index>=list->length)
    {
        printf("\n\nError in file '%s' on line %ld:\n\tindex provided value is (%ld)\n\tList size is(%ld)\n\tindex overflow\n\n", srcs,line,index,list->length);
        exit(2);
    }
    else if(index==0 && list->length==1){
        free(list->head);
        list->head == NULL;
        list->tail == NULL;
        list->length = 0;
        return;
    }
    else if(index==0){
        struct node *delete_first = list->head;
        list->head = list->head->next;
        free(delete_first);
        list->length--;
        return;
    }

    struct node *iterator = list->head;
    while(--index) iterator = iterator->next;

    struct node *delete_node = iterator->next;
    iterator->next = iterator->next->next;
    free(delete_node);
    list->length--;
}
#define remove_index(list,index) remove_index_raw(__FILE__,__LINE__,list,index)

void display_list(struct linkedlist *list)
{
    size_t iterate = list->length;
    if(list->length==0) return;
    struct node* start = list->head;
    while(iterate){
        --iterate;
        printf("%d ",start->value);
        start = start->next;
    }
    printf("\n");
}

void init_list(struct linkedlist *list)
{
    list->head = NULL;
    list->tail = NULL;
    list->length = 0;
}

void main()
{
    int MAX = 10000000;

    int_list largelist = new_int_list();
    init_list(largelist);
    
    for(size_t i=0; i<MAX; ++i) append(largelist,i);

    char pause;
    printf("allocated");
    scanf("%c",&pause);

    for(size_t i=0; i<MAX; ++i) remove_index(largelist,0);
    
    printf("deallocated");
    scanf("%c",&pause);
    scanf("%c",&pause);
}