This dart project it is showing an error that context is undefined and when i sent it as an argument it is showing error i am new to flutter and my mentor wrote the same(as i have seen) i am facing this problem
import 'package:flutter/material.dart';
class Category_item extends StatelessWidget {
final String title;
final Color color;
Category_item(this.title, this.color);
void selectCategory() {
Navigator.of(context);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: selectCategory,
splashColor: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(15), //wave ripple waves
child: Container(
padding: const EdgeInsets.all(15),
child: Text(
title,
style: Theme.of(context).textTheme.headline6,
),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
color.withOpacity(0.7),
color,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(15),
),
),
);
}
}
CodePudding user response:
void selectCategory() {
Navigator.of(context);
}
this function is outside the build. try this:
import 'package:flutter/material.dart';
class Category_item extends StatelessWidget {
final String title;
final Color color;
Category_item(this.title, this.color);
void selectCategory() {
Navigator.of(context);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: selectCategory,
splashColor: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(15), //wave ripple waves
child: Container(
padding: const EdgeInsets.all(15),
child: Text(
title,
style: Theme.of(context).textTheme.headline6,
),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
color.withOpacity(0.7),
color,
],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
borderRadius: BorderRadius.circular(15),
),
),
);
}
}
CodePudding user response:
Try this-
selectCategory(BuildContext context) {
Navigator.of(context);
}
CodePudding user response:
The function needs to have an input for the context in order for it to know what the context is.
void selectCategory(BuildContext context) {
Navigator.of(context);
}
And then call it like this:
onTap: () => selectCategory(context),