import 'dart:async'; import 'package:flutter/material.dart'; import 'package:memory/data/data.dart'; import 'package:memory/models/TileModel.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', debugShowCheckedModeBanner: false, theme: ThemeData( primarySwatch: Colors.blue, ), home: Home(), ); } } class Home extends StatefulWidget { @override _HomeState createState() => _HomeState(); } class _HomeState extends State<Home> { List<TileModel> gridViewTiles = []; List<TileModel> questionPairs = []; @override void initState() { super.initState(); reStart(); } void reStart() { myPairs = getPairs(); myPairs.shuffle(); gridViewTiles = myPairs; Future.delayed(const Duration(seconds: 3), () { setState(() { questionPairs = getQuestionPairs(); gridViewTiles = questionPairs; selected = false; }); }); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, body: SingleChildScrollView( child: Container( padding: EdgeInsets.symmetric(horizontal: 20, vertical: 50), child: Column( children: <Widget>[ SizedBox( height: 40, ), points != 800 ? Column( crossAxisAlignment: CrossAxisAlignment.center, children: <Widget>[ Text( "$points/800", style: TextStyle( fontSize: 20, fontWeight: FontWeight.w500, ), ), Text( "Points", textAlign: TextAlign.start, style: TextStyle( fontSize: 14, fontWeight: FontWeight.w300, ), ), ], ) : Container(), SizedBox( height: 20, ), points != 800 ? GridView( shrinkWrap: true, //physics: ClampingScrollPhysics(), scrollDirection: Axis.vertical, gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent( mainAxisSpacing: 0.0, maxCrossAxisExtent: 100.0, ), children: List.generate(gridViewTiles.length, (index) { return Tile( imagePathUrl: gridViewTiles[index].getImageAssetPath(), tileIndex: index, parent: this, ); }), ) : Container( child: Column( children: <Widget>[ GestureDetector( onTap: () { setState(() { points = 0; reStart(); }); }, child: Container( height: 50, width: 200, alignment: Alignment.center, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(24), ), child: Text( "Replay", style: TextStyle( color: Colors.white, fontSize: 17, fontWeight: FontWeight.w500, ), ), ), ), ], ), ), ], ), ), ), ); } } class Tile extends StatefulWidget { final String imagePathUrl; final int tileIndex; final _HomeState? parent; Tile({this.imagePathUrl = '', this.tileIndex = 0, this.parent}); @override _TileState createState() => _TileState(); } class _TileState extends State<Tile> { @override Widget build(BuildContext context) { return GestureDetector( onTap: () { if (!selected) { setState(() { myPairs[widget.tileIndex].setIsSelected(true); }); if (selectedTile != "") { /// testing if the selected tiles are same if (selectedTile == myPairs[widget.tileIndex].getImageAssetPath()) { print("add point"); points = points + 100; print(selectedTile + " -> " + widget.imagePathUrl); TileModel tileModel = new TileModel(); print(widget.tileIndex); selected = true; Future.delayed(const Duration(seconds: 2), () { tileModel.setImageAssetPath(""); myPairs[widget.tileIndex] = tileModel; print(selectedIndex); myPairs[selectedIndex] = tileModel; this.widget.parent?.setState(() {}); setState(() { selected = false; }); selectedTile = ""; }); } else { print(selectedTile + " -> " + myPairs[widget.tileIndex].getImageAssetPath()); print("wrong choice"); print(widget.tileIndex); print(selectedIndex); selected = true; Future.delayed(const Duration(seconds: 2), () { this.widget.parent?.setState(() { myPairs[widget.tileIndex].setIsSelected(false); myPairs[selectedIndex].setIsSelected(false); }); setState(() { selected = false; }); }); selectedTile = ""; } } else { setState(() { selectedTile = myPairs[widget.tileIndex].getImageAssetPath(); selectedIndex = widget.tileIndex; }); print(selectedTile); print(selectedIndex); } } }, child: Container( margin: EdgeInsets.all(5), child: myPairs[widget.tileIndex].getImageAssetPath() != "" ? Image.asset(myPairs[widget.tileIndex].getIsSelected() ? myPairs[widget.tileIndex].getImageAssetPath() : widget.imagePathUrl) : Container( color: Colors.white, child: Image.asset("assets/correct.png"), ), ), ); } }