我怎样才能使用户在列表视图中创建的项目可点击到一个新页面中?in flutter

jjjwad0x  于 2023-01-06  发布在  Flutter
关注(0)|答案(1)|浏览(101)

嗨,我有一个页面,你可以在列表视图中创建一个项目,并给它一个名称,现在我想当你点击该项目,它去自己的专用页面上的应用程序栏上的名称.
https://i.stack.imgur.com/5jS0x.png
下面是列表视图代码:

import 'package:flutter/material.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:attendance/insideList.dart';

class lists extends StatefulWidget {
  const lists({super.key});

  @override
  State<lists> createState() => _listsState();
}

class _listsState extends State<lists> {
  List<String> _items = [];
  late TextEditingController _textController;
  @override
  void initState() {
    super.initState();
    _textController = TextEditingController();
  }

  @override
  void dispose() {
    _textController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _items.sort();
    return Scaffold(
      body: _items.length > 0
          ? ListView.separated(
              itemCount: _items.length,
              itemBuilder: (_, index) {
                return ListTile(
                  leading: const Icon(Icons.school),
                  trailing: const Icon(Icons.arrow_forward),
                  title: Center(child: Text('${_items[index]}')),
                  **//here the attempt I made that didnt work**
                  onTap: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: ((context) => InsideList(index))));
                  },
                  onLongPress: (() async {
                    await showDialog(
                        context: context,
                        builder: ((context) {
                          return AlertDialog(
                            title: const Text(
                              "Are you sure you want to delete this class?",
                              style: TextStyle(fontSize: 15),
                            ),
                            actions: [
                              TextButton(
                                  child: Text("cancel"),
                                  onPressed: (() {
                                    Navigator.pop(context);
                                  })),
                              TextButton(
                                child: Text('Delete'),
                                onPressed: () {
                                  setState(() {
                                    _items.removeAt(index);
                                    Navigator.pop(context);
                                  });
                                },
                              ),
                            ],
                          );
                        }));
                  }),
                );
              },
              separatorBuilder: (BuildContext context, int index) =>
                  const Divider(
                color: Colors.black,
              ),
            )
          : const Center(
              child: Text("You currently have no classes. Add from below."),
            ),
      floatingActionButton: SpeedDial(
        animatedIcon: AnimatedIcons.menu_arrow,
        spacing: 6,
        spaceBetweenChildren: 6,
        backgroundColor: const Color.fromARGB(255, 22, 37, 50),
        foregroundColor: const Color.fromARGB(255, 255, 255, 255),
        children: [
          SpeedDialChild(
              child: const Icon(Icons.group_add), label: "add student"),
          SpeedDialChild(
            child: const Icon(Icons.school),
            label: "add class",
            onTap: () async {
              final result = await showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    title: const Text('Add a new class'),
                    content: TextField(
                      controller: _textController,
                      autofocus: true,
                      decoration: const InputDecoration(
                          hintText: "Enter the name of the class."),
                    ),
                    actions: [
                      TextButton(
                        child: Text('Cancel'),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                      TextButton(
                        child: Text('Add'),
                        onPressed: () {
                          Navigator.pop(context, _textController.text);
                          _textController.clear();
                        },
                      ),
                    ],
                  );
                },
              );
              if (result != null) {
                result as String;
                setState(() {
                  _items.add(result);
                });
              }
            },
          )
        ],
      ),
    );
  }
}
    • 这是我为新页面创建的新dart文件:**
import 'package:flutter/material.dart';

class InsideList extends StatelessWidget {
  final int index;

  InsideList(this.index);

  List<String> _students = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("attendance"),
          centerTitle: true,
          backgroundColor: const Color.fromARGB(255, 22, 37, 50),
          toolbarHeight: 65,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.vertical(
              bottom: Radius.circular(30),
            ),
          ),
        ),
        body: _students.length > 0
            ? Center(child: Text("hi"))
            : Center(
                child:
                    Text("You currently have no students. Add from below.")));
  }
}
ix0qys7i

ix0qys7i1#

您可以传递项本身,而不是传递index,

onTap: () {
  Navigator.push(
    context,
    MaterialPageRoute(
      builder: ((context) => InsideList(_items[index])),
    ),
  );
},

还有

class InsideList extends StatelessWidget {
  final String name;
  InsideList(this.name); // use key constructor 
  List<String> _students = [];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("attendance  $name"),

相关问题