-1

我有一个无状态小部件类,它有一个需要跟踪其运动的小部件。我不能将此小部件保留在有状态小部件中,因为我不希望刷新此小部件的状态。

我有以下代码。

import 'package:flutter/material.dart';
import 'package:control_pad/control_pad.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
    home: new MyHomePage(),
   );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          Expanded(
            child: JoystickView(
              onDirectionChanged: (degree, direction) {
                //Change the state here.
              },
            ),
          ),
          Expanded(
            child: MyStateFull(),
          ),
        ],
      ),
    );
  }
}

class MyStateFull extends StatefulWidget {
  @override
  _MyStateFullState createState() => _MyStateFullState();
}

class _MyStateFullState extends State<MyStateFull> {
  double degree = 10;
  double direction = 10;

  //Call this from the stateless Widget
  void changedDirection(degree, direction) {
    setState(() {
      this.degree = degree;
      this.direction = direction;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(
        "The degree Moved is $degree and the direction is $direction",
        style: TextStyle(fontSize: 25, color: Colors.black),
      ),
    );
  }
}

此代码产生以下输出。 在此处输入图像描述

我希望在移动操纵杆时改变方向和度数。

谢谢你。

4

1 回答 1

-1

我自己尝试过并找到了解决方案。这可以使用流来完成。我将发布代码以防将来有人需要它。

import 'package:flutter/material.dart';
import 'package:control_pad/control_pad.dart';

class MyStateLess extends StatelessWidget {
  StreamController<List<double>> _controller = StreamController<List<double>>();

  GlobalKey<_MyStateFullState> statefulKey = new GlobalKey<_MyStateFullState>();

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        JoystickView(
          onDirectionChanged: (degree, direction) {
            List<double> temp = new List<double>();
            temp.add(degree);
            temp.add(direction);
            _controller.add(temp);
          },
        ),
        MyStateFull(stream: _controller.stream, key: statefulKey),
      ],
    );
  }
}

class MyStateFull extends StatefulWidget {
  final Stream<List<double>> stream;
  MyStateFull({Key key, @required this.stream}) : super(key: key);

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

class _MyStateFullState extends State<MyStateFull> {
  double _degree = 0.0;
  double _direction = 0.0;

  @override
  void initState() {
    super.initState();
    widget.stream.listen((event) {
      setState(() {
        _degree = event[0];
        _direction = event[1];
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("$_degree, $_direction"),
    );
  }
}
于 2020-06-30T13:38:37.663 回答