Flutter中的手势捕获异常

9w11ddsr  于 2023-01-09  发布在  Flutter
关注(0)|答案(1)|浏览(109)

我在Flutter中做一个待办事项列表,我目前正在研究路线,我遇到了一个名为"手势捕获异常"的问题,描述是"在_WidgetsAppState中找不到路线RouteSettings("/details","Note"示例)的生成器。",有人知道如何修复上述错误吗?
下面是我的代码:

import 'package:flutter/material.dart';
import 'package:todo_list/details.dart';
import 'package:todo_list/note.dart';

class MyApp extends StatelessWidget {
  final String text;
  final int number;
  final String listDescription;

  const MyApp(
      {super.key,
      required this.text,
      required this.number,
      required this.listDescription});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        DetailsPage.routeName: (ctx) => DetailsPage(text, listDescription),
      },
      home: CustomListTile(
        text: text,
        number: number,
        listDescription: listDescription,
      ),
    );
  }
}

class CustomListTile extends StatelessWidget {
  final String text;
  final int number;
  final String listDescription;

  const CustomListTile(
      {super.key,
      required this.text,
      required this.number,
      required this.listDescription});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        Navigator.pushNamed(context, DetailsPage.routeName,
            arguments: Note(title: text, description: listDescription));
      },
      /* onTap: () {
        Widget okButton = TextButton(
          child: const Text("CLOSE"),
          onPressed: () {
            Navigator.of(context).pop();
          },
        );

        AlertDialog alert = AlertDialog(
            title: Text(text),
            content: Text('This item in the list contains $listDescription'),
            actions: [
              okButton,
            ]);
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return alert;
            });
      }, */

      child: Padding(
        padding: const EdgeInsets.only(left: 20.0, right: 20.0, top: 20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("$number. $text",
                    style: const TextStyle(
                      fontSize: 20,
                    )),
                const Icon(Icons.chevron_right)
              ],
            ),
            Text(
              listDescription,
              style: const TextStyle(fontSize: 14, color: Colors.grey),
            ),
            const Divider()
          ],
        ),
      ),
    );
  }
}
import 'package:flutter/material.dart';

import 'note.dart';

class DetailsPage extends StatefulWidget {
  static const String routeName = "/details";
  final String text;
  final String listDescription;

  const DetailsPage(this.text, this.listDescription, {super.key});

  @override
  State<DetailsPage> createState() => _DetailsPageState();
}

class _DetailsPageState extends State<DetailsPage> {
  late Note params;
  @override
  void didChangeDependencies() {
    params = ModalRoute.of(context)!.settings.arguments! as Note;
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    Widget titleSection = Container(
      padding: const EdgeInsets.all(32),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: const EdgeInsets.only(bottom: 0),
                  child: Text(
                    params.title,
                    style: const TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 25,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );

    Color color = Theme.of(context).primaryColor;

    Widget buttonSection = Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        _buildButtonColumn(
          color,
          Icons.edit,
          'EDIT',
        ),
        _buildButtonColumn(color, Icons.delete, 'DELETE'),
      ],
    );

    Widget textSection = Padding(
      padding: const EdgeInsets.all(20),
      child: Text(
        params.description,
        softWrap: true,
      ),
    );

    return MaterialApp(
      title: 'Layout for a New Screen',
      theme: ThemeData(
        primarySwatch: Colors.brown,
      ),
      home: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: const Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
          title: Text(params.title),
        ),
        body: ListView(
          children: [
            Image.asset(
              'lib/images/placeholder.jpg',
              width: 600,
              height: 240,
              fit: BoxFit.cover,
            ),
            titleSection,
            buttonSection,
            textSection,
          ],
        ),
      ),
    );
  }

  Column _buildButtonColumn(
    Color color,
    IconData icon,
    String label,
  ) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Icon(icon, color: color),
        Container(
          margin: const EdgeInsets.only(top: 8),
          child: Text(
            label,
            style: TextStyle(
              fontSize: 12,
              fontWeight: FontWeight.w400,
              color: color,
            ),
          ),
        ),
      ],
    );
  }
}

    /* return Scaffold(
        appBar: AppBar(title: Text(text)),
        body: Center(
          child: Row(
            children: [Text(description)],
          ),
        ));
  }
} */

我以前尝试过另一条路线,它成功地工作了,但这一次,我设置的路线需要我有一个论点,这对我来说是新的,因此我正在经历的错误对我来说是新的,我不知道如何修复或什么应该是解决方案。

b1zrtrql

b1zrtrql1#

  • 您必须使用脚手架小部件。
  • 这是演示
import 'package:flutter/material.dart';

void main(List<String> args) {
  runApp(MyApp(
    listDescription: '????',
    number: 1,
    text: 'note',
  ));
}

class MyApp extends StatelessWidget {
  final String text;
  final int number;
  final String listDescription;

  const MyApp(
      {super.key,
      required this.text,
      required this.number,
      required this.listDescription});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        routes: {
          DetailsPage.routeName: (ctx) => DetailsPage(text, listDescription),
        },
        home: Scaffold(
          body: CustomListTile(
            text: text,
            number: number,
            listDescription: listDescription,
          ),
        ));
  }
}

class CustomListTile extends StatelessWidget {
  final String text;
  final int number;
  final String listDescription;

  const CustomListTile(
      {super.key,
      required this.text,
      required this.number,
      required this.listDescription});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        Navigator.pushNamed(context, DetailsPage.routeName,
            arguments: Note(title: text, description: listDescription));
      },
      /* onTap: () {
        Widget okButton = TextButton(
          child: const Text("CLOSE"),
          onPressed: () {
            Navigator.of(context).pop();
          },
        );

        AlertDialog alert = AlertDialog(
            title: Text(text),
            content: Text('This item in the list contains $listDescription'),
            actions: [
              okButton,
            ]);
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return alert;
            });
      }, */

      child: Padding(
        padding: const EdgeInsets.only(left: 20.0, right: 20.0, top: 20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("$number. $text",
                    style: const TextStyle(
                      fontSize: 20,
                    )),
                const Icon(Icons.chevron_right)
              ],
            ),
            Text(
              listDescription,
              style: const TextStyle(fontSize: 14, color: Colors.grey),
            ),
            const Divider()
          ],
        ),
      ),
    );
  }
}

class DetailsPage extends StatefulWidget {
  static const String routeName = "/details";
  final String text;
  final String listDescription;

  const DetailsPage(this.text, this.listDescription, {super.key});

  @override
  State<DetailsPage> createState() => _DetailsPageState();
}

class _DetailsPageState extends State<DetailsPage> {
  late Note params;
  @override
  void didChangeDependencies() {
    params = ModalRoute.of(context)!.settings.arguments! as Note;
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    Widget titleSection = Container(
      padding: const EdgeInsets.all(32),
      child: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: const EdgeInsets.only(bottom: 0),
                  child: Text(
                    params.title,
                    style: const TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 25,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );

    Color color = Theme.of(context).primaryColor;

    Widget buttonSection = Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        _buildButtonColumn(
          color,
          Icons.edit,
          'EDIT',
        ),
        _buildButtonColumn(color, Icons.delete, 'DELETE'),
      ],
    );

    Widget textSection = Padding(
      padding: const EdgeInsets.all(20),
      child: Text(
        params.description,
        softWrap: true,
      ),
    );

    return MaterialApp(
      title: 'Layout for a New Screen',
      theme: ThemeData(
        primarySwatch: Colors.brown,
      ),
      home: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: const Icon(Icons.arrow_back),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
          title: Text(params.title),
        ),
        body: ListView(
          children: [
            Container(
              width: 600,
              height: 240,
              color: Colors.amber,
            ),
            titleSection,
            buttonSection,
            textSection,
          ],
        ),
      ),
    );
  }

  Column _buildButtonColumn(
    Color color,
    IconData icon,
    String label,
  ) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Icon(icon, color: color),
        Container(
          margin: const EdgeInsets.only(top: 8),
          child: Text(
            label,
            style: TextStyle(
              fontSize: 12,
              fontWeight: FontWeight.w400,
              color: color,
            ),
          ),
        ),
      ],
    );
  }
}

class Note {
  String title;
  String description;
  Note({required this.title, required this.description});
}

相关问题