Last active
May 12, 2021 23:44
-
-
Save IsmailAlamKhan/937ae347b0ef8ecd7eca96d5b309c7d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Material App', | |
home: Home(), | |
); | |
} | |
} | |
class Home extends StatefulWidget { | |
const Home({ | |
Key? key, | |
}) : super(key: key); | |
@override | |
_HomeState createState() => _HomeState(); | |
} | |
class _HomeState extends State<Home> { | |
final _selected = <MyClass>[]; | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Material App Bar'), | |
), | |
body: Center( | |
child: SizedBox( | |
width: 500, | |
child: GridView( | |
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( | |
crossAxisCount: 2, | |
), | |
children: [ | |
for (var item in _list) _build(item), | |
], | |
), | |
), | |
), | |
); | |
} | |
Widget _build(MyClass item) { | |
final isSelected = _selected.contains(item); | |
return Container( | |
height: 100, | |
width: 100, | |
color: isSelected ? Colors.blue : Colors.green, | |
child: InkWell( | |
onTap: () { | |
setState(() { | |
if (!isSelected) { | |
_selected.add(item); | |
} else { | |
_selected.remove(item); | |
} | |
}); | |
}, | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Image.network( | |
item.image, | |
), | |
), | |
), | |
); | |
} | |
} | |
class MyClass { | |
final String image; | |
MyClass(this.image); | |
} | |
final List<MyClass> _list = [ | |
MyClass( | |
'https://images.pexels.com/photos/7363245/pexels-photo-7363245.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260', | |
), | |
MyClass( | |
'https://images.pexels.com/photos/7558430/pexels-photo-7558430.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260', | |
), | |
MyClass( | |
'https://images.pexels.com/photos/1640777/pexels-photo-1640777.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260', | |
), | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment