OLD | NEW |
1 --- | 1 --- |
2 layout: homepage | 2 layout: homepage |
3 googleapi: true | 3 googleapi: true |
4 js: | 4 js: |
5 - url: /js/display-news.js | 5 - url: /js/display-news.js |
6 - url: /js/os-switcher.js | 6 - url: /js/os-switcher.js |
7 defer: true | 7 defer: true |
8 - url: /js/editor-downloads-analytics.js | 8 - url: /js/editor-downloads-analytics.js |
9 defer: true | 9 defer: true |
10 - url: /js/editor-version.js | 10 - url: /js/editor-version.js |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 <div id="feed-posts"> | 133 <div id="feed-posts"> |
134 </div> | 134 </div> |
135 </div> | 135 </div> |
136 </div> | 136 </div> |
137 </div> | 137 </div> |
138 </section> | 138 </section> |
139 | 139 |
140 <section id="code"> | 140 <section id="code"> |
141 <h1> Code </h1> | 141 <h1> Code </h1> |
142 | 142 |
143 <p> | 143 <p> |
144 Experiment with Dart right in your browser: | 144 You can find code examples in Dart Editor's welcome page, |
145 <a href="http://try.dartlang.org">try.dartlang.org</a> | 145 in the <a href="/samples/">Samples page</a>, |
| 146 and below. |
| 147 You can also experiment with Dart right in your browser: |
| 148 <a href="http://try.dartlang.org" target="_blank">try.dartlang.org</a> |
146 </p> | 149 </p> |
147 | 150 |
148 <p> | 151 <div class="tabbable"> |
149 Here's some code from a Dart web app that uses Web UI. | 152 <ul class="nav nav-tabs"> |
150 To learn about web programming with Dart, try our | 153 <li class="active"><a href="#syntax" data-toggle="tab">Syntax</a></li> |
151 <a href="/docs/tutorials/">Dart tutorials</a>. | 154 <li><a href="#types" data-toggle="tab">Optional types</a><
/li> |
152 </p> | 155 <li><a href="#classes" data-toggle="tab">Classes</a></li> |
153 | 156 <li><a href="#futures" data-toggle="tab">Futures</a></li> |
154 <div class="row-fluid"> | 157 <li><a href="#webui" data-toggle="tab">Web UI</a></li> |
155 <div class="span6" id="dart-code"> | 158 </ul> |
| 159 |
| 160 <div class="tab-content"> |
| 161 <div class="tab-pane active" id="syntax"> |
| 162 <p> |
| 163 Dart aims to be unsurprising, |
| 164 but it does add a few language features |
| 165 to keep your code succinct yet understandable. |
| 166 Read all about Dart syntax in the |
| 167 <a href="/docs/dart-up-and-running/contents/ch02.html">Dart language tou
r</a>. |
| 168 </p> |
| 169 |
| 170 <div class="row-fluid"> |
| 171 <div class="span10 offset1"> |
| 172 {% prettify dart %} |
| 173 import 'dart:math'; // Import a library. |
| 174 |
| 175 // Functions and variables can be inside or outside of classes. |
| 176 var shapes = new List(); // A global variable. |
| 177 addShape(shape) [[highlight]]=>[[/highlight]] shapes.add(shape); // Function sho
rthand syntax. |
| 178 |
| 179 // Every app has a main() function, where execution starts. |
| 180 [[highlight]]main()[[/highlight]] { |
| 181 // The cascade operator (..) saves you from repetitive typing. |
| 182 addShape(new Ellipse(10, 20)[[highlight]]..[[/highlight]]rotation = 45*PI/180 |
| 183 [[highlight]]..[[/highlight]]color = 'rgb(0,129,19
8)' |
| 184 [[highlight]]..[[/highlight]]outlineWidth = 0); |
| 185 |
| 186 // You can easily insert expression values into strings. |
| 187 print('Area of the first shape: [[highlight]]${shapes[0].area}[[/highlight]]')
; |
| 188 } |
| 189 {% endprettify %} |
| 190 </div> |
| 191 </div> |
| 192 </div> |
| 193 |
| 194 <div class="tab-pane" id="types"> |
| 195 <p> |
| 196 Declaring types is optional in Dart. |
| 197 These types don't affect the runtime behavior, |
| 198 but you can use them to clarify APIs |
| 199 and get better help from tools—for example, |
| 200 better code completion, easier navigation, and earlier error detection. |
| 201 For details, see |
| 202 <a href="/articles/optional-types/">Optional Types in Dart</a>. |
| 203 </p> |
| 204 |
| 205 <div class="row-fluid"> |
| 206 <div class="span6" id="without-types"> |
| 207 {% prettify dart %} |
| 208 // Without types. |
| 209 recalculate(origin, offset, estimate) { |
| 210 // ... |
| 211 } |
| 212 {% endprettify %} |
| 213 </div> |
| 214 <div class="span6" id="with-types"> |
| 215 {% prettify dart %} |
| 216 // With types (and an optional parameter). |
| 217 [[highlight]]num[[/highlight]] recalculate([[highlight]]Point[[/highlight]] orig
in, |
| 218 [[highlight]]num[[/highlight]] offset, |
| 219 {[[highlight]]bool[[/highlight]] estimate:false}) { |
| 220 // ... |
| 221 } |
| 222 {% endprettify %} |
| 223 </div> |
| 224 </div> |
| 225 </div> |
| 226 |
| 227 <div class="tab-pane" id="classes"> |
| 228 <p> |
| 229 Dart is object oriented, with full support for classes. |
| 230 Each class has a single <em>superclass</em>, |
| 231 but classes can implement any number of <em>interfaces</em>. |
| 232 For details, see |
| 233 <a href="/docs/dart-up-and-running/contents/ch02.html#classes">Classes</a>
in the language tour. |
| 234 </p> |
| 235 |
| 236 <div class="row-fluid"> |
| 237 <div class="span10 offset1"> |
| 238 {% prettify dart%} |
| 239 import 'dart:math'; |
| 240 |
| 241 [[highlight]]class[[/highlight]] Ellipse extends Shape { // Declare
a class. |
| 242 num majorAxis = 0; // An instance variable (property). |
| 243 num minorAxis = 0; |
| 244 static const num C = PI/4; // A constant. |
| 245 num get area => C*majorAxis*minorAxis; // A property implemented with a gett
er. |
| 246 |
| 247 Ellipse([[highlight]]this.majorAxis[[/highlight]], [[highlight]]this.minorAxis
[[/highlight]]); // Compact constructor syntax. |
| 248 Ellipse.circle(num diameter) { // A named constructor. |
| 249 minorAxis = majorAxis = diameter; |
| 250 } |
| 251 } |
| 252 |
| 253 [[highlight]]abstract[[/highlight]] class Shape { // Declare
a class that can't be instantiated. |
| 254 num get area; |
| 255 num rotation = 0; |
| 256 num outlineWidth = 1; |
| 257 String color = 'black'; |
| 258 } |
| 259 {% endprettify %} |
| 260 </div> |
| 261 </div> |
| 262 </div> |
| 263 |
| 264 <div class="tab-pane" id="futures"> |
| 265 <p> |
| 266 Functions that do potentially expensive work return Futures. |
| 267 To specify what happens when the function's work is done, |
| 268 invoke <code>then()</code> on the Future. |
| 269 To handle errors, invoke <code>catchError()</code>. |
| 270 Learn more by reading |
| 271 <a href="/articles/using-future-based-apis/">Using Future Based APIs</a>
. |
| 272 </p> |
| 273 |
| 274 <div class="row-fluid"> |
| 275 <div class="span10 offset1"> |
| 276 |
| 277 {% prettify dart %} |
| 278 import 'dart:io'; |
| 279 import 'dart:async'; |
| 280 |
| 281 void printDailyNewsDigest() { |
| 282 File file = new File("dailyNewsDigest.txt"); |
| 283 [[highlight]]file.readAsString()[[/highlight]] // readAsStrin
g() returns a Future. |
| 284 [[highlight]].then[[/highlight]]((content) => print(content)) // Handle succ
essful completion. |
| 285 [[highlight]].catchError[[/highlight]]((error) => // Handle fail
ure. |
| 286 print("Sorry, no news today. Here's why:\n$error")); |
| 287 } |
| 288 {% endprettify %} |
| 289 </div> |
| 290 </div> |
| 291 </div> |
| 292 <div class="tab-pane" id="webui"> |
| 293 <p> |
| 294 Here's an example of implementing a web app using the Web UI package. |
| 295 To learn about web programming with Dart, try our |
| 296 <a href="/docs/tutorials/">Dart tutorials</a>. |
| 297 </p> |
| 298 |
| 299 <div class="row-fluid"> |
| 300 <div class="span6" id="dart-code"> |
156 {% prettify dart %} | 301 {% prettify dart %} |
157 // Dart code: | 302 // Dart code: |
158 import 'package:web_ui/web_ui.dart'; | 303 import 'package:web_ui/web_ui.dart'; |
159 | 304 |
160 @observable String myString = ''; | 305 @observable |
161 | 306 var [[highlight]]shout[[/highlight]] = new Shout(); |
162 String get shouted => myString.toUpperCase(); | 307 |
163 | 308 @observable |
164 String get palindrome => | 309 class Shout { |
165 myString + myString.split('').reversed.join(); | 310 String [[highlight]]myString[[/highlight]] = ''; |
166 {% endprettify %} | 311 String get [[highlight]]shouted[[/highlight]] => myString.toUpperCase(); |
167 </div> | 312 String get [[highlight]]palindrome[[/highlight]] => |
168 <div class="span6" id="html-code"> | 313 myString + myString.split('').reversed.join(); |
| 314 } |
| 315 {% endprettify %} |
| 316 </div> |
| 317 <div class="span6" id="html-code"> |
169 {% prettify html %} | 318 {% prettify html %} |
170 <!-- HTML code: --> | 319 <!-- HTML code: --> |
171 <input type="text" bind-value="myString" | 320 <template id="stringManipulation" |
172 placeholder="Type here"> | 321 instantiate="if [[highlight]]shout[[/highlight]] != null"> |
173 | 322 <input type="text" |
174 <div> Shouted: {{'{{'}}shouted}} </div> | 323 bind-value="shout.[[highlight]]myString[[/highlight]]" |
175 <div> Palindromic: {{'{{'}}palindrome}} </div> | 324 placeholder="Type here"> |
176 | 325 |
177 <script type="application/dart" src="little.dart"></script> | 326 <div> Shouted: {{'{{'}}shout.[[highlight]]shouted[[/highlight]]}} </div> |
178 <script src="packages/browser/dart.js"></script> | 327 <div> Palindromic: {{'{{'}}shout.[[highlight]]palindrome[[/highlight]]}} </div
> |
| 328 </template> |
179 {% endprettify %} | 329 {% endprettify %} |
180 {% comment %} | 330 {% comment %} |
181 Beginning double curlies are processed by liquid, | 331 Beginning double curlies are processed by liquid, |
182 so you have to surround them in single quotes, | 332 so you have to surround them in single quotes, |
183 surrounded by double curlies. Yup. | 333 surrounded by double curlies. Yup. |
184 {% endcomment %} | 334 {% endcomment %} |
185 </div> | 335 </div> |
186 </div> <!-- code --> | 336 </div> <!-- code --> |
187 | 337 </div> |
| 338 </div> |
| 339 </div> |
188 </section> | 340 </section> |
189 | 341 |
190 | 342 |
191 <section id="get-started"> | 343 <section id="get-started"> |
192 | 344 |
193 <h1> Get started </h1> | 345 <h1> Get started </h1> |
194 | 346 |
195 <p> | 347 <p> |
196 Getting started with Dart is easy. | 348 Getting started with Dart is easy. |
197 Just download Dart, | 349 Just download Dart, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 You can write Dart apps using any recent version of | 420 You can write Dart apps using any recent version of |
269 {% include os-choices.html %} | 421 {% include os-choices.html %} |
270 The Dart development tools <b>do not support Windows XP</b>. | 422 The Dart development tools <b>do not support Windows XP</b>. |
271 Dart Editor requires Java version 6 or higher. | 423 Dart Editor requires Java version 6 or higher. |
272 </aside> | 424 </aside> |
273 | 425 |
274 </section> | 426 </section> |
275 | 427 |
276 </article> <!-- /.homepage --> | 428 </article> <!-- /.homepage --> |
277 | 429 |
OLD | NEW |