我目前正在编写一个应用程序,我有一些页面路由问题。我使用的是RouteGenerator
,它工作得很好,除了在这种情况下。
如果我尝试导航到第二个屏幕,我收到一个错误说:
如果有人能帮我的话,我会很感激的。未定义名称'context'。试着把名字改过来已定义,或定义名称。
下面是我的代码(项目类在不同的文件中):
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: (){},
)
];
让你的类有状态,并尝试使用initstate
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> {
List<Project> demo_projects= [];
@override
void initState() {
// TODO: implement 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(),
],
)),
);
}
}
你得到这个错误是因为你没有在任何地方定义变量context,并试图传递它。
List<Project> demo_projects = [
Project(
title: "Test",
description:
"Test",
onTap: (){Navigator.pushNamed(**context**, '/second');},
),
通常,在您的小部件的构建方法中有一个上下文。
@override
Widget build(BuildContext context) {
}
你没有提供完整的代码片段,所以我不知道完整的实现。
尝试声明上下文变量,希望对您有所帮助。
class Project {
final BuildContext context;
final String title, description;
void Function() onTap;
Project({
this.title,
this.context,
this.description,
@required this.onTap,
});
}
BuildContext context;
List<Project> demo_projects = [
Project(
context: context,
title: "Test",
description: "Test",
onTap: () {
Navigator.pushNamed(context, '/second');
},
),
];