【问题标题】:flutter_gauge widget won't update index value on setStateflutter_gauge 小部件不会更新 setState 上的索引值
【发布时间】:2020-08-08 19:07:55
【问题描述】:

我遇到的问题是,虽然我通过 setState 成功更新了我的仪表 (FlutterGauge) 小部件的值,但小部件本身并未反映该更改。我知道重建正在进行,并且仪表小部件上的值确实正在更新。


void updateScore(bool isOnTopic) {
    //for the purposes of testing we won't use isOnTopic (its a hardcoded true anyway)
    setState(() {
      meterScore += 15;
    });
    print("Score Updated:");
    print("--------" + meterScore.toString() + "--------");
  }

  @override
  Widget build(BuildContext context) {
    print('+++++++We\'re building! Using this.meterScore value: ' +
        meterScore.toString() +
        '+++++++');

    //Replacing my FlutterGauge and return with the line below as a way to isolate the FlutterGauge widget as the problem
    //return Center(child: Text(this.meterScore.toString()));

    FlutterGauge meter = new FlutterGauge(
        circleColor: meterColor,
        secondsMarker: SecondsMarker.none,
        hand: Hand.short,
        number: Number.none,
        width: 200,
        index: meterScore,
        fontFamily: "Iran",
        counterStyle: TextStyle(color: Colors.black, fontSize: 35),
        counterAlign: CounterAlign.center,
        isDecimal: false);
    print("------------Accessing meter.index: " +
        meter.index.toString() +
        "----------------");
    return meter;
  }

我相当确定问题在于我如何使用来自 flutter_gauge 包的 FlutterGauge 小部件,因为当我用一个简单的 Text 小部件替换它并将我的值提供给它时​​,它会更新并将更新反映为预计。

既然如此,这里有一个flutter_gauge的链接供参考: https://pub.dev/packages/flutter_gauge#-readme-tab-

我对 Flutter 很陌生,这是我的第一个 stackoverflow 问题,如果我犯了任何明显的错误,请道歉。提前致谢!

以防我遗漏了您可能需要查看的重要代码,这是整个文件:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_gauge/flutter_gauge.dart';

class Meter extends StatefulWidget {
  Meter({Key key}) : super(key: key);
  MeterState createState() => MeterState();
}

class MeterState extends State<Meter> {
  @override
  initState() {
    super.initState();
  }

  double meterScore = 75;
  Color meterColor = Colors.green;

  void updateMeterColor() {
    setState(() {
      meterColor = Colors.green;
    });
  }

  void updateScore(bool isOnTopic) {
    //for the purposes of testing we won't use isOnTopic (its a hardcoded true anyway)
    setState(() {
      meterScore += 15;
    });
    print("Score Updated:");
    print("--------" + meterScore.toString() + "--------");
  }

  @override
  Widget build(BuildContext context) {
    print('+++++++We\'re building! Using this.meterScore value: ' +
        meterScore.toString() +
        '+++++++');

    //Replacing my FlutterGauge and return with the line below as a way to isolate the FlutterGauge widget as the problem
    //return Center(child: Text(this.meterScore.toString()));

    FlutterGauge meter = new FlutterGauge(
        circleColor: meterColor,
        secondsMarker: SecondsMarker.none,
        hand: Hand.short,
        number: Number.none,
        width: 200,
        index: meterScore,
        fontFamily: "Iran",
        counterStyle: TextStyle(color: Colors.black, fontSize: 35),
        counterAlign: CounterAlign.center,
        isDecimal: false);
    print("------------Accessing meter.index: " +
        meter.index.toString() +
        "----------------");
    return meter;
  }

  @override
  void dispose() {
    print("---------We are disposing (as intended)------------");
    super.dispose();
  }
}

编辑: 这是热重启和初次访问后的终端:

I/flutter (11573): +++++++We're building! Using this.meterScore value: 75.0+++++++
I/flutter (11573): ------------Accessing meter.index: 75.0----------------
I/flutter (11573): 75.0

调用一次函数:

I/flutter (11573): Score Updated:
I/flutter (11573): --------90.0--------
I/flutter (11573): +++++++We're building! Using this.meterScore value: 90.0+++++++
I/flutter (11573): ------------Accessing meter.index: 90.0----------------

我更新了代码 sn-ps(从所有 MeterScore 中删除了 this.,添加了针对函数未使用参数的注释)

我应该提到 updateScore 函数是在文件外部调用的。正如我所说,函数本身似乎可以正常工作,如打印语句所示。

这是我使用小部件的地方(这是整个文件):

class RecordingPage extends StatefulWidget {
  RecordingPage({Key key}) : super(key: key);

  _RecordingPageState createState() => _RecordingPageState();
}

class _RecordingPageState extends State<RecordingPage> {
  final GlobalKey<MeterState> meterState = GlobalKey<MeterState>();
  int yes = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: Container(
            width: this.yes * 10.0,
            child: FittedBox(
                child: FloatingActionButton(
              onPressed: null,
              backgroundColor: Colors.white,
            ))),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
        backgroundColor: Colors.white,
        appBar: offTopTitle,
        body: Column(
          children: <Widget>[
            Row(children: [
              Expanded(
                child: FlatButton(
                  child: Text("Go To Websocket"),
                  color: Colors.blue,
                  onPressed: () {
                    Navigator.pushNamed(context, WebsocketRoute);
                  },
                ),
              ),
            ]),
            Container(
              height: MediaQuery.of(context).size.height / 4,
              child: Image.asset('assets/placeholderWave.gif'),
            ),
            Container(
              margin: EdgeInsets.only(top: 5),
              height: MediaQuery.of(context).size.height / 4,
              child: Meter(key: meterState),
            ),
            Recorder((isOnTopic) {
              meterState.currentState.updateScore(isOnTopic);
            }),
          ],
        ),
        bottomNavigationBar: AppBarBuilder());
  }
}

【问题讨论】:

    标签: flutter dart setstate flutter-widget


    【解决方案1】:

    在一个新的颤振项目中玩弄了这个包之后,我无法在使用许多不同的方式创建它之后更新它,并且在查看了这个包的所有可用文档之后,我不确定它的意思以这种方式使用。没有在构建后更改仪表的示例,因此它似乎是一个不可变的小部件。

    我做了一些挖掘,syncfusion_flutter_gauges 看起来是一个很有前途的替代方案,this 文章讨论了如何做我认为您在项目中尝试的事情。希望对你有帮助

    【讨论】:

    • 感谢您的光临!我其实并不打算拥有这个。在任何meterScore上,我的错!我删除了它并更新了代码 sn-ps(很明显它没有效果)。我还在编辑中添加了打印。
    • 你能把代码贴在你实际使用小部件的地方,这样我就可以看到小部件树了吗?
    • 更新了我的帖子,你可以试试上面的,看看能不能解决它。
    • 是的,我的意思是在父窗口小部件 RecordingPage 中
    • 我相信要么是包装坏了,要么是在我自己的尝试和错误之后不打算以这种方式使用。我用另一种方法更新了我的答案,希望可以作为替代方法。
    【解决方案2】:

    我想我找到了解决方案。这对我有用。打开 flutter_gauge.dart 并删除所有注释部分。重新安装您的应用程序并刷新您的数据。这个步骤对我有用。

    【讨论】:

      猜你喜欢
      • 2019-08-31
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 2021-12-14
      相关资源
      最近更新 更多