Install the Flutter and Dart plugins in Android Studio
We select Projects and then New Flutter Project
We see the folders and files new Flutter project structure
We select Chrome (web) in the dropdown list:
We can verify the application is running
We select Windows (desktop) in the dropdown list:
We select the Device Manager button
We create a new Device pressing the button Create Device
After creating the device we select the Launch this AVD in the emulator button
We can see the new Mobile Device Simulator is running
If we want to increase the Device size we first press in the Window button
Then we have separate window for the device
Now for running the application we select the device in the dropdonw list Pixel 7 PRO API 34 (mobile) and then we press the play button
We verify the application in running in the Mobile simulator
We press the + button and we see the counter value is increasing each time we press that button
We first import the material.dart library
import 'package:flutter/material.dart';
We run the application inside the main() function:
void main() {
runApp(const MyApp());
}
We create a StatelessWidget object called MyApp:
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
As you see inside the MyApp StatelessWidget we set the application title and the application theme
Also inside the MyApp StatelessWidget we create a new MyHomePage StatefulWidget
We create a new State inside the MyHome
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),//
],//children: <Widget>
),//Column
),//body Center
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), //floatingActionButton
); //Scaffold
}//Widget build
}//class _MyHomePageState extends State<MyHomePage>
The application source code is defined in the file lib/main.dart:
lib/main.dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}