Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic BoardArrow List #23

Open
hiulusoy opened this issue Mar 12, 2022 · 5 comments
Open

Dynamic BoardArrow List #23

hiulusoy opened this issue Mar 12, 2022 · 5 comments

Comments

@hiulusoy
Copy link

My goal was trying to create a dynamic list of BoardArrow that can be changed at each user move.

The problem is after I use setState with BoardArrow list, the 'ChessBoard' widget is going to be reset with the initial position.

The process were;

  • Create a list of board arrow
    Cursor_and_chess-tactics_–_chess_board_view_dart-2

  • Assign board arrow list the to 'ChessBoard' widget that we're mainly using.
    Cursor_and_chess-tactics_–_chess_board_view_dart

  • After these steps we have two options which are adding new BoardArrow to the list and show in the 'ChessBoard' widget.
    Cursor_and_chess-tactics_–_chess_board_view_dart-3

  • Or we can clear the BoardArrow list after we've shown.
    chess-tactics_–_chess_board_view_dart

@deven98
Copy link
Owner

deven98 commented Mar 20, 2022

Hi @hiusanmaz,

This a peculiar bug, I'll test it out and push a fix in a couple of days.

Thanks for reporting this.

@deven98
Copy link
Owner

deven98 commented Apr 17, 2022

Hi @hiusanmaz, I'm unable to recreate your bug (assuming your bug is the board resetting after you change the board arrows).

Here's a video and code snippet which work for me:

Video:

chessissue480.mov

Code:

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

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

class _HomePageState extends State<HomePage> {
  ChessBoardController controller = ChessBoardController();

  var arrows = [
    BoardArrow(
      from: 'd2',
      to: 'd4',
      //color: Colors.red.withOpacity(0.5),
    ),
    BoardArrow(
      from: 'e7',
      to: 'e5',
      color: Colors.red.withOpacity(0.7),
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Chess Demo'),
      ),
      body: Column(
        children: [
          FlatButton(
            onPressed: () {
              setState(() {
                arrows.add(BoardArrow(from: 'a1', to: 'h7'));
              });
            },
            child: Text('Add Arrow'),
          ),
          Expanded(
            child: Center(
              child: ChessBoard(
                controller: controller,
                boardColor: BoardColor.orange,
                arrows: arrows,
                boardOrientation: PlayerColor.white,
              ),
            ),
          ),
          Expanded(
            child: ValueListenableBuilder<Chess>(
              valueListenable: controller,
              builder: (context, game, _) {
                return Text(
                  controller.getSan().fold(
                        '',
                        (previousValue, element) =>
                            previousValue + '\n' + (element ?? ''),
                      ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}

Do you think there's anything I might need to know for replicating this bug?
Additionally, are you sure the ChessBoardController isn't being recreated since the board is stored in the controller, not in the board Widget itself?

@hiulusoy
Copy link
Author

Hi @deven98,

My business logic is integrated with the onMove method. When a move done; I should be able to clear the arrows or able to add new ones. Now we can try to create and set onMove method to ChessBoard like

child: ChessBoard(
               controller: controller,
               boardColor: BoardColor.orange,
               arrows: arrows,
               boardOrientation: PlayerColor.white,
               onMove: onMove

             ),
onMove(){
// Clear the current arrows with setState
setState(){
arrows = [];
}
Business Logic etc.
// Let's create a new arrow and show
}

And secondly I checked and found I was creating an instance of my main ChessBoardController in initState method. I changed from this:
image

To this:
image

But the problem is still remaining.

@deven98
Copy link
Owner

deven98 commented Apr 17, 2022

Hey @hiusanmaz,

This doesn't really give me any idea of what's going wrong since the example I provided works fine for me even if I use the onMove callback instead of the button.
Can you give me a full example that I can run directly which has your issue - similar to the one in my comment?

Thanks.

@hiulusoy
Copy link
Author

hiulusoy commented Apr 17, 2022

Hello @deven98,
Firstly let me check my Business Logic again. If I still came with errors, I'm going to provide you a full example.

I just checked my Business Logic and found there were some unnecessary setState methods which causes an error that mentioned before on the Stackoverflow, resets the ChessBoard Widget. So I just wrapped my arrow list and ChessBoard widget with ValueNotifier and It's working as expected.

Thank you for your effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants