简体   繁体   English

从无状态小部件调用有状态小部件的 setState

[英]Call a setState of a statefull widget from the stateless widget

I have a stateless widget class that has a widget whose movements need to be tracked.我有一个无状态小部件 class 有一个需要跟踪其运动的小部件。 I cannot keep this widget inside the stateful widgets as I don't want the state of this widget to be refreshed.我不能将此小部件保留在有状态小部件中,因为我不希望刷新此小部件的 state。

I have the following code.我有以下代码。

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),
      ),
    );
  }
}

This code produces the following output.此代码生成以下 output。 在此处输入图像描述

I want the direction and degree values to be changed as the joystick is moved.我希望在移动操纵杆时改变方向和度数。

Thank You.谢谢你。

I tried it myself and found the solution.我自己尝试过并找到了解决方案。 This can be done using streams.这可以使用流来完成。 I will post the code just in case someone needs it in the future.我将发布代码以防将来有人需要它。

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"),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM