In this adviser we’ll allocution about Google Flutter and appearance you how to body a cantankerous belvedere ‘Hello World’ app.
Android may be one of the best accepted operating systems in the world, but it’s far from the only mobile operating system. If your app is activity to ability the widest accessible audience, again you’ll charge to accomplish it accessible on added platforms, including iOS.
Porting your Android app to iOS requires time and effort. Forth the way you may alpha to admiration whether this is absolutely the best use of your time — wouldn’t it accomplish added faculty to administer on cutting the assignment you’ve done so far, rather than artlessly re-creating it on addition platform? Ideally, you’d be able to cut your development time by creating a distinct app that can run above assorted platforms.
Google Flutter is a user interface (UI) toolkit that promises to do absolutely that, giving you a way to advance UIs for Android and iOS from a distinct codebase.
By the end of this commodity you’ll accept added Flutter abutment to your Android Studio installation, explored some of its amount features, and created a “Hello World” application, accounting absolutely in Google Flutter.
Google Flutter isn’t based on Kotlin or Java. If you appetite to advance for it, you’ll charge to apprentice Dart.
While the anticipation of acquirements yet addition programming accent may be off putting, there are some appealing acute affidavit to accord Flutter a try:
The Google Flutter SDK includes appealing abundant aggregate you’ll charge to get started with Flutter, including the Dart SDK. However, it’s hosted on GitHub, you should apparently carbon this SDK appliance Git, rather than downloading it.
One of the above allowances of cloning a project, is that it’s abundant easier to accumulate up to date with new releases.
Once you’ve installed the latest adaptation of Git, you’re accessible to carbon Flutter:
Google Flutter may additionally acquaint that your Xcode accession is incomplete. You alone charge Xcode if you’re developing for iOS. If you’re alone absorbed in Android, save yourself some time and deejay amplitude by blank this warning.
You can use Google Flutter with any altercation editor, but it’s far easier to use Android Studio’s committed Flutter and Dart plugins.
Assuming you’re active Android Studio 3.0 or later:
The best way to apprentice a new technology, is to use it, so let’s actualize a “Hello World” app appliance Flutter and Dart:
Open your project’s flutter_app/java/main.dart file. The “Flutter application” arrangement contains lots of cipher (which we’ll be exploring later) but for now let’s accumulate things simple and actualize an app that displays a “Hello World” message. Delete aggregate in this book and alter it with the following:
To run this app, chase these steps:
This app may be simple, but it demonstrates some of the amount concepts of developing with Flutter, so let’s breach it down:
In Dart, the => announcement is autograph for { acknowledgment expression; }.
In Flutter, about aggregate is a widget. Some Flutter widgets are the asperous agnate of Android’s Views. For archetype the Altercation accoutrement is agnate to a TextView. However, in Flutter rows, columns, padding, and alike the appliance itself are additionally classed as widgets, which is why our appliance chic extends from commodity alleged StatelessWidget.
StatelessWidgets are widgets that accommodate no State. All of a StatelessWidget’s ethics are final and cannot change at runtime, so the StatelessWidget artlessly renders what’s anesthetized to its constructors.
The adverse of StatelessWidget is a StatefulWidget, which can change at runtime.
The build() adjustment describes how to affectation a widget. Flutter calls build() back it inserts a accoutrement into the accoutrement hierarchy, and again re-runs build() every time that accoutrement needs to be updated.
Flutter comes with an accomplishing of Material Design that you can administer by declaring “MaterialApp” as your app’s top akin widget.
Here, we’re appliance the MaterialApp affair out-of-the-box, but you can adapt this affair by creating a new ThemeData() instance that contains your adapted changes, for example:
The “home” altercation references the accoutrement that defines the capital UI, which in this instance is Scaffold.
A Scaffold accoutrement gives your app a anatomy that follows Material Design guidelines, by accouterment an app bar, title, and accomplishments color. It additionally provides APIs for announcement drawers, snackbars, and basal sheets.
Here, we’re ambience the altercation for our app bar. While apps like Scaffold and AppBar are specific to Material Design apps, Altercation is a all-encompassing accoutrement that you can use in any app.
Center is a blueprint accoutrement that takes a distinct adolescent and positions it in the average of the screen, agnate to a LinearLayout with a axial gravity.
We’ve created our aboriginal Google Flutter application, but it’s not absolutely an agitative app. Acquirements a new toolkit and an absolute programming accent is hardly above the ambit of this article, so for our additional appliance let’s bluff and use a template:
This app counts how abounding times you’ve broke the Floating Action Button.
If you accessible the flutter_app/java/main.dart file, you’ll see the following:
See Also: hack facebookThis automatically-generated cipher demonstrates a cardinal of Flutter appearance we haven’t affected on yet:
In Google Flutter, widgets are either stateless or stateful. A StatefulWidget has Accompaniment advice that can change at runtime, in acknowledgment to user interaction.
A StatefulWidget consists of these things:
You admit the Accompaniment commodity into the accoutrement tree, by calling createState.
You additionally charge to ascertain a setState method, which informs Flutter back the widget’s Accompaniment changes, causing the framework to clean this accurate widget.
In Flutter, you align widgets by agreement them central added widgets, best frequently in two orientations:
Both Rows and Columns accept assorted backdrop that let you ascendancy absolutely area anniversary adolescent appears onscreen, and how abundant amplitude they absorb aural the Row or Column. Here, we’re appliance a mainAxisAlignment acreage to specify that the adolescent widgets should be centered forth the parent’s capital axis:
If the ancestor is a Column, again the capital arbor is the vertical axis. If the ancestor is a Row, again the capital arbor is the accumbent axis.
There’s addition big aberration amid this appliance and our “Hello World” activity — it can ascertain user input!
If a accoutrement already supports accident detection, you aloof charge to canyon a action that can handle the adapted interaction, for example:
The easiest way to analysis whether a accoutrement supports accident detection, is to analysis its documentation. For example, the FloatingActionButton folio makes it bright this accoutrement supports onPressed.
If a accoutrement doesn’t abutment accident detection, you can accomplish it alternate by wrapping it in a GestureDetector widget, which can ascertain gestures like taps, presses, and drags.
In this snippet, GestureDetector responds to user ascribe by calling onTap and again press a bulletin to the Console:
If you’re accustomed with Android Studio’s “Instant Run,” again you’ll apperceive aloof how abundant time you can save by selectively blame changes, rather than architecture a new APK every time you abuse your appliance code.
Google Flutter has a agnate “Hot Reload” affection which lets you advance adapted cipher and assets to a active Dart VM while advancement the application’s state. This appliance is absolute for demonstrating hot reload:
To:
In this article, we got an addition to Google Flutter and Dart — what it is, how to set it up, and how to actualize some simple apps.
Will you be abacus Dart to the account of languages you use for Android development? Or are you afraid with Java (or Kotlin?) Let us apperceive in the comments below!
Why should I use Google Flutter?
Installing Google Flutter and Dart
Creating your aboriginal Google Flutter app
Creating our additional Flutter app: User alternation and StatefulWidgets
Wrapping up
- Cross-platform: By appliance the aforementioned renderer, framework, and libraries, Flutter lets you actualize a distinct UI to run on both Android and iOS. To ensure your appliance feels appropriately at home on either platform, Flutter provides widgets styled according to Cupertino (iOS) and Material Design (Android) guidelines, additional a cardinal of Flutter bales that accord you admission to some platform-specific casework and APIs.
- Compatible with added programming languages: Flutter integrates with Java cipher on Android, and ObjectiveC and Swift on iOS, so you don’t accept to absolutely carbon your absolute applications to alpha appliance Flutter.
- Hot reload. It’s accepted convenance to assignment on your application’s cipher while the appliance itself is active on an Android accessory to analysis changes as you accomplish them. The time it takes Android Studio to advance anniversary set of changes to the active appliance can absolutely eat into your development time. To advice you be added productive, Flutter has a “hot reload” affection that can inject adapted antecedent cipher into a active Dart Virtual Apparatus (VM). Appliance hot reload, you’ll about see the after-effects of your changes in beneath a second. Appliance accompaniment is additionally preserved, so you don’t charge to absorb time recreating the adapted accompaniment manually. If your app has a login awning you won’t accept to re-enter your login accreditation afterward anniversary hot reload.
- Head over to Flutter’s GitHub page.
- Click “Clone or download,” and again archetype the URL.
- Open a Command Alert (Windows) or Terminal (Mac) window, and blazon “git clone,” followed by “-b beta” (since we’re cloning Flutter’s beta branch), and the project’s URL:
- Next, add Flutter to your aisle appliance the afterward command:
- Check whether there’s any missing dependencies or outstanding tasks you charge to accomplish by active the afterward Command Prompt/Terminal command:
- If the consecutive address warns you that your Flutter accession is “x” canicule old, again run the following:
- Select “Android Studio > Preferences…” from the toolbar.
- Select “Plugins” from the left-hand menu.
- Give the “Browse repositories…” button a click.
- Search for “Flutter.” Back the plugin appears, bang its accompanying “Install” button.
- At this point, a popup should alert you to install Dart. Bang “Yes.”
- Restart Android Studio.
- Select “New > New Flutter Project…” from the Android Studio toolbar.
- Select “Flutter appliance > Next.”
- Give your activity a name and specify area it should be stored.
- Point Android Studio in the administration of your Flutter SDK by beat the little “…” figure and abyssal to the “Flutter” binder (on Mac, this is about stored in your Home directory). Bang “Open.”
- Enter your aggregation domain, and again bang “Finish.”
- Launch an Android Virtual Accessory (AVD) or attach a concrete Android accessory to your development machine.
- Open the “Flutter Accessory Selection” dropdown (where the cursor is positioned in the afterward screenshot) and baddest your device.
- Select “Run > main.dart” from the Android Studio toolbar.
- Select “New > New Flutter Project…”
- Select the “Flutter application” template, and again bang “Next.”
- Complete the accepted activity setup.
- Run this app, by aperture the “Flutter Accessory Selection” dropdown and selecting your accessory from the list.
- Select “Run > main.dart” from the Android Studio toolbar.
- A accoutrement that extends the StatefulWidget class, which is immutable. The absolute StatefulWidget is a acting commodity acclimated to assemble the app in its accepted state.
- A Accompaniment class, which contains some approximate abstracts that ability change over the widget’s lifetime. Accompaniment altar abide amid calls to build(), so they can absorb information.
- A Row takes a account of adolescent widgets and displays them horizontally.
- A Column takes a account of adolescent widgets and displays them vertically.
- Click the FAB button a few times so the adverse is announcement annihilation added than 0.
- Make an approximate change to the project’s code, for archetype changing:
- Click the little “Flutter Hot Reload” button in the Android Studio toolbar (it uses absolutely the aforementioned figure as Instant Run).
- Take a attending at your application. The appellation should accept afflicted after resetting the adverse to zero, demonstrating that the appliance accompaniment has been preserved.
git carbon -b beta https://github.com/flutter/flutter.git
export PATH=$PWD/flutter/bin:$PATH
flutter doctor
Flutter upgrade
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Accoutrement build(BuildContext context) { acknowledgment new MaterialApp( title: 'Welcome to Flutter', home: new Scaffold( appBar: new AppBar( title: new Text('This is the appellation text'), ), body: new Center( child: new Text('Hello World'), ), ), ); } }
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override Accoutrement build(BuildContext context) {
return new MaterialApp(
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Accoutrement build(BuildContext context) { acknowledgment new MaterialApp( title: 'Welcome to Flutter', theme: new ThemeData( primaryColor: Colors.white, ), home: new Scaffold( appBar: new AppBar( title: new Text('This is the appellation text'), ), body: new Center( child: new Text('Hello World'), ), ), ); } }
home: new Scaffold(
appBar: new AppBar( title: new Text('This is the appellation text'), ),
body: new Center( child: new Text('Hello World'),
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends StatelessWidget { @override Accoutrement build(BuildContext context) { acknowledgment new MaterialApp( title: 'Flutter Demo', theme: new ThemeData( primarySwatch: Colors.blue, ), home: new MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; //Prefixing an identifier with an accentuate makes it clandestine to its class// @override _MyHomePageState createState() => new _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int _counter = 0; //Increment the adverse afterward the click// abandoned _incrementCounter() { setState(() { _counter++; }); } @override Accoutrement build(BuildContext context) { acknowledgment new Scaffold( appBar: new AppBar( //Set the appbar’s title, by casual the .title authentic in MyHomePage// title: new Text(widget.title), ), body: new Center( //Column is a blueprint widget, agnate to Center// child: new Column( //mainAxisAlignment position the widgets central the Column// mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ new Text( 'You accept pushed the button this abounding times:', ), new Text( '$_counter', style: Theme.of(context).textTheme.display1, ), ], ), ), floatingActionButton: new FloatingActionButton( onPressed: _incrementCounter, //Similar to Android’s contentDescription, tooltips accommodate altercation labels for// //assistive technologies such as awning readers// tooltip: 'Increment', //Display the ‘Add’ icon. See the Icons chic docs for a account of accurate icons// child: new Icon(Icons.add), ), ); } }
class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override //Override the createState() adjustment to attach the _MyHomePageState class// _MyHomePageState createState() => new _MyHomePageState(); } //Add the _MyHomePageState class// class _MyHomePageState extends State<MyHomePage> { int _counter = 0; abandoned _incrementCounter() { setState(() { _counter++; }); }
child: new Column( mainAxisAlignment: MainAxisAlignment.center,
floatingActionButton: new FloatingActionButton( //Pass the onPressed parameter, which is about the agnate of onClick// onPressed: _incrementCounter, tooltip: 'Increment', child: new Icon(Icons.add), ), ); } }
class MyApp extends StatelessWidget { @override Accoutrement build(BuildContext context) { acknowledgment new Scaffold( body: new Center( child: new GestureDetector( child: new FlutterLogo( size: 300.0, ), //When the adolescent accoutrement is tapped....// onTap: () { //...print the following// print("onTap!"); }, ), )); } }
home: new MyHomePage(title: 'Flutter Demo Home Page'),
home: new MyHomePage(title: 'Flutter'),
Comments
Post a Comment