GestureDetector中StreamBuilder的问题

时间:2019-08-22 01:18:00

标签: flutter dart bloc

如果GestureDetector内部有StreamBuilder,则GestureDetector无法识别手势。

  

painter_bloc:

import 'dart:ui';

import 'dart:async';

class PainterBloc {
  final _controller = StreamController();

  get stream => _controller.stream;

  void fetch(Offset pos) {
    print("fetch --------");
    _controller.sink.add(pos);
  }

  void close() {
    _controller.close();
  }
}
  

main.dart:

import 'dart:ui';
import 'package:flutter/material.dart';
import 'services/painter_bloc.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _painterBloc = PainterBloc();
  var _offsetList = <Offset>[];
  double x = 200;

  @override
  Widget build(BuildContext context) {
    print('Build');
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Column(
          children: <Widget>[
            GestureDetector(
              child: StreamBuilder(
                stream: _painterBloc.stream,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    _offsetList.add(snapshot.data);
                    print(snapshot.data.toString());
                  }

                  print('StreamBuilder');
                  return CustomPaint(painter: CrossPainter(_offsetList));
                },
              ),
              onTap: () {
                print('>>>>>>> on tap');
              },
              onPanStart: (DragStartDetails details) {},
              onPanUpdate: (DragUpdateDetails details) {
                RenderBox object = context.findRenderObject();
                Offset _localPosition =
                    object.globalToLocal(details.globalPosition);
                _painterBloc.fetch(_localPosition);
                print('>>>>>>>>>> ' + details.localPosition.toString());
              },
              onPanEnd: (DragEndDetails details) {
                _painterBloc.fetch(null);
              },
            ),
            RaisedButton(
              child: Text('click'),
              onPressed: () {
                x = x + 1;
                _painterBloc.fetch(Offset(x, 400));
              },
            )
          ],
        ));
  }

  @override
  void dispose() {
    _painterBloc.close();
    super.dispose();
  }
}

class CrossPainter extends CustomPainter {
  Paint _paint;
  List<Offset> _offsetList;

  CrossPainter(this._offsetList) {
    _paint = Paint()
      ..color = Colors.blue
      ..strokeWidth = 10.0
      ..strokeCap = StrokeCap.round;
  }

  @override
  void paint(Canvas canvas, Size size) {
    canvas.drawLine(Offset(0.0, 0.0), Offset(size.width, size.height), _paint);

    canvas.drawLine(Offset(size.width, 0.0), Offset(0.0, size.height), _paint);
    canvas.drawPoints(PointMode.points, <Offset>[Offset(40, 40)], _paint);
    canvas.drawPoints(PointMode.points, _offsetList, _paint);
  }

  @override
  bool shouldRepaint(CrossPainter oldDelegate) {
    return false;
  }
}

为什么GestureDetector上的事件永远不会发生?

1 个答案:

答案 0 :(得分:0)

我认为您应该将 GestureDetector 包装在 StreamBuilder builder 中,例如:

    StreamBuilder(
                stream: _painterBloc.stream,
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    _offsetList.add(snapshot.data);
                    print(snapshot.data.toString());
                  }

                  print('StreamBuilder');
                  return GestureDetector(
                        onTap:(){
                          print('onTap');}
                        child:CustomPaint(painter: CrossPainter(_offsetList)),
                },
              ),