我有一张 map ,上面有这些labels
。
final Map<int, int> labels = {
1: 8,
2: 24,
3: 6,
4: 12,
5: 32,
6: 2,
7: 8,
8: 77,
};
可以使用变量“selected”来调用
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
小部件扩展为: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
。如何将选定的值称为
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();
}
}
最佳答案
在int selected
中添加变量Card1State
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(),
),