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