Created
July 9, 2023 00:04
-
-
Save vvahyudi/268b120a31a6943a04f5501355ac4802 to your computer and use it in GitHub Desktop.
Puzzle Game - Result Page
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
| //resultpage.dart | |
| import 'package:flutter/material.dart'; | |
| class ResultPage extends StatefulWidget { | |
| final int num1; | |
| final int num2; | |
| const ResultPage({super.key, required this.num1, required this.num2}); | |
| @override | |
| _ResultPageState createState() => _ResultPageState(); | |
| } | |
| class _ResultPageState extends State<ResultPage> { | |
| List<String> imagePaths = []; | |
| List<String> generateImages(int count, String imageName) { | |
| return List.generate(count.abs(), (index) => 'images/$imageName'); | |
| } | |
| Widget buildDragTarget({required bool isLeft}) { | |
| return DragTarget<String>( | |
| builder: (context, candidateData, rejectedData) { | |
| return SizedBox( | |
| width: 100, | |
| height: 100, | |
| child: Center( | |
| child: Image.asset( | |
| isLeft | |
| ? 'images/blueplaceholder.png' | |
| : 'images/redplaceholder.png', | |
| width: 50, | |
| height: 50, | |
| ), | |
| ), | |
| ); | |
| }, | |
| onAccept: (imagePath) { | |
| // Update the position of the dragged block | |
| setState(() { | |
| imagePaths.remove(imagePath); | |
| if (isLeft) { | |
| imagePaths.insert(0, imagePath); | |
| } else { | |
| imagePaths.add(imagePath); | |
| } | |
| }); | |
| }, | |
| onWillAccept: (imagePath) => true, | |
| ); | |
| } | |
| List<Widget> buildImages() { | |
| List<Widget> imageWidgets = []; | |
| if (widget.num1 != 0) { | |
| final num1Images = | |
| generateImages(widget.num1, widget.num1 > 0 ? 'blue.png' : 'red.png'); | |
| imageWidgets.addAll( | |
| num1Images.map( | |
| (imagePath) => Draggable( | |
| feedback: Image.asset( | |
| imagePath, | |
| width: 50, | |
| height: 50, | |
| ), | |
| childWhenDragging: Container(), | |
| child: Image.asset( | |
| imagePath, | |
| width: 50, | |
| height: 50, | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| if (widget.num2 != 0) { | |
| final num2Images = | |
| generateImages(widget.num2, widget.num2 > 0 ? 'blue.png' : 'red.png'); | |
| imageWidgets.addAll( | |
| num2Images.map( | |
| (imagePath) => Draggable( | |
| feedback: Image.asset( | |
| imagePath, | |
| width: 50, | |
| height: 50, | |
| ), | |
| childWhenDragging: Container(), | |
| child: Image.asset( | |
| imagePath, | |
| width: 50, | |
| height: 50, | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| return [ | |
| Row( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: [ | |
| buildDragTarget(isLeft: true), // Left Placeholder | |
| const SizedBox(width: 20), | |
| buildDragTarget(isLeft: false), // Right Placeholder | |
| ], | |
| ), | |
| const SizedBox(height: 20), | |
| GridView.count( | |
| shrinkWrap: true, | |
| crossAxisCount: 4, | |
| mainAxisSpacing: 10, | |
| crossAxisSpacing: 10, | |
| children: imageWidgets, | |
| ), | |
| ]; | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Result'), | |
| ), | |
| body: SingleChildScrollView( | |
| child: Column( | |
| crossAxisAlignment: CrossAxisAlignment.center, | |
| children: [ | |
| const Padding( | |
| padding: EdgeInsets.all(20), | |
| child: Column( | |
| crossAxisAlignment: CrossAxisAlignment.center, | |
| children: [ | |
| Text( | |
| 'Placeholder Images:', | |
| style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), | |
| ), | |
| SizedBox(height: 10), | |
| // GridView.builder( | |
| // shrinkWrap: true, | |
| // itemCount: 4, | |
| // gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( | |
| // crossAxisCount: 2, | |
| // mainAxisSpacing: 10, | |
| // crossAxisSpacing: 10, | |
| // ), | |
| // itemBuilder: (context, index) { | |
| // return Image.asset( | |
| // 'images/placeholder.png', | |
| // width: 100, | |
| // height: 50, | |
| // fit: BoxFit.contain, | |
| // ); | |
| // }, | |
| // ), | |
| ], | |
| ), | |
| ), | |
| const SizedBox(height: 10), | |
| Padding( | |
| padding: const EdgeInsets.all(20), | |
| child: Column( | |
| crossAxisAlignment: CrossAxisAlignment.center, | |
| children: [ | |
| const Text( | |
| 'Block Images:', | |
| style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold), | |
| ), | |
| const SizedBox(height: 10), | |
| ...buildImages(), | |
| ], | |
| ), | |
| ), | |
| ], | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment