gpt4 book ai didi

flutter - 从无状态小部件调用到有状态小部件时,变量返回null吗?

转载 作者:行者123 更新时间:2023-12-03 04:56:43 25 4
gpt4 key购买 nike

我有一张 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(),
),

关于flutter - 从无状态小部件调用到有状态小部件时,变量返回null吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60463903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com