Slideshow Modul 1 - Hello Flutter
Slideshow Modul 1 - Hello Flutter
Fitri Wibowo
► It's easy to learn: If you have some knowledge of Java, C#, or JavaScript, Dart
will be extremely easy to learn, as you'll see in the next few pages.
► It's aimed at productivity: Its syntax is exceptionally concise and easy to read
and debug.
► It can transpile to JavaScript, in order to maximize compatibility with web
development.
► It has a general purpose: You can use it for client-side, server-side, and
mobile development.
► As an added bonus, Google is deeply involved in this project and has some big
plans for Dart, including a new operating system, called Google Fuchsia.
Hello dart
► https://dartpad.dev/
► Write the following code & run
void main() {
String name = "Dart";
print("Hello $name!");
}
Program penghitung luas
► main() method:
void main() {
double result = calculateArea (12, 5, false);
print('The result for a rectangle is ' + result.toString());
result = calculateArea (12, 5, true);
print('The result for a triangle is ' + result.toString());
}
Program penghitung luas (lanjutan)
► Result:
Foor loop dan string
► Dart supports the same loops as many other C-influenced languages: the for,
while, and do while loops
► An example, a program that reverse a string.
► main() method:
void main() {
String myString = 'Throw your Dart' ;
String result = reverse(myString) ;
print(result);
}
Foor loop dan string (lanjutan)
► Ternary operator:
bool convertToBoolLong (int value)
{
if (value == 1) {
return false; bool convertToBool (int value) => (value == 1) ? false : true;
} else {
return true;
}
While loop, list, dan generic
► To define a collection in Dart, you use List objects. It’s called array in
another programming language.
► Example: void main() {
String mySongs = sing();
print(mySongs);
}
String sing() {
var songs = List<String>();
var songString = '';
songs.add('We will Rock You');
songs.add('One');
songs.add('Sultans of Swing');
int i = 0;
while (i < songs.length) {
songString += '${songs[i]} - ';
i++;
}
return songString;
}
While loop, list, dan generic (lanjutan)
► Result:
foreach()
► The for and while loops can be generally used for any type of loop, but lists
also have some specific methods that help us write elegant and readable
code.
► The foreach() method of a lists lets us run a function on each element in the
array.
► In the previous code, you could delete while loop and use this code instead
songs.forEach((song) => songString += song + " - ");
map()
► The map() method transforms each element in a list and return the result of the
transformation list.
► Example: void main() {
String mySongs = sing();
print(mySongs);
}
String sing() {
var songs = List<String>();
songs.add('We will Rock You');
songs.add('One');
songs.add('Sultans of Swing');
var capitalSongs = songs.map((song) => song.toUpperCase());
return capitalSongs.toString();
}
► Result:
where()
String sing() {
var songs = List<String>();
songs.add('We will Rock You' );
songs.add('One');
songs.add('Sultans of Swing' );
var wSongs = songs.where((song) => song.contains('w'));
return wSongs.toString();
}
► The where() method only returns the elements that satisfy song.contains(‘w’)
Classes and objects
► Example:
class Person {
String name;
String surname;
}
main() {
Person clark = Person();
clark.name = 'Clark';
clark.surname = 'Kent';
print('${clark.name} ${clark.surname}');
}
► In Dart, there are no identifiers such as private or public. Each property of a class
is considered public unless its name begins with an underscore character (_)
Menggunakan getter and setter
► Example: class Person {
String name, surname;
int _age;
set age(int years) {
if (years > 0 && years < 120) {
_age = years;
} else {
_age = 0;
}
}
main() {
Person clark = Person();
clark.name = 'Clark';
clark.surname = 'Kent';
clark.age = 30;
print('${clark.name} ${clark.surname} ${clark.age}');
}
Constructors
► Example: main() {
Person clark = Person('Clark', 'Kent');
print('${clark.name} ${clark.surname}');
}
class Person {
String name, surname;
Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
}
this keyword
► Example:
main() { main() {
Person clark = Person('Clark', 'Kent'); Person clark = Person('Clark', 'Kent');
print('${clark.name} ${clark.surname}'); print('${clark.name} ${clark.surname}');
} }
4. Hasil
Menjalankan “Hello World” Flutter
(lanjutan)
5. Buka project di code editor, dan ubah isi lib/main.dart menjadi:
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
body: Center(
► Ubah kode class child: Column(children: [
MyApp pada property Text(
body di main.dart 'Hello World Travel' ,
dengan kode berikut: style: TextStyle(
fontSize : 26,
fontWeight : FontWeight .bold,
color : Colors.blue[800]),
),
Text(
'Discover the World' ,
style: TextStyle(fontSize: 20, color: Colors.deepPurpleAccent) ,
)
]))
Menampilkan citra digital dari internet
dengan Image
► Kode untuk menampilkan image dari URL
Image.network(
'https://media-cdn.tripadvisor.com/media/photo-s/0c/6a/e4/48/bukit-
rindu-alam-tanjung.jpg',
height: 250,
),
Menambahkan button
ElevatedButton(
child: Text('Contact Us'),
onPressed: () => true,
),
Menampilkan AlertDialog saat button
di-klik
► Untuk menampilkan AlertDialog membutuhkan beberapa langkah:
► Buat sebuah void method dengan yang menerima parameter BuildContext
► Buat sebuah method didalam method pada langkah 1 misalkan showDialog()
► Setting context
► Setting builder
► Kembalikan properti AlertDialog
► Setting properti AlertDialog
Menampilkan AlertDialog saat button
di-klik (lanjutan)
► Contoh kode showDialog() di dalam void contactUs(BuildContext context)
ElevatedButton (
child: Text('Contact Us' ),
onPressed : () => contactUs(context),
),
Hasil
Menggunakan padding
► Properti: padding
► Nilai:
► EdgeInsets.all(24) // Semua
► EdgeInsets.only(right:80) // Kanan saja
► EdgeInsets.symmetric(vertical:48.5) // Atas bawah
Menggunakan SingleChildScrollView
► Always check your app in every orientation when developing for mobile.
► To fix bottom overflow:
…
builder: (context) => SingleChildScrollView(
child: Padding(
…
End