【问题标题】:listview with navigator push to different screens not a single screen带有导航器的列表视图推送到不同的屏幕而不是单个屏幕
【发布时间】:2021-10-23 12:48:25
【问题描述】:

我有 listview,这是预期的行为:

  • 当我点击第一个列表视图项目时,我希望它导航到屏幕“A”,

  • 当我点击第二个列表视图项目时,我希望它导航到屏幕“C”,

  • 当我点击第三个列表视图项目时,我希望它导航到屏幕“T”,

  • 等等……

简而言之,当我单击每个列表视图项时,我希望它导航到不同的屏幕,因为我有不同的屏幕和不同的数据。

重要提示: “导航到单个屏幕”无处不在,但我没有获得有关如何导航到不同屏幕的帮助。在 youtube 和 google 文章上,每个人都以相同的方式上传,因为他们在一个屏幕上拥有相同类型的数据。就我而言,不同之处在于我有多个屏幕,每个屏幕都有自己独特的数据。

代码:

class _DashboardScreenState extends State<DashboardScreen> {
  final CategoriesScroller categoriesScroller = CategoriesScroller();
  ScrollController controller = ScrollController();
  bool closeTopContainer = false;
  double topContainer = 0;

  List<Widget> itemsData = [];

  void getPostsData() {
    List<dynamic> responseList = FOOD_DATA;
    List<Widget> listItems = [];
    responseList.forEach((post) {
      listItems.add(GestureDetector(
         onTap: () {
          _navigateAndDisplaySelection(context);
        },
        child: Container(

          height: 150,
          margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 10),
          decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(20.0)), color: Colors.white, boxShadow: [
            BoxShadow(color: Colors.black.withAlpha(100), blurRadius: 10.0),
          ]),
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10),
            child: Row(

              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      post["name"],
                      style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(
                      height: 10,
                    )
                  ],
                ),
                Image.asset(
                  "assets/images/${post["image"]}",
                  height: double.infinity,
                )
              ],
            ),
          )),));
    });
    setState(() {
      itemsData = listItems;
    });
  }

【问题讨论】:

标签: android flutter listview


【解决方案1】:

已编辑

你将通过context and item index

_navigateAndDisplaySelection(context, 1) // consider index 1

在_navigateAndDisplaySelection(context) 中,您将更改它,如下代码:

_navigateAndDisplaySelection(BuildContext context, int index){
// here you check the index and then route the navigation 

if(index==1){
//navigate add customer
}

if(index==2){
//navigate search customer
}

}

【讨论】:

  • void _navigateAndDisplaySelection(BuildContext context) async { // Navigator.push 返回一个在选择屏幕上调用 // Navigator.pop 后完成的 Future。 if(index==1){ //导航添加客户最终结果 = await Navigator.push( context, MaterialPageRoute(builder: (context) => SearchCustomerScreen()), ); }
  • 错误:未定义的名称“索引”。 ([flutter_auth] lib\Screens\Dashboard\DashboardScreen.dart:147 处的 undefined_identifier)
  • 您需要在_navigateAndDisplaySelection 中分配索引` 可以分享_navigateAndDisplaySelection 代码
  • 编辑了我的答案,请尝试理解这个概念然后去寻找代码
猜你喜欢
  • 2019-09-18
  • 2020-11-22
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多