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

问题描述 投票:0回答:1

我有以下有状态的小部件

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();},

但是我想将类型为int的MealsView.index参数传递给MyDiaryScreen2,就像这样

onTap: () {MyDiaryScreen2(MealsView.index);},

我该如何实现?

flutter dart
1个回答
0
投票

您只需在初始化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();
}
热门问题
推荐问题
最新问题