In this code Navigator.push is executing infinite times I am beginner flutter development and developing calculator app and implementing unit converter features in this app.
I created list of units that are displaying using gridview.builder method and display the ConverterCard, and when i added onTap functionality it execute this line of code infinite times that is causing performance issue and new widget is rebuilding again and again :
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
/// this line is is running infinite times
return ConverterScreen(appbarName: converterList[widget.index]['name']);
}));`
Please help me to fix that
import 'package:calculator_app/contants/convertor_list.dart';
import 'package:calculator_app/screen/convertor_screen.dart';
import 'package:flutter/material.dart';
class ConverterCard extends StatefulWidget {
const ConverterCard(
{super.key, required this.name, required this.icon, required this.index});
final int index;
final String name;
final IconData icon;
@override
State<ConverterCard> createState() => _ConverterCardState();
}
class _ConverterCardState extends State<ConverterCard> {
void onTapFunc() {
Navigator.of(context).push(MaterialPageRoute(builder: (context) {
/// this line is is running infinite times
return ConverterScreen(appbarName: converterList[widget.index]['name']);
}));
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {},
child: Card(
margin: const EdgeInsets.all(10),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Icon(
color: Theme.of(context).primaryColor,
widget.icon,
size: 30,
),
const SizedBox(
height: 10,
),
Text(
widget.name,
style: const TextStyle(
fontSize: 20,
),
),
],
),
),
),
);
}
}
Here is more description of code where is used builder method : `
import 'package:calculator_app/contants/convertor_list.dart';
// import 'package:calculator_app/screen/convertor_screen.dart';
// import 'package:calculator_app/screen/temp_screen.dart';
import 'package:calculator_app/widgets/convertor_card.dart';
import 'package:flutter/material.dart';
class ConvertorListScreen extends StatelessWidget {
const ConvertorListScreen({super.key});
@override
Widget build(BuildContext context) {
const List<Map<String, dynamic>> converterGrid = converterList;
return Scaffold(
appBar: AppBar(
title: const Text('Converters List'),
),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
int crossItemCount = constraints.maxWidth > 600 ? 3 : 2;
crossItemCount = constraints.maxWidth > 1200 ? 4 : crossItemCount;
return GridView.builder(
itemCount: converterGrid.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossItemCount,
),
itemBuilder: (context, index) {
return ConverterCard(
index: index,
name: converterGrid[index]['name'].toString(),
icon: converterGrid[index]['icon'],
);
},
);
},
),
);
}
}`