【问题标题】:I need to change color for single container but it's change color for whole container我需要更改单个容器的颜色,但它会更改整个容器的颜色
【发布时间】:2021-10-22 20:38:41
【问题描述】:
      AnimationController? controller;
       Animation<double>? animation;
      @override
      void initState() {
super.initState();
controller =
    AnimationController(duration: const Duration(seconds: 3), vsync: this)
      ..addListener(() => setState(() {}));
animation = Tween(begin: -500.0, end: 500.0).animate(controller!);
controller!.forward();
     }


     List tile = [
     [true, true, true, true],
     [true, true, true, true],
      [true, true, true, true],
       [true, true, true, true],
     ];
     String? gettw;

   Container(
      color: Colors.white,
      child: ListView.builder(
        reverse: false,
        itemCount: tile[0].length,
        itemBuilder: (BuildContext context, int index) {
          String? gettw;

          if (tile[index][0]) {
            if (gettw != 'transparent') {
              gettw = 'black';
            }
          } else if (tile[index][0] == false) {
            gettw = 'transparent';
          }
          print('gettw $gettw');
          return GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              setState(() {
                if (tile[index][0] == true) {
                  print('working');
                  gettw = 'transparent';
                  print('gettw $gettw');
                } else if (tile[index][0] == false) {
                  gettw = 'red';
                }
              });
            },
            child: Transform.translate(
              offset: Offset(0.0, animation!.value),
              child: Container(
                height: MediaQuery.of(context).size.height / 4,
                color: (gettw == 'black')
                    ? Colors.black
                    : (gettw == 'transparent')
                        ? Colors.transparent
                        : (gettw == 'red')
                            ? Colors.red
                            : Colors.green,
                child: Text('${tile[index][0]}',
                    style: const TextStyle(color: Colors.purple)),
              ),
            ),
          );
        },
      )),

我从上到下为 4 个容器设置动画。如果用户单击容器。容器颜色需要将黑色变为白色。但所有 4 个容器的颜色都发生了变化。 gettw 变量包含需要在容器中显示的颜色。请有人帮我解决这个问题。

【问题讨论】:

    标签: flutter flutter-animation


    【解决方案1】:

    试试这个

    import 'package:flutter/material.dart';
    
    class ColorChanger extends StatefulWidget {
    const ColorChanger({ Key? key }) : super(key: key);
    
    @override
    _ColorChangerState createState() => _ColorChangerState();
    }
    
    class _ColorChangerState extends State<ColorChanger> {
       List<bool> widgetActive = List.generate(4, (index){
       return false;
    });
    @override
    Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        itemCount: 4,
        itemBuilder: (context, index){
        
        return InkWell(
          onTap: (){
            setState(() {
              widgetActive[index] = !widgetActive[index];
            });
          },
          child: Container(
            margin: EdgeInsets.all(30),
            width: 40,
            height:50,
            color: widgetActive[index] ? Colors.black:Colors.red,
          ),
        );
      }),
      );
     }
    }
    

    【讨论】:

    • 好的。但是当用户单击容器时我需要更改颜色。容器颜色状态在用户点击之前是黑白的。如果用户单击,则颜色将变为白色或红色。
    • 另一个问题我点击了第一个容器,但第二个容器的颜色发生了变化。列表已更新我要显示的颜色但状态未重建(颜色未更改)
    • 更新了我的答案,请尝试那个
    • 我试试你的代码,但问题是颜色没有改变正确的容器。例如,如果点击第一个容器颜色在第二个容器上改变。如果我再次点击第二个容器上的第一个容器颜色更改。我将 transform.translate 用于容器从上到下的动画。所以容器位置每秒都在变化。你能帮帮我吗?
    • 你能详细说明一下从上到下的动画吗?
    猜你喜欢
    • 2020-10-11
    • 1970-01-01
    • 2020-09-25
    • 2020-09-29
    • 2022-10-18
    • 1970-01-01
    • 2021-06-29
    • 2020-03-26
    • 1970-01-01
    相关资源
    最近更新 更多