我有一个映射,它有这些labels
。
final Map<int, int> labels = {
1: 8,
2: 24,
3: 6,
4: 12,
5: 32,
6: 2,
7: 8,
8: 77,
};
可以使用变量"选定"调用
class Score extends StatelessWidget
{
final int selected;
Score(this.selected);
@override
Widget build(BuildContext context) {
return Text('You're score is ${labels[selected]}',
style: TextStyle(
fontStyle: FontStyle.normal,
fontSize: 24.0,
fontWeight: FontWeight.w500,
color: Colors.white,
fontFamily: "Netflix"));
}
}
它连接到另一个类Card1中的StreamBuilder
,该类作为Stateful
Widget扩展为:
class Card1 extends StatefulWidget
{
@override
Card1({Key key}) : super(key: key);
Card1State createState() => Card1State();
}
class Card1State extends State<Card1>
{
@override
Widget build(BuildContext context) {
return ... //other code,
StreamBuilder(
stream: _dividerController.stream,
builder: (context, snapshot) => snapshot.hasData
? Score(snapshot.data)
: Container(),
),
}
我的问题是,我不能将selected
的值从Score
类调用到Card1
类,并且它总是返回null
。
如何将selected的值调用到Card1
类?
编辑:这是我的完整源代码
import 'dart:async';
import 'dart:math';
import 'package:flutter_spinning_wheel/flutter_spinning_wheel.dart';
import 'package:flutter/material.dart';
final Map<int, int> labels = {
1: 8,
2: 24,
3: 6,
4: 12,
5: 32,
6: 2,
7: 8,
8: 77,
};
class Card1 extends StatefulWidget {
@override
Card1({Key key}) : super(key: key);
Card1State createState() => Card1State();
}
class Card1State extends State<Card1> {
String spinReward;
final StreamController _dividerController = StreamController<int>();
final _wheelNotifier = StreamController<double>();
double _generateRandomVelocity() => (Random().nextDouble() * 6000) + 2000;
double _generateRandomAngle() => Random().nextDouble() * pi * 2;
@override
void dispose() {
_dividerController.close();
_wheelNotifier.close();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Material(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [const Color(0xFFFFFFF), const Color(0xFFFFFFF)],
begin: Alignment.centerLeft,
end: Alignment.centerRight,
),
),
child: WillPopScope(
onWillPop: () async {
return true;
},
child: Scaffold(
backgroundColor: Colors.transparent,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SpinningWheel(
Image.asset('assets/app/Card1-8-300.png'),
width: 350,
height: 350,
initialSpinAngle: _generateRandomAngle(),
spinResistance: 0.6,
canInteractWhileSpinning: false,
dividers: 8,
onUpdate: _dividerController.add,
onEnd: _dividerController.add,
secondaryImage:
Image.asset('assets/app/Card1-center-300.png'),
secondaryImageHeight: 110,
secondaryImageWidth: 110,
shouldStartOrStop: _wheelNotifier.stream,
),
SizedBox(height: 30),
StreamBuilder(
stream: _dividerController.stream,
builder: (context, snapshot) =>
snapshot.hasData ? Score(snapshot.data) : Container(),
),
SizedBox(height: 20),
RaisedButton(
child: Text(
"SPIN NOW!",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontFamily: "Netflix",
fontWeight: FontWeight.bold,
),
),
color: Color(0xFFFE524B),
onPressed: () {
spinWheel();
},
),
],
),
),
),
),
),
);
}
spinWheel() {
Timer(Duration(seconds: 5), () {
//labels[selected] is null
spinReward = "${labels[selected]}";
print(spinReward);
});
_wheelNotifier.sink.add(_generateRandomVelocity());
}
}
class Score extends StatelessWidget {
//This is the value of selected I want to call in my other class
final int selected;
Score(this.selected);
@override
Widget build(BuildContext context) {
return Container();
}
}
在Card1State
中添加变量int selected
class Card1State extends State<Card1>
{
int selected;
}
在创建Score
时设置所选值
@override
Widget build(BuildContext context) {
return ... //other code,
StreamBuilder(
stream: _dividerController.stream,
builder: (context, snapshot) => snapshot.hasData
? Score(selected = snapshot.data)
: Container(),
),