我目前正在编写一个应用程序,但我在页面路由方面遇到了一些问题。我正在使用 a RouteGenerator
,除了在这种情况下,它工作得很好。
如果我尝试导航到第二个屏幕,我会收到一条错误消息:
未定义的名称“上下文”。尝试将名称更正为已定义的名称,或定义名称。
如果有人可以帮助我,我将不胜感激。
这是我的代码(项目类在不同的文件中):
import 'package:flutter/material.dart';
class ProjectCard extends StatefulWidget {
const ProjectCard({
Key? key,
required this.project,
}) : super(key: key);
final Project project;
@override
_ProjectCardState createState() => _ProjectCardState();
}
class _ProjectCardState extends State<ProjectCard> {
// ignore: non_constant_identifier_names
List<Project> demo_projects = [];
@override
void initState() {
super.initState();
demo_projects.add(Project(title: 'Test', description: 'Test', onTap: () {Navigator.pushNamed(context, '/second');}),);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {},
child: Container(
decoration: BoxDecoration(
color: secondaryColor,
borderRadius: BorderRadius.all(Radius.circular(30))),
padding: const EdgeInsets.all(defaultPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
widget.project.title!,
maxLines: 2,
overflow: TextOverflow.ellipsis,
style: TextStyle(fontSize: 20, color: primaryColor),
),
Spacer(),
Text(
widget.project.description!,
maxLines: Responsive.isMobileLarge(context) ? 3 : 4,
overflow: TextOverflow.ellipsis,
style: TextStyle(height: 1.5),
),
Spacer(),
],
)),
);
}
}
import 'package:flutter/material.dart';
class ProjectsGridView extends StatelessWidget {
const ProjectsGridView({
Key? key,
this.crossAxisCount = 3,
this.childAspectRatio = 1.5,
}) : super(key: key);
final int crossAxisCount;
final double childAspectRatio;
@override
Widget build(BuildContext context) {
return GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: demo_projects.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
childAspectRatio: childAspectRatio,
crossAxisSpacing: defaultPadding,
mainAxisSpacing: defaultPadding,
),
itemBuilder: (context, index) => ProjectCard(
project: demo_projects[index],
),
);
}
}
class Project {
final String? title, description;
void Function () onTap;
Project({this.title, this.description, required this.onTap});
}
// ignore: non_constant_identifier_names
List<Project> demo_projects = [
Project(
title: "Test",
description:
"Test",
onTap: (){},
)
];