'onTap doesn't work in a widget I created outside of the main file in flutter
I have three files: a widget.dart file where I create the widget that I named WalletItemMenu, another data_generator.dart file where I create a list of widgets that I have created and finally my main file main.dart where i use the list of WalletItemMenu created in data_generator.dart in a GridView.
widget.dart
InkWell WalletItemMenu(String titre,String img,MaterialColor? couleur){
return InkWell(
child: Container(
child: Card(
color: couleur,
child: InkWell(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Image.asset(img,height: 80),
SizedBox(height: 10),
Text(titre)
],
),
onTap: (){print("ok");},
),
),
),
onTap: (){print("ok");},
);
}
In data_generator.dart I create a list of widgets WalletItemMenu
data_generator.dart
List<Widget> listeWalletItems(){
List<Widget> list = [];
var el1 = WalletItemMenu("Achat des unités",Ozangue_porte_monaie,Colors.yellow);
list.add(el1);
var el2 = WalletItemMenu("Transfert de crédit",Ozangue_porte_monaie,Colors.yellow);
list.add(el2);
var el3 = WalletItemMenu("Achat article",Ozangue_porte_monaie,Colors.yellow);
list.add(el3);
var el4 = WalletItemMenu("Echange d'argent",Ozangue_porte_monaie,Colors.yellow);
list.add(el4);
return list;
}
In the main file I try to create a GridView Containing the list of WalletItemMenu that I created beforehand in data_generator.dart.
main.dart
class MenuPorteMonaiePage extends StatefulWidget{
@override
_MenuPorteMonaiePageState createState() => _MenuPorteMonaiePageState();
}
class _MenuPorteMonaiePageState extends State<MenuPorteMonaiePage>{
List<Widget> menus = [];
@override
void initState() {
// TODO: implement initState
super.initState();
menus = listeWalletItems();
}
@override
Widget build(BuildContext context){
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Container(
height: 500,
child: GridView.builder(
itemCount: menus.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemBuilder: (BuildContext context,int index){
return menus[index];
},
),
),
)
);
}
The problem is that onTap does not work. How can I solve the problem? Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
