FlutterIntermediate 3 min read

Flutter game input: translate interaction into world actions

Distinguish screen, local and world coordinates, and route input through game rules rather than directly awarding points.

On this page

An input event tells you that an interaction happened. The game still needs to interpret it: which entity was selected, whether the action is allowed and what changes as a result.

Name the coordinate system

Pointer positions can be reported in global or local coordinates. A camera can introduce a separate world coordinate system. Comparing values from different systems can make hit detection appear offset even when the shape calculation is correct.

Use local coordinates for a bounded widget game

The fragment belongs inside a build method. hitTestTarget and tryHit are functions in the game model. The model should reject an action when the game is paused or already complete.

Worked example

This is a focused code fragment. Place it in the class or file described above; it is not a complete standalone app.

GestureDetector(
  behavior: HitTestBehavior.opaque,
  onTapDown: (event) {
    final position = event.localPosition;
    final targetId = game.hitTestTarget(position);
    if (targetId != null) game.tryHit(targetId);
  },
  child: SizedBox(width: 300, height: 400, child: gameView),
)

Read the result

Local positions are meaningful within this detector. When using a game engine, use its documented conversion and input APIs rather than applying a guessed scale factor. A control overlay and world entity can have different input responsibilities.

Compare positions expressed in the same space

The local pointer position is measured relative to this detector. A screen-global point or a camera-world coordinate can have a different origin and scale. Subtracting numbers from different spaces can produce a consistent-looking offset that no radius adjustment will truly fix.

Convert through the documented coordinate APIs for the actual game/view before testing shapes. Then send a semantic intent such as tryHit(targetId) to the game model, which can reject the action when paused or finished.

Practise with the bounded tap game first. Add camera/world conversion only when a world-space game actually needs it.

Check a resized and letterboxed viewport

If a game world is scaled into a centered viewport, the pointer’s screen position includes the viewport offset. Convert through the game’s camera/viewport transform before comparing it with world positions. A point near the screen’s left edge may be outside the playable world even though it is inside the Flutter widget.

Use one coordinate convention for a component’s position, anchor and hit region. Draw a temporary marker at the converted point during development, then verify a tap at each corner after resizing. Include a game embedded below an app bar so a missing vertical offset becomes visible.

For grid games, convert a validated level cell to a world position using a tile size and origin. The AI-generated level lesson keeps these cells separate from rendering coordinates; the same validator can therefore run without a canvas.

Practice and verification

Move the game area inside padding and verify hit detection stays aligned. Add a pause state and ensure taps cannot change the score while paused. Offer a keyboard or accessible alternative when the game rules permit it.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.