'Flutter - Prevent rebuild liview.builder items

I have a ListView.builder like this:

class DStanding extends StatefulWidget {
  const DStanding({Key key}) : super(key: key);

  @override
  _DStandingState createState() => _DStandingState();
}

class _DStandingState extends State<DStanding> {
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
              padding: const EdgeInsets.fromLTRB(20.0, 5.0, 10.0, 5.0),
              child: Text(
                  AppLocalizations.of(context).translate('standing'),
                  style: const TextStyle(color: Colors.white, fontSize: 30))),
          Flexible(
            child: ListView.builder(
              shrinkWrap: true,
              itemBuilder: (context, index) {
                return GestureDetector(
                    onTap: () => showModalBottomSheet(
                        context: context,
                        builder: (context) {
                          return BottomInfoTest(...);
                        }),
                    child: TestCard(...));
              },
              itemCount: 10,
            ),
          ),
        ],
      ),
    );
  }
}

TestCard code:

class TestCard extends StatelessWidget {

  const TestCard(
      {Key key}): super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
    ....);
  }
}

But every time I scroll up and down the list, Flutter rebuilds all the items/cards.

How can I improve my code to prevent Flutter from rebuilding the elements every time I scroll through the list?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source