OLD | NEW |
(Empty) | |
| 1 --- |
| 2 layout: default |
| 3 title: "Try Dart" |
| 4 description: "Write some Dart code. Learn some stuff." |
| 5 has-permalinks: true |
| 6 tutorial: |
| 7 id: trydart |
| 8 js: |
| 9 - url: /js/os-switcher.js |
| 10 defer: true |
| 11 - url: /js/editor-downloads-analytics.js |
| 12 defer: true |
| 13 - url: /js/editor-version.js |
| 14 defer: true |
| 15 header: |
| 16 css: ["/articles/trydart/trydart.css"] |
| 17 --- |
| 18 |
| 19 # {{ page.title }} |
| 20 |
| 21 ## Got an hour? Write a Dart app. |
| 22 |
| 23 In this code lab, |
| 24 you build a pirate badge generator from a skeleton app. |
| 25 |
| 26 <strong>Build this app!</strong> |
| 27 |
| 28 <iframe class="running-app-frame" |
| 29 style="height:220px;width:530px;" |
| 30 src="examples/piratebadge/piratebadge.html"> |
| 31 </iframe> |
| 32 |
| 33 <hr> |
| 34 |
| 35 ## The Map |
| 36 |
| 37 * [Step 0: Set up](#set-up) |
| 38 * [Step 1: Look at the code for the skeleton app](#step-one) |
| 39 * [Step 2: Add an input field](#step-two) |
| 40 * [Step 3: Add a button](#step-three) |
| 41 * [Step 4: Create a PirateName class](#step-four) |
| 42 * [Step 5: Generate random names](#step-five) |
| 43 * [Step 6: Add radio buttons](#step-six) |
| 44 * [Step 7: Read the names from a JSON file with HttpRequest](#step-seven) |
| 45 |
| 46 |
| 47 XXX: Notes to self: |
| 48 XXX: use fancy code highlighter for hover, perhaps this would be |
| 49 good for comments about language features vs. code walkthrough comments |
| 50 |
| 51 <hr> |
| 52 |
| 53 ## Step 0: Set up {#set-up} |
| 54 |
| 55 In this step you download Dart and get the sample code. |
| 56 |
| 57 #### <i class="icon-anchor"> </i> Get Dart. |
| 58 |
| 59 <div class="trydart-step-details" markdown="1"> |
| 60 If you haven't already done so, |
| 61 get the Dart download. |
| 62 |
| 63 {% include downloads/_dart-editor.html %} |
| 64 |
| 65 <p markdown="1"> |
| 66 The Dart tools |
| 67 work in recent versions of |
| 68 {% include os-choices.html %} |
| 69 Having trouble? Go to the |
| 70 [Troubleshooting Dart Editor](/tools/editor/troubleshoot.html) page. |
| 71 </p> |
| 72 </div> |
| 73 |
| 74 #### <i class="icon-anchor"> </i> Start the Editor. |
| 75 |
| 76 <div class="trydart-step-details"> |
| 77 Start Dart Editor by double clicking its icon |
| 78 <img src="/imgs/Dart_Logo_21.png" |
| 79 width="21" height="21" alt="Dart Editor icon">. |
| 80 </div> |
| 81 |
| 82 #### <i class="icon-anchor"> </i> Get the sample code. |
| 83 |
| 84 <div class="trydart-step-details"> |
| 85 <a href="https://github.com/dart-lang/dart-tutorials-samples/archive/master.zip"
> |
| 86 Download |
| 87 </a> |
| 88 the sample code. |
| 89 Unzip the ZIP file. |
| 90 </div> |
| 91 |
| 92 #### <i class="icon-anchor"> </i> Open the directory. |
| 93 |
| 94 <div class="trydart-step-details" markdown="1"> |
| 95 In Dart Editor, |
| 96 open the `piratebadge` directory from the ZIP file |
| 97 with **File>Open Existing Folder...**. |
| 98 </div> |
| 99 |
| 100 <div class="row"> <div class="span7" markdown="1"> |
| 101 |
| 102  |
| 103 |
| 104 </div> <div class="span5" markdown="1"> |
| 105 |
| 106 <i class="icon-key"> </i> <strong> Key Information </strong> |
| 107 |
| 108 * Dart web apps use the public `browser` package to run on a web page. |
| 109 This project has all of the package dependencies set up for you |
| 110 and the packages installed. |
| 111 The packages directory, the `pubspec.yaml`, and `pubspec.lock` files are |
| 112 all related to library dependencies. |
| 113 |
| 114 * Several numbered directories contain the completed code for each step. |
| 115 `1-blankbadge` contains the skeletal version of the app that you begin with. |
| 116 `6-piratebadge_json` contains the final version of the app. |
| 117 |
| 118 * The `piratebadge.css` file |
| 119 provides the CSS styles for all steps of the app. |
| 120 You don't change this file during this code lab. |
| 121 |
| 122 {% comment %} non-breaking space required for bootstrap/markdown bogosity
{% endcomment %} |
| 123 |
| 124 </div> </div> |
| 125 |
| 126 <hr> |
| 127 |
| 128 ##Step 1: Run the skeleton app {#step-one} |
| 129 |
| 130 In this step, you open the source files for the skeleton app, |
| 131 familiarize yourself with the Dart and HTML code, |
| 132 and run the app. |
| 133 |
| 134 #### <i class="icon-anchor"> </i> Expand the 1-blankbadge directory. |
| 135 |
| 136 <div class="trydart-step-details" markdown="1"> |
| 137 In Dart Editor, expand the `1-blankbadge` directory, |
| 138 which contains two files, `piratebadge.html` and `piratebadge.dart`. |
| 139 </div> |
| 140 |
| 141 #### <i class="icon-anchor"> </i> Open the files. |
| 142 |
| 143 <div class="trydart-step-details" markdown="1"> |
| 144 Open both files by double-clicking each filename in Dart Editor. |
| 145 </div> |
| 146 |
| 147 #### <i class="icon-anchor"> </i> Review the code |
| 148 |
| 149 <div class="trydart-step-details" markdown="1"> |
| 150 Get familiar with the HTML and the Dart code for the skeleton version of the app
. |
| 151 The interesting bits are highlighted below. |
| 152 </div> |
| 153 |
| 154 <div class="trydart-step-details" markdown="1"> |
| 155 ##### piratebadge.html |
| 156 </div> |
| 157 |
| 158 <div class="row"> <div class="span7"> |
| 159 |
| 160 <div class="trydart-step-details" markdown="1"> |
| 161 {% prettify html%} |
| 162 <html> |
| 163 <head> |
| 164 <meta charset="utf-8"> |
| 165 <title>Pirate badge</title> |
| 166 <link rel="stylesheet" href="../piratebadge.css"> |
| 167 </head> |
| 168 <body> |
| 169 <h1>Pirate badge</h1> |
| 170 |
| 171 <div class="widgets"> |
| 172 [[highlight]]<span>TO DO: Put the UI widgets here.</span>[[/highlight]] |
| 173 </div> |
| 174 <div class="outer"> |
| 175 <div class="boilerplate"> |
| 176 Arrr! Me name is |
| 177 </div> |
| 178 <div class="name"> |
| 179 [[highlight]]<span id="badgeName"> </span>[[/highlight]] |
| 180 </div> |
| 181 </div> |
| 182 |
| 183 [[highlight]]<script type="application/dart" src="piratebadge.dart"></script
>[[/highlight]] |
| 184 [[highlight]]<script src="packages/browser/dart.js"></script>[[/highlight]] |
| 185 </body> |
| 186 </html> |
| 187 {% endprettify %} |
| 188 </div> |
| 189 |
| 190 </div> <div class="span5" markdown="1"> |
| 191 |
| 192 <i class="icon-key"> </i> <strong> Key Information </strong> |
| 193 |
| 194 * During the code lab, |
| 195 all the changes you make to `piratebadge.html` are within |
| 196 the <div> element identified with the class `widgets`. |
| 197 |
| 198 * The <span> element with the ID `badgeName` |
| 199 is programmatically updated by the Dart code |
| 200 based on user input. |
| 201 |
| 202 * The `piratebadge.dart` script provides the main program for the app. |
| 203 |
| 204 * The `packages/browser/dart.js` script is a bootstrap script |
| 205 that takes care of turning on the Dart VM, |
| 206 as well as compatibility with non-Dart browsers. |
| 207 |
| 208 </div> </div> |
| 209 |
| 210 <div class="trydart-step-details" markdown="1"> |
| 211 ##### piratebadge.dart |
| 212 </div> |
| 213 |
| 214 <div class="row"> <div class="span7" markdown="1"> |
| 215 |
| 216 <div class="trydart-step-details" markdown="1"> |
| 217 {% prettify dart %} |
| 218 [[highlight]]void main() { |
| 219 // Your app starts here. |
| 220 } |
| 221 [[/highlight]] |
| 222 {% endprettify %} |
| 223 </div> |
| 224 |
| 225 </div> <div class="span5" markdown="1"> |
| 226 |
| 227 * This file is included in the HTML file |
| 228 with the <script> tag. |
| 229 |
| 230 * The `main()` function is a top-level function. |
| 231 Dart calls this function when your app starts. |
| 232 |
| 233 {% comment %} non-breaking space required for bootstrap/markdown bogosity
{% endcomment %} |
| 234 </div> </div> |
| 235 |
| 236 #### <i class="icon-anchor"> </i> Run the app. |
| 237 |
| 238 <div class="trydart-step-details" markdown="1"> |
| 239 To run the app in Dart Editor, select `piratebadge.html` |
| 240 and click the Run button |
| 241 <img src="images/run.png" width="16" height="16" |
| 242 alt="Run button">. |
| 243 |
| 244 You should see a TO DO comment on the left |
| 245 and a red and white name badge on the right. |
| 246 </div> |
| 247 |
| 248 <div class="trydart-step-details" markdown="1"> |
| 249 <iframe class="running-app-frame" |
| 250 style="height:220px;width:530px;" |
| 251 src="examples/1-blankbadge/piratebadge.html"> |
| 252 </iframe> |
| 253 </div> |
| 254 |
| 255 <hr> |
| 256 |
| 257 ##Step 2: Add an input field {#step-two} |
| 258 |
| 259 In this step you add an input field to the app. |
| 260 The user types a name into the input field and presses return. |
| 261 The Dart code updates the badge from the value of the input field |
| 262 when the user presses return. |
| 263 |
| 264 #### Try it! |
| 265 |
| 266 <div class="trydart-step-details" markdown="1"> |
| 267 Type in the input field and press return. |
| 268 |
| 269 <iframe class="running-app-frame" |
| 270 style="height:220px;width:530px;" |
| 271 src="examples/2-inputnamebadge/inputnamebadge.html"> |
| 272 </iframe> |
| 273 </div> |
| 274 |
| 275 #### <i class="icon-anchor"> </i> Edit piratebadge.html. |
| 276 |
| 277 <div class="row"> <div class="span7" markdown="1"> |
| 278 |
| 279 <div class="trydart-step-details" markdown="1"> |
| 280 |
| 281 Add an input field to the HTML code as shown by the highlighted code. |
| 282 |
| 283 {% prettify html %} |
| 284 ... |
| 285 <div class="widgets"> |
| 286 [[highlight]] <div> |
| 287 <input type="text" id="inputName" value="Anne"> |
| 288 </div>[[/highlight]] |
| 289 </div> |
| 290 ... |
| 291 {% endprettify %} |
| 292 </div> |
| 293 |
| 294 </div> <div class="span5" markdown="1"> |
| 295 |
| 296 <i class="icon-key"> </i> <strong> Key Information </strong> |
| 297 |
| 298 * The ID for the input element is `inputName`. |
| 299 The Dart code gets this element by querying the DOM for this ID. |
| 300 |
| 301 {% comment %} non-breaking space required for bootstrap/markdown bogosity
{% endcomment %} |
| 302 |
| 303 </div> </div> |
| 304 |
| 305 #### <i class="icon-anchor"> </i> Edit piratebadge.dart. |
| 306 |
| 307 <div class="trydart-step-details"> |
| 308 Add the highlighted code as shown in the code snippets below. |
| 309 </div> |
| 310 |
| 311 <div class="trydart-step-details"> |
| 312 Import the dart:html libary: |
| 313 </div> |
| 314 |
| 315 <div class="row"> <div class="span7"> |
| 316 |
| 317 <div class="trydart-step-details" markdown="1"> |
| 318 |
| 319 {% prettify dart %} |
| 320 [[highlight]]import 'dart:html';[[/highlight]] |
| 321 {% endprettify %} |
| 322 </div> |
| 323 |
| 324 </div> <div class="span5" markdown="1"> |
| 325 |
| 326 * The `dart:html` library provides HTML elements and access to the DOM. |
| 327 |
| 328 </div> </div> |
| 329 |
| 330 <div class="trydart-step-details"> |
| 331 |
| 332 Add a top-level variable to refer to the input field element. |
| 333 |
| 334 </div> |
| 335 |
| 336 <div class="row"> <div class="span7"> |
| 337 |
| 338 <div class="trydart-step-details" markdown="1"> |
| 339 {% prettify dart %} |
| 340 [[highlight]]InputElement inputNameElement;[[/highlight]] |
| 341 {% endprettify %} |
| 342 </div> |
| 343 </div><div class="span5" markdown="1"> |
| 344 |
| 345 * Declare a top-level variable of type |
| 346 <a href="https://api.dartlang.org/dart_html/InputElement.html" target="_blank">I
nputElement</a>, |
| 347 a class defined in `dart:html`. |
| 348 |
| 349 </div></div> |
| 350 |
| 351 <div class="trydart-step-details" markdown="1"> |
| 352 Add code to the main program to get the input field and give it an event handler
. |
| 353 </div> |
| 354 |
| 355 <div class="row"> <div class="span7"> |
| 356 <div class="trydart-step-details" markdown="1"> |
| 357 {% prettify dart %} |
| 358 void main() { |
| 359 [[highlight]] inputNameElement = query('#inputName'); |
| 360 inputNameElement.onChange.listen(generateBadge);[[/highlight]] |
| 361 } |
| 362 {% endprettify %} |
| 363 </div> |
| 364 |
| 365 </div> <div class="span5" markdown="1"> |
| 366 |
| 367 * The `query()` function gets an element from the DOM. |
| 368 This program uses the CSS ID selector `#inputName` to get the input field. |
| 369 |
| 370 * The code registers a change event handler on the input field. |
| 371 A change event occurs when the user presses return. |
| 372 Other kinds of events include mouse click, and input events. |
| 373 |
| 374 </div> </div> |
| 375 |
| 376 <div class="trydart-step-details" markdown="1"> |
| 377 Implement the event handler for the text field. |
| 378 </div> |
| 379 |
| 380 <div class="row"> <div class="span7"> |
| 381 <div class="trydart-step-details" markdown="1"> |
| 382 {% prettify dart %} |
| 383 |
| 384 [[highlight]]void generateBadge(Event event) { |
| 385 query('#badgeName').text = inputNameElement.value; |
| 386 }[[/highlight]] |
| 387 {% endprettify %} |
| 388 </div> |
| 389 |
| 390 </div> <div class="span5" markdown="1"> |
| 391 |
| 392 * The `generateBadge()` function is a top-level function. |
| 393 It is also an event handler based on its signature. |
| 394 |
| 395 * Dart has common programming language constructs like `if`-`else`. |
| 396 |
| 397 * Set the text of the badgeName element using the value of the input field. |
| 398 |
| 399 |
| 400 {% comment %} non-breaking space required for bootstrap/markdown bogosity
{% endcomment %} |
| 401 |
| 402 </div> </div> |
| 403 |
| 404 <hr> |
| 405 |
| 406 ##Step 3: Add a button {#step-three} |
| 407 |
| 408 In this step you add a button to the app. |
| 409 |
| 410 #### Try it! |
| 411 |
| 412 <div class="trydart-step-details"> |
| 413 Type in the text field and click the button. |
| 414 </div> |
| 415 |
| 416 <div class="trydart-step-details"> |
| 417 <iframe class="running-app-frame" |
| 418 style="height:220px;width:530px;" |
| 419 src="examples/3-buttonbadge/piratebadge.html"> |
| 420 </iframe> |
| 421 </div> |
| 422 |
| 423 #### <i class="icon-anchor"> </i> Edit piratebadge.html. |
| 424 |
| 425 <div class="trydart-step-details"> |
| 426 Add the button below the input field as shown: |
| 427 </div> |
| 428 |
| 429 <div class="row"> <div class="span7"> |
| 430 |
| 431 <div class="trydart-step-details"> |
| 432 {% prettify html %} |
| 433 ... |
| 434 <div class="widgets"> |
| 435 <div> |
| 436 <input type="text" id="inputName" value="Anne"> |
| 437 </div> |
| 438 [[highlight]] <div> |
| 439 <button id="generateButton">Generate badge</button> |
| 440 </div>[[/highlight]] |
| 441 </div> |
| 442 ... |
| 443 {% endprettify %} |
| 444 </div> |
| 445 |
| 446 </div> <div class="span5" markdown="1"> |
| 447 |
| 448 <i class="icon-key"> </i> <strong> Key Information </strong> |
| 449 |
| 450 * The button has the ID `generateButton` so that |
| 451 the Dart code can query the DOM for element |
| 452 and provide it with a mouse click handler. |
| 453 |
| 454 </div> </div> |
| 455 |
| 456 #### <i class="icon-anchor"> </i> Edit piragebadge.dart. |
| 457 |
| 458 <div class="trydart-step-details"> |
| 459 Add the highlighted line of code that |
| 460 provides a mouse click handler for the button: |
| 461 </div> |
| 462 |
| 463 <div class="row"> <div class="span7"> |
| 464 |
| 465 <div class="trydart-step-details"> |
| 466 {% prettify dart %} |
| 467 InputElement inputNameElement; |
| 468 [[highlight]]ButtonElement genButtonElement;[[/highlight]] |
| 469 ... |
| 470 inputNameElement = query('#inputName'); |
| 471 inputNameElement.onChange.listen(generateBadge); |
| 472 |
| 473 [[highlight]]genButtonElement = query('#generateButton'); |
| 474 genButtonElement.onClick.listen(generateBadge);[[/highlight]] |
| 475 ... |
| 476 {% endprettify %} |
| 477 </div> |
| 478 |
| 479 </div> <div class="span5" markdown="1"> |
| 480 |
| 481 * The button uses the same event handler as the input field, |
| 482 but uses it to handle mouse click events |
| 483 instead of change events. |
| 484 |
| 485 {% comment %} non-breaking space required for bootstrap/markdown bogosity
{% endcomment %} |
| 486 |
| 487 </div> </div> |
| 488 |
| 489 <div class="trydart-step-details" markdown="1"> |
| 490 Modify the `generateBadge` event handler method as follows: |
| 491 </div> |
| 492 |
| 493 <div class="row"> <div class="span7"> |
| 494 |
| 495 <div class="trydart-step-details"> |
| 496 {% prettify dart %} |
| 497 void generateBadge(Event event) { |
| 498 [[highlight]]if (event.target == inputNameElement) {[[/highlight]] |
| 499 query('#badgeName').text = inputNameElement.value; |
| 500 [[highlight]] } else { |
| 501 query('#badgeName').text = 'Anne Bonney'; |
| 502 }[[/highlight]] |
| 503 } |
| 504 {% endprettify %} |
| 505 </div> |
| 506 </div> <div class="span5" markdown="1"> |
| 507 * If the user presses the button, the program updates the badge with the name `A
nne Bonney`. |
| 508 Otherwise, it uses the value of the input field. |
| 509 </div></div> |
| 510 |
| 511 <hr> |
| 512 |
| 513 ##Step 4: Create a PirateName class {#step-four} |
| 514 |
| 515 In this step, you change only the Dart code. |
| 516 You create a class to represent a pirate name. |
| 517 When created, an instance of this class |
| 518 randomly selects a name from a list, |
| 519 or optionally you can provide a name to the constructor. |
| 520 |
| 521 <iframe class="running-app-frame" |
| 522 style="height:220px;width:530px;" |
| 523 src="examples/4-classbadge/piratebadge.html"> |
| 524 </iframe> |
| 525 |
| 526 #### <i class="icon-anchor"> </i> Edit piratebadge.dart. |
| 527 |
| 528 <div class="trydart-step-details"> |
| 529 At the top of the file, |
| 530 add another import. |
| 531 </div> |
| 532 |
| 533 <div class="row"> <div class="span7"> |
| 534 <div class="trydart-step-details"> |
| 535 {% prettify dart %} |
| 536 import 'dart:math'; |
| 537 {% endprettify %} |
| 538 </div> |
| 539 </div class="span5" markdown="1"> |
| 540 Import the dart:math library to get constants |
| 541 and Random, the random number generator. |
| 542 |
| 543 |
| 544 <div class="trydart-step-details"> |
| 545 At the end of the file, add all of this code, |
| 546 which provides the definition of a new |
| 547 class called `PirateName`. |
| 548 </div> |
| 549 |
| 550 <div class="row"> <div class="span7"> |
| 551 |
| 552 <div class="trydart-step-details"> |
| 553 {% prettify dart %} |
| 554 class PirateName { |
| 555 |
| 556 Random indexGen = new Random(); |
| 557 |
| 558 String _pirateName; |
| 559 |
| 560 String get name => _pirateName; |
| 561 set name(String value) => _pirateName = value; |
| 562 |
| 563 String toString() => name; |
| 564 |
| 565 PirateName({String firstName}) { |
| 566 if (firstName == null) { |
| 567 firstName = names[indexGen.nextInt(names.length)]; |
| 568 } |
| 569 _pirateName = '$firstName'; |
| 570 } |
| 571 |
| 572 List names = const [ 'Anne', 'Bette', 'Cate', 'Dawn', |
| 573 ... |
| 574 'Yvan', 'Zeb']; |
| 575 } |
| 576 {% endprettify %} |
| 577 </div> |
| 578 |
| 579 |
| 580 </div> <div class="span5" markdown="1"> |
| 581 |
| 582 <i class="icon-key"> </i> <strong> Key Information </strong> |
| 583 |
| 584 * Class definition. |
| 585 |
| 586 * Instance variable declaration. |
| 587 |
| 588 * Private variables start with underscore (`_`). |
| 589 |
| 590 * Getters and setters. |
| 591 |
| 592 * Fat arrow shorthand. |
| 593 |
| 594 * Constructor with optional parameter. |
| 595 |
| 596 </div> </div> |
| 597 |
| 598 <div class="trydart-step-details" markdown="1"> |
| 599 Modify the code for `generateBadge()` event handler with this code: |
| 600 </div> |
| 601 |
| 602 <div class="row"> <div class="span7"> |
| 603 |
| 604 <div class="trydart-step-details"> |
| 605 {% prettify dart %} |
| 606 void generateBadge(Event event) { |
| 607 if (event.target == inputNameElement) { |
| 608 [[highlight]]query('#badgeName').text = new PirateName(firstName: inputNameE
lement.value).name;[[/highlight]] |
| 609 } else { |
| 610 [[highlight]]query('#badgeName').text = new PirateName().name;[[/highlight]] |
| 611 } |
| 612 } |
| 613 {% endprettify %} |
| 614 </div> |
| 615 |
| 616 </div><div class="span5" markdown="1"> |
| 617 |
| 618 * Calling a constructor with `new`. |
| 619 |
| 620 * Passing in an optional parameter. |
| 621 |
| 622 </div></div> |
| 623 |
| 624 |
| 625 ### Concepts {#step-four-concepts} |
| 626 |
| 627 Blahdity blah blah blah. |
| 628 |
| 629  |
| 630 |
| 631 ##Step 5: Add a check box {#step-five} |
| 632 |
| 633 In this step you add a checkbox to the app |
| 634 that allows the user to use a |
| 635 randomly selected name for the badge. |
| 636 |
| 637 #### Try it! |
| 638 |
| 639 <div class="trydart-step-details"> |
| 640 Select the check box then click the button. |
| 641 </div> |
| 642 |
| 643 <div class="trydart-step-details"> |
| 644 <iframe class="running-app-frame" |
| 645 style="height:220px;width:530px;" |
| 646 src="examples/4-randombadge/piratebadge.html"> |
| 647 </iframe> |
| 648 </div> |
| 649 |
| 650 #### <i class="icon-anchor"> </i> Edit piratebadge.html. |
| 651 |
| 652 <div class="trydart-step-details"> |
| 653 Add the checkbox just below the input field (and above the button) as shown: |
| 654 </div> |
| 655 |
| 656 <div class="row"> <div class="span7"> |
| 657 |
| 658 <div class="trydart-step-details"> |
| 659 {% prettify html %} |
| 660 ... |
| 661 <div class="widgets"> |
| 662 <div> |
| 663 <input type="text" id="inputName" value="Anne"> |
| 664 </div> |
| 665 [[highlight]] <div> |
| 666 <input type="checkbox" id="useRandomName"> |
| 667 <label for="useRandomName">Use random name.</label> |
| 668 </div>[[/highlight]] |
| 669 ... |
| 670 </div> |
| 671 ... |
| 672 {% endprettify %} |
| 673 </div> |
| 674 |
| 675 </div> <div class="span5" markdown="1"> |
| 676 |
| 677 <i class="icon-key"> </i> <strong> Key Information </strong> |
| 678 |
| 679 * Include a label for the check box so that the user can click either the label |
| 680 or the checkbox to change the setting. |
| 681 |
| 682 </div> </div> |
| 683 |
| 684 #### <i class="icon-anchor"> </i> Edit piratebadge.dart. |
| 685 |
| 686 <div class="trydart-step-details"> |
| 687 Add an import to the top of the file. |
| 688 </div> |
| 689 |
| 690 <div class="row"> <div class="span7"> |
| 691 |
| 692 <div class="trydart-step-details"> |
| 693 {% prettify dart %} |
| 694 import 'dart:html'; |
| 695 [[highlight]]import 'dart:math';[[/highlight]] |
| 696 {% endprettify %} |
| 697 </div> |
| 698 |
| 699 </div> <div class="span5" markdown="1"> |
| 700 |
| 701 Import the `dart:math` library, which includes |
| 702 the |
| 703 <a href="https://api.dartlang.org/dart_math/Random.html" target="_blank">Random<
/a> |
| 704 class, a random number generator. |
| 705 |
| 706 </div> </div> |
| 707 |
| 708 <div class="trydart-step-details"> |
| 709 Declare two top-level variables. |
| 710 </div> |
| 711 |
| 712 <div class="row"> <div class="span7"> |
| 713 |
| 714 <div class="trydart-step-details"> |
| 715 {% prettify dart %} |
| 716 ... |
| 717 InputElement inputNameElement; |
| 718 |
| 719 [[highlight]]bool useRandomName = false[[/highlight]] |
| 720 [[highlight]]Random indexGenerator = new Random();[[/highlight]] |
| 721 ... |
| 722 {% endprettify %} |
| 723 </div> |
| 724 |
| 725 </div> <div class="span5" markdown="1"> |
| 726 |
| 727 * The click handler for the check box |
| 728 sets `useRandomName` to true when |
| 729 the check box is selected and false otherwise. |
| 730 |
| 731 * Create a random number generator object using `new` |
| 732 and the name of the constructor. |
| 733 |
| 734 </div> </div> |
| 735 |
| 736 <div class="trydart-step-details"> |
| 737 Register a click handler |
| 738 for the check box in the `main()` function. |
| 739 </div> |
| 740 |
| 741 <div class="row"> <div class="span7"> |
| 742 |
| 743 <div class="trydart-step-details"> |
| 744 {% prettify dart %} |
| 745 void main() { |
| 746 ... |
| 747 [[highlight]]query('#useRandomName').onClick.listen(useRandomNameClickHandler)
;[[/highlight]] |
| 748 } |
| 749 {% endprettify %} |
| 750 </div> |
| 751 |
| 752 </div> <div class="span5" markdown="1"> |
| 753 |
| 754 * is there anything to say here? |
| 755 |
| 756 {% comment %} non-breaking space required for bootstrap/markdown bogosity
{% endcomment %} |
| 757 |
| 758 </div> </div> |
| 759 |
| 760 <div class="trydart-step-details"> |
| 761 Provide the code for the click handler. |
| 762 </div> |
| 763 |
| 764 <div class="row"> <div class="span7"> |
| 765 |
| 766 <div class="trydart-step-details"> |
| 767 {% prettify dart %} |
| 768 [[highlight]]void useRandomNameClickHandler(MouseEvent e) { |
| 769 if ((e.target as CheckboxInputElement).checked) { |
| 770 inputNameElement.disabled = true; |
| 771 useRandomName = true; |
| 772 } else { |
| 773 inputNameElement.disabled = false; |
| 774 useRandomName = false; |
| 775 } |
| 776 }[[/highlight]] |
| 777 |
| 778 {% endprettify %} |
| 779 </div> |
| 780 |
| 781 </div> <div class="span5" markdown="1"> |
| 782 |
| 783 * The click handler enables or disables the |
| 784 input field depending on whether the check box is selected. |
| 785 |
| 786 * The click handler sets `useRandomName` to true when the |
| 787 check box is selected. |
| 788 |
| 789 * Note the use of `as`, Dart's a typecast operator. |
| 790 |
| 791 </div> </div> |
| 792 |
| 793 <div class="trydart-step-details"> |
| 794 Add a top-level variable—a |
| 795 list of names from which to randomly select. |
| 796 </div> |
| 797 |
| 798 <div class="row"> <div class="span7"> |
| 799 <div class="trydart-step-details"> |
| 800 {% prettify %} |
| 801 ... |
| 802 bool useRandomName = false; |
| 803 Random indexGenerator = new Random(); |
| 804 |
| 805 [[highlight]]List names = [ "Anne", "Bette", "Cate", "Dawn", |
| 806 "Elise", "Faye", "Ginger", "Harriot", |
| 807 "Izzy", "Jane", "Kaye", "Liz", |
| 808 "Maria", "Nell", "Olive", "Pat", |
| 809 "Queenie", "Rae", "Sal", "Tam", |
| 810 "Uma", "Violet", "Wilma", "Xana", "Yvonne", "Zelda"]; [[/highlight]] |
| 811 ... |
| 812 {% endprettify %} |
| 813 </div> |
| 814 |
| 815 </div> <div class="span5" markdown="1"> |
| 816 |
| 817 * This is a list literal and defines a list that contains 26 strings. |
| 818 |
| 819 </div> </div> |
| 820 |
| 821 <div class="trydart-step-details"> |
| 822 Modify the click handler |
| 823 for the **Generate badge** button |
| 824 so that it generates a name when the check box is selected. |
| 825 </div> |
| 826 |
| 827 <div class="row"> <div class="span7"> |
| 828 <div class="trydart-step-details"> |
| 829 {% prettify dart %} |
| 830 void generateBadge(Event event) { |
| 831 [[highlight]] if (useRandomName) { |
| 832 query('#badgeName').text = names[indexGenerator.nextInt(names.length)]; |
| 833 } else {[[/highlight]] |
| 834 query('#badgeName').text = inputNameElement.value; |
| 835 [[highlight]] } [[/highlight]] |
| 836 } |
| 837 {% endprettify %} |
| 838 </div> |
| 839 |
| 840 </div> <div class="span5" markdown="1"> |
| 841 |
| 842 * You can get the length of a list with the `length` property. |
| 843 |
| 844 * The random number generator's `nextInt()` function |
| 845 gets a new random number betwee 0 and `names.length`. |
| 846 |
| 847 * The code uses that number as an index into the list of strings. |
| 848 |
| 849 </div> </div> |
| 850 |
| 851 {% comment %} |
| 852 #### piratebadge.dart |
| 853 |
| 854 <div class="row"> |
| 855 <div class="span7"> |
| 856 </div> |
| 857 <div class="span5"> |
| 858 </div> |
| 859 </div> |
| 860 |
| 861 #### piratebadge.html |
| 862 |
| 863 <div class="row"> |
| 864 <div class="span7"> |
| 865 </div> |
| 866 <div class="span5"> |
| 867 </div> |
| 868 </div> |
| 869 {% endcomment %} |
OLD | NEW |