// Dart basic sintax from https://dart.dev/language// 5. Imports// Importing core librariesimport'dart:math';import'dart:async';voidmain(){// 1. Printprint("1. Print");print('Hello, World!');// 2. Variablesprint("\n 2. Variables");varname='Voyager I';varyear=1977;varantennaDiameter=3.7;// 2.1 ArraysvarflybyObjects=['Jupiter','Saturn','Uranus','Neptune'];// 2.2 Objectsvarimage={'tags':['saturn'],'url':'//path/to/saturn.jpg'};print(name);print(year);print(antennaDiameter);print(flybyObjects);print(image);// 3. Control flow statementsprint("\n 3. Control flow statements");// 3.1 Conditionalif(year>=2001){print('21st century');}elseif(year>=1901){print('20th century');}// 3.2 Loopsfor(finalobjectinflybyObjects){print(object);}for(intmonth=1;month<=12;month++){print(month);}while(year<1980){year+=1;print(year);}// 4. Functionsprint("\n 4.Functions");intfibonacci(intn){if(n==0||n==1)returnn;returnfibonacci(n-1)+fibonacci(n-2);}varresult=fibonacci(20);print(result);flybyObjects.where((name)=>name.contains('turn')).forEach(print);// Using Classprint("\n 6. Classes");varvoyager=Spacecraft('Voyager I',DateTime(1977,9,5));voyager.describe();varvoyager3=Spacecraft.unlaunched('Voyager III');voyager3.describe();// Using Enumprint("\n 7. Enum");finalyourPlanet=Planet.uranus;if(!yourPlanet.isGiant){print('Your planet is not a "giant planet".');}// from exeptiontry{// Some code that might throw an exceptiondivide(10,0);// Division by zero}catch(e){print('Exception caught: $e');}}// 6. ClassesclassSpacecraft{Stringname;DateTime?launchDate;// Read-only non-final propertyint?getlaunchYear=>launchDate?.year;// Constructor, with syntactic sugar for assignment to members.Spacecraft(this.name,this.launchDate){// Initialization code goes here.}// Named constructor that forwards to the default one.Spacecraft.unlaunched(Stringname):this(name,null);// Method.voiddescribe(){print('Spacecraft: $name');// Type promotion doesn't work on getters.varlaunchDate=this.launchDate;if(launchDate!=null){intyears=DateTime.now().difference(launchDate).inDays~/365;print('Launched: $launchYear ($years years ago)');}else{print('Unlaunched');}}}// 7. EnumenumPlanetType{terrestrial,gas,ice}/// Enum that enumerates the different planets in our solar system/// and some of their properties.enumPlanet{mercury(planetType:PlanetType.terrestrial,moons:0,hasRings:false),venus(planetType:PlanetType.terrestrial,moons:0,hasRings:false),// ···uranus(planetType:PlanetType.ice,moons:27,hasRings:true),neptune(planetType:PlanetType.ice,moons:14,hasRings:true);/// A constant generating constructorconstPlanet({requiredthis.planetType,requiredthis.moons,requiredthis.hasRings});/// All instance variables are finalfinalPlanetTypeplanetType;finalintmoons;finalboolhasRings;/// Enhanced enums support getters and other methodsboolgetisGiant=>planetType==PlanetType.gas||planetType==PlanetType.ice;}// 8. InheritanceclassOrbiterextendsSpacecraft{doublealtitude;Orbiter(super.name,DateTimesuper.launchDate,this.altitude);}// 9. MixinsmixinPiloted{intastronauts=1;voiddescribeCrew(){print('Number of astronauts: $astronauts');}}// 10. Interfaces and abstract classesabstractclassDescribable{voiddescribe();voiddescribeWithEmphasis(){print('=========');describe();print('=========');}}// 11. AsyncconstoneSecond=Duration(seconds:1);// ···Future<void>printWithDelay(Stringmessage)async{awaitFuture.delayed(oneSecond);print(message);}// 12. Exceptiondoubledivide(inta,intb){if(b==0){throwException('Cannot divide by zero');}returna/b;}
import'package:flutter/material.dart';classMyAppBarextendsStatelessWidget{constMyAppBar({requiredthis.title,super.key});// Fields in a Widget subclass are always marked "final".finalWidgettitle;@overrideWidgetbuild(BuildContextcontext){returnContainer(// CONTAINERheight:56,// in logical pixelspadding:constEdgeInsets.symmetric(horizontal:8),decoration:BoxDecoration(color:Colors.blue[500]),// Row is a horizontal, linear layout.child:Row(children:[constIconButton(icon:Icon(Icons.menu),tooltip:'Navigation menu',onPressed:null,// null disables the button),// Expanded expands its child// to fill the available space.Expanded(child:title,),constIconButton(icon:Icon(Icons.search),tooltip:'Search',onPressed:null,),],),);}}classMyScaffoldextendsStatelessWidget{constMyScaffold({super.key});@overrideWidgetbuild(BuildContextcontext){// Material is a conceptual piece// of paper on which the UI appears.returnMaterial(// Column is a vertical, linear layout.child:Column(children:[MyAppBar(title:Text('Example title',style:Theme.of(context)//.primaryTextTheme.titleLarge,),),constExpanded(child:Center(child:Text('Hello, world!'),),),],),);}}voidmain(){runApp(constMaterialApp(title:'My app',// used by the OS task switcherhome:SafeArea(child:MyScaffold(),),),);}