GridView In Flutter

GridView In Flutter :

Screenshot :

GridView In Flutter

Program :

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

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

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

class _HomePageState extends State<HomePage> {
  final List<IconData> _listIcons = [
    Icons.offline_bolt,
    Icons.ac_unit,
    Icons.dashboard,
    Icons.backspace,
    Icons.cached,
    Icons.edit,
    Icons.face,
  ];

  List<IconData> _selectedIcons = [];

  @override
  Widget build(BuildContext context) {
    Widget gridViewSelection = GridView.count(
      childAspectRatio: 2.0,
      crossAxisCount: 3,
      mainAxisSpacing: 20.0,
      padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 20.0),
      children: _listIcons.map((iconData) {
        return GestureDetector(
          onTap: () {
            _selectedIcons.clear();

            setState(() {
              _selectedIcons.add(iconData);
            });
          },
          child: GridViewItem(iconData, _selectedIcons.contains(iconData)),
        );
      }).toList(),
    );

    return Scaffold(
      appBar: AppBar(
        title: Text('GridView'),
      ),
      body: gridViewSelection,
    );
  }
}

class GridViewItem extends StatelessWidget {
  final IconData _iconData;
  final bool _isSelected;

  GridViewItem(this._iconData, this._isSelected);

  @override
  Widget build(BuildContext context) {
    return RawMaterialButton(
      child: Icon(
        _iconData,
        color: Colors.white,
      ),
      shape: CircleBorder(),
      fillColor: _isSelected ? Colors.blue : Colors.black,
      onPressed: null,
    );
  }
}