Fireship · @fireship

Flutter Drag & Drop for Two-Year-Olds

Uploaded May 1, 2019 · Length 8:19

Flutter Drag & Drop for Two-Year-Olds

Summary

build a simple kids game with drag and drop feature using flutter and audio player plugin

  • flutter drag and drop
  • flutter kids game
  • flutter audio player
  • flutter game development
  • flutter tutorial

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    yesterday my two-year-old was playing

  2. 0:01

    with a book that has you connect a color

  3. 0:03

    to a fruit and I realize that would be

  4. 0:05

    the perfect feature to demonstrate drag

  5. 0:06

    and drop and flutter in today's video

  6. 0:08

    we'll build a simple kids game that

  7. 0:10

    allows you to drag a fruit over to a

  8. 0:12

    square with the corresponding color if

  9. 0:13

    you drop it on the correct square it

  10. 0:15

    will update the score and also give you

  11. 0:17

    some audio feedback if you're new here

  12. 0:20

    like and subscribe and you can find the

  13. 0:21

    full source code on fire ship bio now

  14. 0:23

    before we get started a lot of people

  15. 0:25

    have asked me when I'm going to release

  16. 0:27

    a full flutter course the answer is very

  17. 0:29

    soon it'll be released before Google i/o

  18. 0:31

    which is now only six days away so

  19. 0:32

    expect more details on that in the next

  20. 0:34

    few days now let's go ahead and take a

  21. 0:36

    closer look at the app we're building

  22. 0:37

    today you'll see two columns here and

  23. 0:39

    you can grab an item from the column on

  24. 0:41

    the left and then drag it to a target on

  25. 0:43

    the right if it's the correct color it

  26. 0:45

    will mark it as correct and then update

  27. 0:46

    the score but if it's dropped on the

  28. 0:48

    incorrect target then it will just go

  29. 0:49

    back to its original position and then

  30. 0:51

    we'll also randomize the ordering of the

  31. 0:53

    targets for each round just so there's a

  32. 0:55

    little bit of replay value and as an

  33. 0:56

    added touch we'll play a little sound

  34. 0:58

    effect every time the user makes a

  35. 0:59

    successful drop if we jump into the code

  36. 1:03

    the first thing we'll look at is the pub

  37. 1:04

    spec amyl file the only dependency

  38. 1:06

    outside of flutter is this audio players

  39. 1:08

    plugin I really like this plug-in

  40. 1:10

    because it works on both iOS and Android

  41. 1:11

    and also allows you to play multiple

  42. 1:14

    audio files simultaneously and that

  43. 1:15

    tends to be pretty important if you're

  44. 1:17

    building a game and you'll also notice

  45. 1:18

    that we have an mp3 file here registered

  46. 1:20

    in the assets directory and I've also

  47. 1:22

    registered a custom font called press

  48. 1:24

    Start which you can download from Google

  49. 1:25

    Fonts that'll give you that kind of

  50. 1:27

    pixelated retro look now moving on to

  51. 1:29

    the source code I'm importing dart math

  52. 1:31

    as well as the audio players plug-in

  53. 1:33

    then we'll update the font family in our

  54. 1:35

    theme data and then we'll set the home

  55. 1:37

    page as this color game screen that

  56. 1:39

    we're about to define now the screen

  57. 1:40

    itself is a stateful widget and the

  58. 1:42

    first property we'll define is the score

  59. 1:44

    which just starts out as an empty map

  60. 1:46

    eventually we'll update the keys on this

  61. 1:48

    map when the user drags a draggable

  62. 1:50

    widget on to the correct drag target the

  63. 1:52

    next thing we'll define is a map of

  64. 1:53

    choices and so each emoji which is a

  65. 1:55

    string will map to a material color

  66. 1:57

    emojis are really handy because they not

  67. 1:59

    only provide a graphic that we can use