Flutter Notes
Flutter Notes
Recording Link:
E
Widgets:
C
themes that are required to build a material design app.
Documentation - https://api.flutter.dev/flutter/material/MaterialApp-class.html
Video - Flutter Widget Basics - MaterialApp
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Home'),
W
SC
),
),
debugShowCheckedModeBanner: false,
)
2 - Scaffold: Scaffold Widget is used under MaterialApp, it gives you many basic functionalities,
D
Scaffold(
appBar: AppBar(
//The basic mobile appbar, looks and works great
title: const Text('Sample Code'),
),
body: Center(child: Text('You have pressed the button $_count times.')),
//Body uses the most space - when using a textfield(or other widgets with focusnodes,
wrap the body with a gesturedetector and set an ontap to FocusScope.of(context).unfocus();
to undofcus when tapping of the widget ;)
floatingActionButton: FloatingActionButton(
//FAB is great and a defined in the scaffold
onPressed: () => setState(() => _count++),
tooltip: 'Increment Counter',
child: const Icon(Icons.add),
),
);
E
sizing widgets.
Documentation - https://api.flutter.dev/flutter/widgets/Container-class.html
Video - Container (Flutter Widget of the Week)
C
Container(
alignment: Alignment.center,
child: Text('Container'),
width: 200.0,
height: 200.0,
decoration: BoxDecoration(
color: Colors.purple[200],
W
SC
borderRadius: BorderRadius.circular(60.0)
)
),
4 - Center: A Widget that helps you orient things in the centre of your screen.
Documentation - Center class - widgets library - Dart API
D
Video - https://youtu.be/c726pWTtxqI
G
Center(
child: Text('Center'),
)
E
body: Image.asset('assets/images/lake.jpg'), // <--- image
),
);
}
C
}
W
6 - Text - This allows you to add text to your application.
Documentation - Text class - dart:html library
Video - Flutter Text Widget | How, what and why
SC
Text(
'Hello',
textAlign: TextAlign.center,
style: const TextStyle(fontWeight: FontWeight.bold),
)
D
7 - RichText (New Widget): The RichText widget displays text that uses multiple different
styles.
Documentation - RichText class - widgets library - Dart API
Video - RichText (Flutter Widget of the Week)
G
RichText(
text: TextSpan(
text: 'Hello ',
style: DefaultTextStyle.of(context).style,
children: const <TextSpan>[
TextSpan(text: 'bold', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: ' world!'),
],
),
8 - SafeArea:
Documentation - SafeArea class - widgets library - Dart API
Video - SafeArea (Flutter Widget of the Week)
E
SafeArea(
bottom: true,
C
left: true,
top: true,
right: true,
maintainBottomViewPadding: true,
),
minimum: EdgeInsets.zero,
child: Scaffold(
W
appBar: AppBar(title: Text('SizedBox')),
),
SC
Single-Child Layout Widgets are the ones that will accept only one widget as their child
Such as: Container(), Center(), Expanded(), Align(), SizedBox() etc.
D
Multi-Child Layout Widgets are the ones that will accept more than one widget as their
child
G
In very simple terms Whenever you see or use a widget with a child property, That
widget is called Single-Child Layout
Container(child: Text());
Center(child: Text());
Expanded(child: Text());
Align(child: Text());
SizedBox(child: Text());
And Whenever you see or use a widget with a children property, That widget is
called Multi-Child Layout*
Column(children: [Text(), Text(), Text(), Text()]); Row(children: [Text(), Text(), Text(), Text()]);
Stack(children: [Text(), Text(), Text(), Text()]);
Single-Child Layout Widget is used as Wrapper for other widgets for changing their
positions, width, height, adding space, margin, aligning content, etc.
Multi-Child Layout Widget is used for creating a list of widgets in the horizontal or
vertical direction, also for stacking widgets one over another, arranging data as well as
widgets in a table structure, etc.
E
9 - Column Widget:
C
Documentation - Column class - widgets library - Dart API
Video - https://youtu.be/siFU8c_Heu0
Column(children: [Text(),
Text(),
Text(),
Text()
]);
W
SC
10 - Row Widget:
Documentation - Row class - widgets library - Dart API
Video - Flutter Tutorial for Beginners #11 - Rows
D
Row(children: [Text(),
Text(),
G
Text(),
Text()
]);
For Row:
mainAxisAlignment = Horizontal Axis
crossAxisAlignment = Vertical Axis
For Column:
E
mainAxisAlignment = Vertical Axis
crossAxisAlignment = Horizontal Axis
C
W
SC
11 - Circle Avatar: A circle that represents a user. Typically used with a user's profile image, or,
D
in the absence of such an image, the user's initials. A given user's initials should always be
paired with the same background colour, for consistency.
G
CircleAvatar(
backgroundImage: NetworkImage(userAvatarUrl),
)
12 - Padding:
Documentation - Padding class - widgets library - Dart API
Video - Padding (Flutter Widget of the Week)
Padding(
padding: EdgeInsets.only(top: 10.0, bottom: 10.0),
padding: EdgeInsets.symmetric(vertical: 10.0,),
child: Text('Padding'),
),
13 - Divider - Adds a line in your application. Works very similar to <hr> tag in HTML
E
Documentation - Divider class - material library - Dart API
Video - Divider (Flutter Widget of the Week)
C
const Divider(
thickness: 5, // thickness of the line
indent: 20, // empty space to the leading edge of divider.
),
W
endIndent: 20, // empty space to the trailing edge of the divider.
color: Colors.black, // The color to use when painting the line.
height: 20, // The divider's height extent.
SC
14 - Card - A material design card: a panel with slightly rounded corners and an elevation
shadow. A card is a sheet of Material used to represent some related information, for
example, an album, a geographical location, a meal, contact details, etc.
Documentation - Card class - material library - Dart API
Card(
shape: RoundedRectangleBorder(
G
borderRadius: BorderRadius.circular(15.0),
),
color: Colors.lightBlueAccent.withOpacity(0.5),
),
15 - ListTile - A single fixed-height row that typically contains some text as well as a leading
or trailing icon.
ListTile(
title: Text('ListTile'),
isThreeLine: true,
subtitle: Text('Secondary text\nTertiary text'),
leading: Icon(Icons.label),
E
trailing: Text('Metadata'),
),
C
16 - Icons -
Video - https://youtu.be/ABmqtI7ec7E
Icon(
Icons.home,
color: Colors.blue,
W
size: 40.0,
SC
textDirection: TextDirection.ltr,
semanticLabel: 'Icon', // Announced in accessibility modes (e.g TalkBack/VoiceOver).
This label does not show in the UI.
),
17 - Buttons -
D
Video - https://youtu.be/ABmqtI7ec7E
G
FlatButton(
color: Colors.red,
splashColor: Colors.black12,
onPressed: (){
},
child: Text(
"Nouvelle Texte"
),
)
18 - Expanded -
Documentation - https://api.flutter.dev/flutter/widgets/Expanded-class.html
Expanded(
child: Container(
height: 100.0,
E
color: Colors.green,
width: 100.0,
),
C
)
Stateless Widget:
W
Note about State Widget - Stateful and Stateless Widgets
3. They can be updated during runtime based on user action or data change.
4. Stateful Widgets have an internal state and can re-render if the input data
changes or if the Widget’s state changes.
5. For Example Checkbox, Radio Button, Slider are Stateful Widgets
G
E
Type Syntax
A variable must be declared before it is used. Dart uses the var keyword to achieve the
same. The syntax for declaring a variable is as given below −
C
var name = 'Smith';
All variables in dart store a reference to the value rather than containing the value. The
W
variable called name contains a reference to a String object with a value of “Smith”.
SC
Dart supports type-checking by prefixing the variable name with the data type. Type-checking
ensures that a variable holds only data specific to a data type. The syntax for the same is
given below −
String name = 'Smith';
int num = 10;
D
}
The above snippet will result in a warning since the value assigned to the variable doesn’t
match the variable’s data type.
Output
Warning: A value of type 'String' cannot be assigned to a variable of type 'int'
Functions in Dart
Functions are the building blocks of readable, maintainable, and reusable code. A function is
a set of statements to perform a specific task. Functions organize the program into logical
blocks of code. Once defined, functions may be called to access code. This makes the code
reusable. Moreover, functions make it easy to read and maintain the program’s code.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
E
1 Defining a Function
C
A function definition specifies what and how a specific task would be done.
2 Calling a Function
W
A function must be called so as to execute it.
SC
3 Returning Functions
Functions may also return value along with control, back to the caller.
4 Parameterized Function
D
Optional Parameters
Optional parameters can be used when arguments need not be compulsorily passed for a
function’s execution. A parameter can be marked optional by appending a question mark to
its name. The optional parameter should be set as the last argument in a function.
We have three types of optional parameters in Dart −
Sr.No Parameter & Description
E
Unlike positional parameters, the parameter's name must be specified while
the value is being passed. Curly brace {} can be used to specify optional
C
named parameters.
3
W
Optional Parameters with Default Values
Example
void main() {
G
print(factorial(6));
}
factorial(number) {
if (number <= 0) {
// termination case
return 1;
} else {
return (number * factorial(number - 1));
// function invokes itself
}
}
It should produce the following output −
720
Lambda Functions
Lambda functions are a concise mechanism to represent functions. These functions are also
called as Arrow functions.
Syntax
[return_type]function_name(parameters)=>expression;
E
Example
C
void main() {
printMsg();
print(test());
}
printMsg()=>
print("hello");
int test()=>123;
// returning function
W
It should produce the following output −
SC
hello 123
19 - Navigator:
D
ListTile(
title:Text("Settings",style:TextStyle(fontSize:20.0),),
leading:Icon(Icons.home),
onTap:(){
Navigator.push(context,MaterialPageRoute(builder:
(context)=>A()
));
},
),
20 - Webview:
Documentation - webview_flutter | Flutter Package
Video: Flutter Tutorial - WebView App | The Right Way [2021] 1/3 Load URL, HTML, Java…
@override
_AState createState() => _AState();
}
E
class _AState extends State<A> {
@override
Widget build(BuildContext context) {
C
return WebView(
initialUrl: 'https://www.youtube.com/watch?v=N7BvXrOYQTY',
);
}
}
W
21 - URL Launcher : A Flutter plugin for launching a URL
SC
Documentation: https://pub.dev/packages/url_launcher
VIdeo: Flutter Tutorial - How To Use URL Launcher [2021] Open URL In Web Browser / In …
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
D
home: Material(
child: Center(
child: RaisedButton(
onPressed: _launchURL,
child: Text('Show Flutter homepage'),
),
),
),
),
);
void _launchURL() async {
if (!await launch(_url)) throw 'Could not launch $_url';
}
E
C
W
SC
D
G