flutter 将两个不同类型的对象传递到一个页面

omhiaaxx  于 2023-01-14  发布在  Flutter
关注(0)|答案(1)|浏览(184)

如何将两个不同的对象类型传递到单个页面,以便我可以忽略所需的任何一个:
发件人:

onTap: () {
Navigator.push(
context, PageTransition(
alignment: Alignment.bottomRight,
type: PageTransitionType.fade,
child: CategoryDetail(
topCategories: categories[i])));
}

OR

Navigator.of(context).push(PageTransition(
alignment: Alignment.bottomRight,
type: PageTransitionType.fade,
child: CategoryDetail(category: categories[i])));

收件人:

class CategoryDetail extends StatefulWidget {
CategoryDetail({Key? key, required this.topCategories, required this.category})
  : super(key: key);

final Category category;
final TopCategories topCategories;

@override
State<CategoryDetail> createState() => _CategoryDetailState();
}
qnakjoqk

qnakjoqk1#

可以创建一个变量而不定义类型

final category;

并在小部件中使用此行检查类别值是否为Category或TopCategories

if(category is TopCategories){

  }else if(category is Category){

  }

相关问题