gpt4 book ai didi

flutter - 如何将参数传递给有状态的小部件?

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

我有以下有状态的小部件

class MyDiaryScreen2 extends StatefulWidget {
final AnimationController animationController;
MyDiaryScreen2({Key key, this.animationController}) : super(key: key);
@override
_MyDiaryScreen2State createState() => _MyDiaryScreen2State();
int index;
}

class _MyDiaryScreen2State extends State<MyDiaryScreen2>
with TickerProviderStateMixin {
Animation<double> topBarAnimation;

List<Widget> listViews = List<Widget>();
var scrollController = ScrollController();
double topBarOpacity = 1.0;
}

我从另一个窗口小部件将此窗口小部件称为
onTap: () {MyDiaryScreen2();},

但是我想像这样将参数类型为Inals的MealsView.index传递给MyDiaryScreen2
onTap: () {MyDiaryScreen2(MealsView.index);},

我该如何实现?

最佳答案

您只需在初始化MyDiaryScreen2时将其作为普通参数传递:

onTap: () => MyDiaryScreen2(
animationController: <controller>,
index: MealsView.index,
)

当然,不要忘记调整 MyDiaryScreen2以在构造函数中接受 index
class MyDiaryScreen2 extends StatefulWidget {
final AnimationController animationController;
final int index;

MyDiaryScreen2({Key key, this.animationController, this.index}) : super(key: key);

@override
_MyDiaryScreen2State createState() => _MyDiaryScreen2State();
}

关于flutter - 如何将参数传递给有状态的小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58234948/

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