Working with StaggeredGridView, I cannot pass an additional instance like the viewModel below to a Material class, this is the error I get:
Error: The getter 'viewModel' isn't defined for the class '_MainPageState'.
Material Items(IconData icon, String heading, Color cColor) {
return Material(
child: InkWell(
onTap: () {
if (heading ==
('Text')) {
showMainDialog(context, viewModel); // <- The error
} else if (heading == /* ^^^^^^^^^ */
('Text1')) {
showMainDialog(context, viewModel); // <- The error
} /* ^^^^^^^^^ */
},
child: Center(
child: Container(
child:
Column(
children: <Widget>[
Expanded(
child: Container(
child: Icon(
icon,
color: cColor,
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.only(top:10),
child: Text(
heading,
softWrap: true,
),
),
)
],
)
),
),
));
}
Widget loadMainPage(ViewModel viewModel) {
return StaggeredGridView.count(
shrinkWrap: true,
crossAxisCount: 2,
crossAxisSpacing: 12.0,
mainAxisSpacing: 12.0,
children: <Widget>[
Items(
Icons.add,
('Text'),
Colors.green),
Items(
Icons.add,
('Text1'),
Colors.red),
],
staggeredTiles: [
StaggeredTile.extent(1, 150.0),
StaggeredTile.extent(1, 150.0),
],
);
}
Problem:
The problem is that you're attempting to use the
viewModelvariable in theItemsmethod but theviewModelvariable is not available to theItemsmethod.The
viewModelvariable is available in theloadMainPagemethod but it is not being passed into theItemsmethod.Solution:
You need to:
ViewModelobject as a parameter for theItemsmethod andviewModelvariable to theItemsmethod used in yourloadMainPagemethod.Update the
Itemsmethod to this:And update the
loadMainPagemethod to this: