0

Any help is appreciated, as I have tried to figure this out and can not. Im making a game that displays about 20 tiles randomly for each new game. I currently have a "List" widget with all 20 tiles as "Flatbutton" widgets. Then I randomize them with a "randomNumGen..shuffle" function and display them in random order inside of a "SliverGrid" (with 5 across, making 4 rows). I then want the user to click on the one they want, and then click on the spot (an empty Slivergrid below) that he wants to place that tile in (it's the player's home base).

The problem is that I can't figure out how to associate specific "data" with each tile. For example..

Tile 1 = 5 energy and 2 people.
Tile 2 = 3 energy and 4 people
Tile 3 = 4 energy and 3 people.... and so on for all 20 tiles.

Once they become randomized, I cant figure out how to 'give' that data to the user who selected that tile, so that the user receives '5 energy and 2 people' if they selected Tile 1 (which is in a random spot).

import 'package:flutter/material.dart';
import 'dart:math';

class TileData {
  int position;
  int energy;
  int people;

  TileData({this.position, this.energy, this.people});
}


// The 'add' below is causing the error "The expression here has a type of void and therefore can not be used"
List<TileData> list=[];
list.add(TileData(
    position:1,
    energy:2,
    people:4
));
list.add(TileData(
    position:2,
    energy:5,
    people:3
));

// The "tileList" below is causing error "Undefined name tileList"
List<FlatButton> buttons = List.generate(tileList.length, (position) {
  TileData data = tileList[position];
  return FlatButton(
      child: Text('${data.position}'),
      onPressed: () {
        print(data.energy);
        print(data.people);
      });
});
  • The UI should be separate from the 'tile" (game unit) properties or at least one level of indirect. It may help to describe your data structures in detail and what you hope to achieve since right now I'm not sure if this is a UI/UX question or a data question. – Morrison Chang Dec 24 '19 at 01:13

1 Answers1

0


You can make a simply create a class having your attributes.

 class TileData{
        int position;
        int energy;
        int people;
        TileData({this.position,this.energy,this.people});
    }

Then you can make a List<TileData>

List<TileData> list=[];
list.add(TileData(
    position:1,
    energy:2,
    people:4
));
list.add(TileData(
    position:2,
    energy:5,
    people:3
));

Then randomise it with the below function. List shuffle(List items) { var random = new Random();

  // Go through all elements.
  for (var i = items.length - 1; i > 0; i--) {

    // Pick a pseudorandom number according to the list length
    var n = random.nextInt(i + 1);

    var temp = items[i];
    items[i] = items[n];
    items[n] = temp;
  }

  return items;
}

Then you can create a list of FlatButtons with List.generate() where you can assign the desired job with exact data.

List<FlatButton> buttons=List.generate(tileList.length,(position){
    //You can get the data here
    TileData data=tileList[position];
    return FlatButton(
        child:Text('${data.position}'),
        onPressed:(){
            //You can do you task here
            print(data.energy);
            print(data.people);
        }
    );
});
Guru Prasad mohapatra
  • 1,809
  • 13
  • 19