ListView With Like – Unlike In Flutter

ListView With Like – Unlike In Flutter : this flutter tutorial is to design listview with like – unlike in flutter.in this flutter tutorial first we create the listview which contain multiple list items.here item 0,item 1…. is a list item then we set the onPress for the list items.if the list item is pressed to set for the like it is mark as a favorite.here is a small example of listview with like -unlike in flutter.study this flutter tutorial to know more about it

Screenshot :

ListView With Like - Unlike In Flutter

ListView With Like – Unlike In Flutter

Program :

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'ListView Like - Unlike'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<Item> _itemList;

  @override
  void initState() {
    super.initState();

    _itemList = List<Item>();
    for (int i = 0; i < 10; i++) {
      _itemList.add(Item(
        title: 'Item $i',
        isFavorite: false,
      ));
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new ListView.builder(
          itemCount: _itemList.length,
          itemBuilder: (context, index) {
            return ListItem(
              _itemList[index],
              () => onFavoritePressed(index),
            );
          },
        ));
  }

  onFavoritePressed(int index) {
    final item = _itemList[index];
    setState(() {
      item.isFavorite = !item.isFavorite;
    });
  }
}

class ListItem extends StatelessWidget {
  ListItem(this.item, this.onFavoritePressed);

  final Item item;
  final VoidCallback onFavoritePressed;

  @override
  Widget build(BuildContext context) {
    return new GestureDetector(
        onTap: (() {onFavoritePressed();}),
        child: ListTile(
            title: Text(item.title),
            leading: IconButton(
              icon: Icon(
                  item.isFavorite ? Icons.favorite : Icons.favorite_border),
            )));
  }
}

class Item {
  Item({this.title, this.isFavorite});

  String title;
  bool isFavorite;
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in