如何在Flutter中为小部件运动设置动画?

时间:2019-10-09 13:15:31

标签: animation flutter dart

我不熟悉Flutter中的动画,也没有弄清楚如何为两个状态之间的小部件运动设置动画。

如果我运行下面的代码,则该列的第一个子代将不可见,并且RaisedButton将出现在第一个小部件之前的位置。 我如何告诉Flutter对此进行动画处理,以便RaisedButton会向上移动而不是仅仅出现在其中?

我认为所有解决方案都太复杂了...

public class HmOpIntf {
    static final String DFLT_IP_ADDR = "127.0.0.1";
    static final int    DFLT_IP_PORT = 9502;
    static final int    DFLT_MB_UNIT = 1;
    static final int    DFLT_POLL_TM = 2;


    public static ModbusClient connectPLC(String ipAddr, int port, int unitNr)
    {
        ModbusClient mc = null;
        System.out.println("Connecting to " + ipAddr + " port " + port + " unit " + unitNr);
        try {
            mc = new ModbusClient(ipAddr, port);
            mc.Connect();
            mc.setUnitIdentifier((byte)unitNr);
            mc.WriteSingleCoil(0, true);
        } catch (Exception e) {
            System.err.println("*** connectPLC: exception caught");
            return null;
        }
        System.out.println("Connected!");
        return mc;
    }

    public static void disconnectPLC(ModbusClient mc)
    {
        mc = null;
    }


    public static String MyConvertRegistersToString(int[] regs, int startIx, int len) {
        char[] ca = new char[len];
        for (int i = 0; i < len; i++) {
            ca[i] = (char) regs[startIx + i];
        }
        return new String(ca);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ModbusClient mc = null;
        String ipAddr = DFLT_IP_ADDR;
        int ipPort = DFLT_IP_PORT;
        int mbUnit = DFLT_MB_UNIT;
        int pollTime = DFLT_POLL_TM;

        int tlBase = 2000; /* Offset in PLC's holding registry */
        int tlBlocksz = 84;
        String[] tlLabel = {"T4 mould", "T injection valve"};  /* Default */
        int trafLightNum = tlLabel.length;
        String[] colors = { "green", "yellow", "red" };
        int status;

//        Notifications.infoBox("Hello world!", "Welcome message");
        if (args != null && args.length > 0) {
            if (args.length < 4) {
                System.err.println("*** Error (" + args.length +"): arguments are: ip-addr port unit polltime label-1 ...");
                System.exit(1);
            }
            ipAddr = args[0];
            ipPort = Integer.parseInt(args[1]);
            mbUnit = Integer.parseInt(args[2]);
            pollTime = Integer.parseInt(args[3]);
        }
        if (args.length > 4) {
            trafLightNum = args.length - 4;
            tlLabel = new String[trafLightNum];
            for (int i = 0; i < trafLightNum; i++) {
                tlLabel[i] = args[i + 4];
            }
        }

//        Scope sc = new Scope();
//        sc.runScope();

        if ((mc = connectPLC(ipAddr, ipPort, mbUnit)) == null) {
            System.out.println("*** Failed to connect to PLC");
            System.exit(1);
        }

        TrafficLight tlLast = null;
        int[] values = new int[tlBlocksz];
        TrafficLight[] tl = new TrafficLight[trafLightNum];
        Scope[] sc = new Scope[trafLightNum];
        Notifications nots = new Notifications(trafLightNum);
        int locX, locY;
        for (int i = 0; i < tl.length; i++) {
            tl[i] = new TrafficLight();
            tl[i].setLbl(tlLabel[i]);
            tl[i].setVisible(true);
            if (tlLast != null) {
                locX = tlLast.getLocation().x;
                locY = tlLast.getLocation().y + tlLast.getHeight();
            } else {
                locX = tl[i].getLocation().x;
                locY = tl[i].getLocation().y;
            }
            tl[i].setLocation(locX, locY);
            sc[i] = new Scope(tlLabel[i], locX + tl[i].getWidth(), locY, 320, 290 /* tl[i].getHeight()-80 */ );
            sc[i].setGrid(10, 5);
            tlLast = tl[i];
        }
        UAppWin uw = new UAppWin("RTM Facility",  5);
        int phase = 1;

//        tl2.setVisible(true); tl2.setLocation(tl.getWidth(), 0);
        try {
            double t = 0.0;
            int[] dreg = new int[2];
            for (;;) {
                uw.newCycle(phase);
                for (int i = 0; i < tl.length; i++) {
                    values = mc.ReadHoldingRegisters(tlBase + i * tlBlocksz, values.length);
                    status = values[0];

                    if (status >= 0 && status < colors.length) {
//                        System.out.println(i + ": " + colors[status]);
                        if (status == 0) tl[i].greenOn();
                        else if (status == 1) tl[i].yellowOn();
                        else tl[i].redOn();
                    }
                    else
                        System.out.println("Status value " + i + " out of range: " + status);

                    dreg[0] = values[1]; dreg[1] = values[2];
                    double dval = (double) ModbusClient.ConvertRegistersToFloat(dreg);
                    sc[i].addValue(t, dval);
                    sc[i].drawSignal();
                    // w.addEntry(int i, float t, String label, int status (o = groen, 1 = yellow, 2 = red), float dval);
                    uw.addEntry(i, t, tlLabel[i], status, dval);

                    int msglen = values[3];
                    if (msglen > 0) {
                        String msg = MyConvertRegistersToString(values, 4, msglen);
                        System.out.println("DEBUG: received message for " + tlLabel[i] + ": " + msg);
                        nots.notify(i, msg);
                        uw.addMessage(msg);
                    }
                    else {
                        nots.notify(i, null);
                        uw.deleteMessage();
                    }
//                    System.out.println("Received for set " + i + ": status=" + status + " dval=" + dval + " msglen=" + msglen);
                }
                uw.endCycle();
                t += 1.0;
                Thread.sleep(pollTime * 500);
            }
        } catch (Exception e) {
            System.out.println("*** Failed to communicate with PLC - exit");
            System.exit(1);
        }

        try {
            mc.Disconnect();
        } catch (IOException e) {
            System.out.println("*** Failed to disconnect from PLC");
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我希望这对您有帮助

  double height = 200;

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        AnimatedContainer(
          duration: Duration(seconds: 3),
          height: height,
          child: Container(height: 200, width: 200, color: Colors.blue),
        ),
        RaisedButton(
          child: Container(height: 200, width: 200, color: Colors.yellow),
          onPressed: () => setState(() => height = height == 0 ? 200 : 0),
        ),
      ],
    );
  }

这是结果:

enter image description here