【问题标题】:Passing ListView.builder index data to a text widget将 ListView.builder 索引数据传递给文本小部件
【发布时间】:2021-07-05 16:37:07
【问题描述】:

所以我想要实现的是,使用ListView.builder,当用户点击特定的ListTile 项目时,它将打开一个AlertDialog 表单,在该表单的顶部,我想要显示一个Text 小部件,它将显示ListView.builder 的特定索引(在这种情况下,eventList list 内的String eventName)。

问题是,我不知道如何将该索引调用到ListView.builder 之外的文本小部件。 我正在努力实现的目标是可能的,还是有另一种方法可以做到这一点? 此外,如果它有帮助,当我尝试在文本小部件中传递索引时,我会收到错误“未定义的名称'索引'”。

关于我想要达到的目标的更多信息(请原谅我的笔迹): The AlertDialog Form

这是我的代码:

import 'package:flutter/material.dart';
import 'package:smc_app/Models/events.dart';

class EventsConfirmation extends StatefulWidget {
  const EventsConfirmation({Key? key}) : super(key: key);

  @override
  _EventsConfirmationState createState() => _EventsConfirmationState();
}
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
 List<EventList> schoolEvents = [
   EventList(eventName: 'SMC General Assembly', location: 'College Gym', time: '9AM-11AM',),
   EventList(eventName: 'College Finals Week', location: 'SMC', time: 'ALL DAY',),
   EventList(eventName: 'College Baccalaureate and Graduation Day', location: 'TBA', time: 'TBA',),
   EventList(eventName: 'Semester Break', location: 'N/A', time: 'N/A',),
];

    class _EventsConfirmationState extends State<EventsConfirmation> {
    
    
      Future<void> showInformationDialog(BuildContext context) async {
        return await showDialog(context: context,
            builder: (context){
          return AlertDialog(
            content: Form(
              child: Column(
                children: [
                  Text(schoolEvents[index].eventName), //HERE IS WHERE I'M HAVING PROBLEMS 
                  TextFormField(
                    validator: (val) => val!.isEmpty ? 'This field is required' : null,
                    decoration: InputDecoration(hintText: "Name/Student ID"),
                  ),
                ],
              ),
            ),
          );
          });
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Upcoming Events, Confirm Your Attendance.'),
          ),
          body: ListView.builder(
              itemCount: schoolEvents.length,
              itemBuilder: (context, index) {
                return Padding(
                  padding: const EdgeInsets.symmetric(vertical: 1.0, horizontal: 4.0),
                  child: Card(
                    child: InkWell(
                      child: ListTile(
                        onTap: () async {
                          await showInformationDialog(context);
                        },
                        title: Text(schoolEvents[index].eventName),
                        subtitle: Text(schoolEvents[index].location),
                      ),
                    ),
                  ),
                );
              }
          ),
        );
      }
    }

EventList 的类:

class EventList {

String eventName;
String location;
String time;

EventList({required this.eventName, required this.location, required this.time});
}

希望有人帮助我,谢谢。

【问题讨论】:

  • 您需要将索引传递给showInformationDialog()。这样的事情会起作用:将顶部的函数更改为Future&lt;void&gt; showInformationDialog(BuildContext context, int index),将 onTap 更改为await showInformationDialog(context,index);

标签: flutter dart listview


【解决方案1】:

我们有一些选项可以解决您的问题:

  1. 就像@Uni 评论的那样,您可以将索引作为参数传递给您的 showInformationDialog:

Future showInformationDialog(BuildContext context, int index) 异步

  1. 为了避免访问列表,我要做的是将 schoolEvent 作为参数传递,这样如果在加载屏幕后列表发生问题,它不会影响您的功能,我也会删除异步和等待,在这种情况下它们不是必需的。
Future<void> showInformationDialog(BuildContext context, EventList event) {
    return showDialog(
      context: context,
      builder: (context) {
        return AlertDialog(
          content: Form(
            child: Column(
              children: [
                Text(event.eventName),
                TextFormField(
                  validator: (val) => val!.isEmpty ? 'This field is required' : null,
                  decoration: InputDecoration(hintText: "Name/Student ID"),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

在列表磁贴上:

ListTile(
  onTap: () async {
    await showInformationDialog(context, schoolEvents[index]);
  },
  title: Text(schoolEvents[index].eventName),
  subtitle: Text(schoolEvents[index].location),
);

【讨论】:

  • 嗨,我尝试遵循您的第二个选项,但它在 Future&lt;void&gt; showInformationDialog(BuildContext context, SchoolEvent event) { 上给了我一个错误,其中指出“未定义的类 'SchoolEvent'。尝试将名称更改为现有类的名称,或创建一个名为“SchoolEvent”的课程。”,但@uni 的第一个选项有效。抱歉,我对编码很陌生,但仍在尝试理解这一切的逻辑哈哈
  • 哦,我的错误,我使用了错误的类名称,应该是 EventList,正在编辑它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 2020-05-01
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
相关资源
最近更新 更多