Flutter-OnTapDown之后未调用OnTapUp

时间:2019-07-08 11:24:21

标签: android flutter dart gesture

我正在尝试创建一个当用户与按钮交互时发送http.get请求以控制机器人的应用程序。问题是当我按下按钮时,onTapDown被调用;但是,当我释放按钮时,不会调用OnTapUp。这是代码:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class Manual extends StatefulWidget {
  @override
  _ManualState createState() => _ManualState();
}

class _ManualState extends State<Manual> {

  double bot_speed = 0;

  Future<http.Response> changeBotSpeed(bot_speed) async {
    var url = 'http://192.168.43.91/manual_bot_speed/';
    var body = jsonEncode({"manual_bot_speed": bot_speed.toStringAsFixed(2)});

    return await http.post(url, headers: {"Content-Type": "application/json"}, body: body);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.max,
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[

          //Arrow Keys
          Container(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Container(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 15),
                  child: GestureDetector(
                    onTapDown: (details) {
                      print("Start Up!");
                      var move = http.get('http://192.168.43.91/forward/');
                    },

                    onTapUp: (details) {
                      print("End UP!");
                      var move = http.get('http://192.168.43.91/stop/');
                    },

                    child: OutlineButton(
                      shape: CircleBorder(),
                      onPressed: () {},
                      child: Icon(
                        Icons.keyboard_arrow_up,
                        size: 60,
                      ),
                    ),
                  ),
                ),
                Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    GestureDetector(
                      onTapDown: (details) {
                        print("Start Left!");
                        var move = http.get('http://192.168.43.91/left/');
                      },

                      onTapCancel: () {
                        print("END LEFT!");
                        var move = http.get('http://192.168.43.91/stop/');
                      },

                      child: OutlineButton(
                        shape: CircleBorder(),
                        onPressed: () {},
                        child: Icon(
                          Icons.keyboard_arrow_left,
                          size: 60,
                        ),
                      ),
                    ),
                    GestureDetector(
                      onTapDown: (details) {
                        print("Start RIGHT!");
                        var move = http.get('http://192.168.43.91/right/');
                      },

                      onTapUp: (details) {
                        print("END RIGHT");
                        var move = http.get('http://192.168.43.91/stop/');
                      },

                      child: OutlineButton(
                        shape: CircleBorder(),
                        onPressed: () {},
                        child: Icon(
                          Icons.keyboard_arrow_right,
                          size: 60,
                        ),
                      ),
                    ),
                  ],
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(0, 15, 0, 0),
                  child: GestureDetector(
                    onTapDown: (details) {
                      print("Start DOWN!");
                      var move = http.get('http://192.168.43.91/reverse/');
                    },

                    onTapCancel: () {
                      print("END DOWN");
                      var move = http.get('http://192.168.43.91/stop/');
                    },

                    child: OutlineButton(
                      shape: CircleBorder(),
                      onPressed: () {},
                      child: Icon(
                        Icons.keyboard_arrow_down,
                        size: 60,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),

          //Bot Speed
          Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                height: 20.0,
                width: 70.0,
                child: Text('Bot Speed: '),
              ),
              Container(
                width: 200,
                height: 48.0,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  mainAxisAlignment: MainAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Slider(
                      value: bot_speed,
                      onChanged: (value) {
                        setState(() {
                          bot_speed = value;
                        });
                        changeBotSpeed(value);
                      },
                    ),
                    Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(bot_speed.toStringAsFixed(2)),
                        Text('  m/s    '),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}

我已经有一段时间了,但是似乎找不到问题。

0 个答案:

没有答案