我遇到的问题是,尽管我通过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-
我很不熟悉,这是我的第一个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----------------
我更新了代码段(从所有meterScore中删除了此代码段,并添加了注释以解决函数未使用的参数)
我可能应该提到在文件外部调用了updateScore函数。正如我所说,该函数本身似乎可以正常运行,如print语句所示。
这是我使用小部件的地方(这是整个文件):
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());
}
}
答案 0 :(得分:2)
在新的flutter项目中尝试使用该软件包后,我无法使用多种不同方式在创建后对其进行更新,并且在查看了该软件包的所有可用文档之后,我不确定这是否意味着以这种方式使用。在构建仪表之后,没有任何更改仪表的示例,因此它似乎是一个不可变的小部件。
我做了一些挖掘工作,syncfusion_flutter_gauges看起来是一个很有前途的选择,而this文章介绍了如何做我认为您在项目中尝试的事情。希望对您有帮助
答案 1 :(得分:2)
我想我找到了解决方案。这对我有用。打开flutter_gauge.dart并删除所有注释的部分。重新安装您的应用并刷新数据。此步骤对我有用。