Strong Typing and Static Analysis

1. Advantage

Dart has a strong static type system and supports static analysis tools. This helps catch errors early in development and enhances code reliability and maintainability.

October 16, 2024 / 0 Comments
read more

Cross-Platform Development

1. Advantage

Dart enables developers to build applications that run on multiple platforms, including mobile (iOS and Android), web, and desktop. This reduces development time and effort by utilizing a single codebase.

October 16, 2024 / 0 Comments
read more

Flutter Integration

1. Advantage

Dart is the primary language for developing apps using the Flutter framework. Flutter’s hot-reload feature and Dart’s reactive programming capabilities allow for faster development and real-time updates.

October 16, 2024 / 0 Comments
read more

Ease of Learning

1. Advantage

Dart has a simple and intuitive syntax, making it easy for developers to learn and understand. Its familiarity with languages like Java and JavaScript also aids in the learning process.

October 16, 2024 / 0 Comments
read more

Efficient Performance

1. Advantage

Dart is known for its fast execution and efficiency, making it suitable for applications that require quick response times and smooth performance.

October 16, 2024 / 0 Comments
read more

TextField

3. Flutter Widgets

A TextField or TextBox is an input element which holds the alphanumeric data, such as name, password, address, etc. It is a GUI control element that enables the user to enter text information using a programmable code. It can be of a single-line text field (when only one line of information is required) or multiple-line text field (when more than one line of information is required). TextField in Flutter is the most commonly used text input widget that allows users to collect inputs from the keyboard into an app. We can use the TextField widget in building forms, sending messages, creating search experiences, and many more. By default, Flutter decorated the TextField with an underline. We can also add several attributes with TextField, such as label, icon, inline hint text, and error text using an InputDecoration as the decoration. If we want to remove the decoration properties entirely, it is required to set the decoration to null. The following code explains a demo example of TextFiled widget in Flutter: Some of the most common attributes used with the TextField widget are as follows: We are going to see how to use TextField widget in the Flutter app through the following steps: Step 1: Create a Flutter project in the IDE you used. Here, I am going to use Android Studio. Step 2: Open the project in Android Studio and navigate to the lib folder. In this folder, open the main.dart file and import the material.dart package as given below: Step 3: Next, call the main MyApp class using void main run app function and then create your main widget class named as MyApp extends with StatefulWidget: Step 4: Next, we need to create the Scaffold widget -> Column widget in the class widget build area as given below: Step 5: Finally, create the TextField widget as the below code. Let us see the complete source code that contains the TextField Widget. This Flutter application takes two TextFields and one RaisedButton. After filling the details, the user clicks on the button. Since we have not specified any value in the onPressed () property of the button, it cannot print them to console. Replace the following code in the main.dart file and see the output. Output When we run the application in android emulator, we should get UI similar to the following screenshot: If we click inside the text box, we can see that a keyboard has appeared from the bottom of the screen, the label goes into the top left corner of the border, and the hint text shown into the field. The below screenshot explains it more clearly: How to retrieve the value of a TextField? We know that Flutter does not have an ID like in Android for the TextField widget. Flutter allows the user to retrieve the text in mainly two ways: First is the onChanged method, and another is the controller method. Both are discussed below: 1. onChanged method: It is the easiest way to retrieve the text field value. This method store the current value in a simple variable and then use it in the TextField widget. Below is the sample example: 2. Controller method: It is a popular method to retrieve text field value using TextEditingController. It will be attached to the TextField widget and then listen to change and control the widget’s text value. Below is the sample code: Sample code for listening to the changes. Sample code to get or set the value. Let us see the second way in detail to retrieve the text field value in Flutter application with the help of following steps: Now, create a new Flutter project in your IDE and open the main.dart file. Replace the below code in the main.dart file. In this example, we are going to display the alert dialog with the current value of the text field when the user taps on a button. Output: When we run the application in android emulator, we should get UI similar to the following screenshot: Click inside the text box and add values, as shown in the field. When we press the sign-in button, an alert dialog box appeared containing the text that the user has entered into the field. If we click on the OK button, the alert dialog will disappear. Look into the below screenshot: How to make TextField expandable? Sometimes, we want to expand a TextField that means it can have more than one line. Flutter can do this very easily by adding the attributes maxLines and set it to null, which is one by default. We can also specify the exact value to expand the number of lines by default. How to control the size of TextField value? TextField widget also allows us to restrict the maximum number of characters inside the text field. We can do this by adding the maxLength attributes as below: How to obscure text field value? Obscure means to make the field not readable or not understandable easily. The obscure text cannot visible clear. In Flutter, it is mainly used with a text field that contains a password. We can make the value in a TextField obscure by setting the obscureText to true.

October 12, 2024 / 0 Comments
read more

Text

3. Flutter Widgets

A Text is a widget in Flutter that allows us to display a string of text with a single line in our application. Depending on the layout constraints, we can break the string across multiple lines or might all be displayed on the same line. If we do not specify any styling to the text widget, it will use the closest DefaultTextStyle class style. This class does not have any explicit style. In this article, we are going to learn how to use a Text widget and how to style it in our application. Here is a simple example to understand this widget. This example shows our project’s title in the application bar and a message in the application’s body. In the above code, we have used a MaterialApp widget that calls the home screen using the MyTextPage() class. This class contains the scaffold widget, which has appBar and body where we have used the Text widget to display the title and body, respectively. It is a simple scenario of Text widget where we have to pass the string that we want to display on our page. When we run this application in the emulator or device, we should get the UI similar to the below screenshot: Text Widget Constructor: The text widget constructor used to make the custom look and feel of our text in Flutter: The following are the essential properties of the Text widget used in our application: TextAlign: It is used to specify how our text is aligned horizontally. It also controls the text location. TextDirection: It is used to determine how textAlign values control the layout of our text. Usually, we write text from left to right, but we can change it using this parameter. Overflow: It is used to determine when the text will not fit in the available space. It means we have specified more text than the available space. TextScaleFactor: It is used to determine the scaling to the text displayed by the Text widget. Suppose we have specified the text scale factor as 1.5, then our text will be 50 percent larger than the specified font size. SoftWrap: It is used to determine whether or not to show all text widget content when there is not enough space available. If it is true, it will show all content. Otherwise, it will not show all content. MaxLines: It is used to determine the maximum number of lines displayed in the text widget. TextWidthBasis: It is used to control how the text width is defined. TextHeightBehavior: It is used to control how the paragraph appears between the first line and descent of the last line. Style: It is the most common property of this widget that allows developers to styling their text. It can do styling by specifying the foreground and background color, font size, font weight, letter and word spacing, locale, shadows, etc. See the table to understand it more easily: Attributes Descriptions foreground It determines the paint as a foreground for the text. background It determines the paint as a background for the text. fontWeight It determines the thickness of the text. fontSize It determines the size of the text. fontFamily It is used to specify the typeface for the font. For this, we need to download a typeface file in our project, and then keep this file into the assets/font folder. Finally, config the pubspec.yaml file to use it in the project. fontStyle It is used to style the font either in bold or italic form. Color It is used to determine the color of the text. letterSpacing It is used to determine the distance between the characters of the text. wordSpacing It is used to specify the distance between two words of the text. shadows It is used to paint underneath the text. decoration We use this to decorate text using the three parameters: decoration, decorationColor, decorationStyle. The decoration determines the location, decorationColor specify the color, decorationStyle determine the shape. Output: When we run this application in the emulator or device, we should get the UI similar to the below screenshot: Flutter RichText Widget Sometimes we want to show a line or a paragraph with multiple styles such as bold, italicized, underlined, different color, different font or everything at once. In that case, we should have to use the RichText widget that allows us to perform multiple test styles without switching many widgets. RichText is a very useful widget in Flutter, which is used for displaying a paragraph of text on the UI with multiple styles. Inside the widget, we can have different styles by giving it a tree of TextSpan widgets. Each TextSpan can set its own style for overriding the default style. RichText Structure The below image explains the structure of the RichText widget. In this image, the parent TextSpan has its own style property and a text parameter, and then it can contain several children TextSpan who have their own style property. From the above overview, now we will see how to use this widget in our application. Example Suppose our app has a login screen and an option to create a new user account. The creation of new user account section has a combination of regular text and colored text, as shown in the below screen: In the above image, we can see the text “Don’t have an account? Sign up” is a single line text, where the last part is a clickable word that navigates the user on the sign-up page. To make this type of text, we will divide it into two parts. The first part uses the parent TextSpan and input the text “Don’t have an account?” with the black color. The second part uses the children TextSpan and input the text “Sign up” with a blue color. See the below code: Since the “Sign up” is a clickable text, so we need to implement onTap() action on this part. The TextSpan contains TapGestureRecognizer() that implement onTap() action. In our example, we will use the recognizer property to make the text tappable. Let us show the entire code snippet to understand it more clearly. Create a new project in the IDE you are using. Open the project, navigate to the lib folder, and replace the below code with the main.dart file. Output: When we run this application in the

October 12, 2024 / 0 Comments
read more

Row and Column

3. Flutter Widgets

In the previous sections, we have learned to create a simple Flutter application and its basic styling to the widgets. Now, we are going to learn how to arrange the widgets in rows and columns on the screen. The rows and columns are not a single widget; they are two different widgets, namely Row and Column. Here, we will integrate these two widgets together because they have similar properties that help us understand them efficiently and quickly. Row and column are the two essential widgets in Flutter that allows developers to align children horizontally and vertically according to our needs. These widgets are very necessary when we design the application user interface in Flutter. Key Points Row Widget This widget arranges its children in a horizontal direction on the screen. In other words, it will expect child widgets in a horizontal array. If the child widgets need to fill the available horizontal space, we must wrap the children widgets in an Expanded widget. A row widget does not appear scrollable because it displays the widgets within the visible view. So it is considered wrong if we have more children in a row which will not fit in the available space. If we want to make a scrollable list of row widgets, we need to use the ListView widget. We can control how a row widget aligns its children based on our choice using the property crossAxisAlignment and mainAxisAlignment. The row’s cross-axis will run vertically, and the main axis will run horizontally. See the below visual representation to understand it more clearly. Note: Flutter row widget has several other properties like mainAxisSize, textDirection, verticalDirection, etc. Here, we will discuss only mainAxisAlignment and crossAxisAlignment properties. We can align the row’s children widget with the help of the following properties: Let us understand it with the help of an example where we are going to align the content such that there is an even space around the children in a row: Output: When we run this app, we should get the UI as the below screenshot. Column This widget arranges its children in a vertical direction on the screen. In other words, it will expect a vertical array of children widgets. If the child widgets need to fill the available vertical space, we must wrap the children widgets in an Expanded widget. A column widget does not appear scrollable because it displays the widgets within the visible view. So it is considered wrong if we have more children in a column which will not fit in the available space. If we want to make a scrollable list of column widgets, we need to use the ListView Widget. We can also control how a column widget aligns its children using the property mainAxisAlignment and crossAxisAlignment. The column’s cross-axis will run horizontally, and the main axis will run vertically. The below visual representation explains it more clearly. Note: Column widget also aligns its content by using the same properties as we have discussed in row widget such as start, end, center, spaceAround, spaceBetween, and spaceEvenly. Let us understand it with the help of an example where we are going to align the content such that there is a free space between the children evenly in a column: Output: When we run this app, we should get the UI as the below screenshot. Flutter also allows developers to align the child widget with a combination of crossAxisAlignment and mainAxisAlignment for both row and column widget. Let us take the above example of column widget where we will set mainAxisAlignment as MainAxisAlignment.spaceAround and crossAxisAlignment is CrossAxisAlignment.stretch. It will make the height of the column is equal to the height of the body. See the below screenshot. Drawbacks OF Row and Column Widget:

October 12, 2024 / 0 Comments
read more

Container

3. Flutter Widgets

The container in Flutter is a parent widget that can contain multiple child widgets and manage them efficiently through width, height, padding, background color, etc. It is a widget that combines common painting, positioning, and sizing of the child widgets. It is also a class to store one or more widgets and position them on the screen according to our needs. Generally, it is similar to a box for storing contents. It allows many attributes to the user for decorating its child widgets, such as using margin, which separates the container with other contents. A container widget is same as <div> tag in html. If this widget does not contain any child widget, it will fill the whole area on the screen automatically. Otherwise, it will wrap the child widget according to the specified height & width. It is to note that this widget cannot render directly without any parent widget. We can use Scaffold widget, Center widget, Padding widget, Row widget, or Column widget as its parent widget. Why we need a container widget in Flutter? If we have a widget that needs some background styling may be a color, shape, or size constraints, we may try to wrap it in a container widget. This widget helps us to compose, decorate, and position its child widgets. If we wrap our widgets in a container, then without using any parameters, we would not notice any difference in its appearance. But if we add any properties such as color, margin, padding, etc. in a container, we can style our widgets on the screen according to our needs. A basic container has a margin, border, and padding properties surrounding its child widget, as shown in the below image: Constructors of the container class The following are the syntax of container class constructor: Properties of Container widget Let us learn some of the essential properties of the container widget in detail. 1. child: This property is used to store the child widget of the container. Suppose we have taken a Text widget as its child widget that can be shown in the below example: 2. color: This property is used to set the background color of the text. It also changes the background color of the entire container. See the below example: 3. height and width: This property is used to set the container’s height and width according to our needs. By default, the container always takes the space based on its child widget. See the below code: 4. margin: This property is used to surround the empty space around the container. We can observe this by seeing white space around the container. Suppose we have used the EdgeInsets.all(25) that set the equal margin in all four directions, as shown in the below example: 5. padding: This property is used to set the distance between the border of the container (all four directions) and its child widget. We can observe this by seeing the space between the container and the child widget. Here, we have used an EdgeInsets.all(35) that set the space between text and all four container directions: 6. alignment: This property is used to set the position of the child within the container. Flutter allows the user to align its element in various ways such as center, bottom, bottom center, topLeft, centerRight, left, right, and many more. In the below example, we are going to align its child into the bottom right position. 7. decoration: This property allows the developer to add decoration on the widget. It decorates or paint the widget behind the child. If we want to decorate or paint in front of a child, we need to use the forgroundDecoration parameter. The below image explains the difference between them where the foregroundDecoration covers the child and decoration paint behind the child. The decoration property supported many parameters, such as color, gradient, background image, border, shadow, etc. It is to make sure that we can either use the color property in a container or decoration, but not in both. See the below code where we have added a border and shadow property to decorate the box: We will see the output as below screenshot:

October 12, 2024 / 0 Comments
read more

Scaffold

3. Flutter Widgets

The Scaffold is a widget in Flutter used to implements the basic material design visual layout structure. It is quick enough to create a general-purpose mobile application and contains almost everything we need to create a functional and responsive Flutter apps. This widget is able to occupy the whole device screen. In other words, we can say that it is mainly responsible for creating a base to the app screen on which the child widgets hold on and render on the screen. It provides many widgets or APIs for showing Drawer, SnackBar, BottomNavigationBar, AppBar, FloatingActionButton, and many more. The Scaffold class is a shortcut to set up the look and design of our app that allows us not to build the individual visual elements manually. It saves our time to write more code for the look and feel of the app. The following are the constructor and properties of the Scaffold widget class. Let us understand all of the above properties in detail. 1. appBar: It is a horizontal bar that is mainly displayed at the top of the Scaffold widget. It is the main part of the Scaffold widget and displays at the top of the screen. Without this property, the Scaffold widget is incomplete. It uses the appBar widget that itself contains various properties like elevation, title, brightness, etc. See the below example: In the above code, the title property uses a Text widget for displaying the text on the screen. 2. body: It is the other primary and required property of this widget, which will display the main content in the Scaffold. It signifies the place below the appBar and behind the floatingActionButton & drawer. The widgets inside the body are positioned at the top-left of the available space by default. See the below code: In the above code, we have displayed a text “Welcome to Javatpoint!!” in the body attribute. This text is aligned in the center of the page by using the Center widget. Here, we have also styled the text by using the TextStyle widget, such as color, font size, etc. 3. drawer: It is a slider panel that is displayed at the side of the body. Usually, it is hidden on the mobile devices, but the user can swipe it left to right or right to left to access the drawer menu. It uses the Drawer widget properties slides in a horizontal direction from the Scaffold edge to show navigation links in the application. An appropriate icon for the drawer is set automatically in an appBar property. The gesture is also set automatically to open the drawer. See the following code. In the above code, we use the drawer property of Scaffold for creating a drawer. We have also used some other widgets to make it attractive. In the ListView widget, we have divided the panel into two parts, Header and Menu. The DrawerHeader property modifies the panel header that also contains an icon or details according to the application. Again, we have used ListTile to add the list items in the menu. 4. floatingActionButton: It is a button displayed at the bottom right corner and floating above the body. It is a circular icon button that floats over the content of a screen at a fixed place to promote a primary action in the application. While scrolling the page, its position cannot be changed. It uses the FloatingActionButton widget properties using Scaffold.floatingActionButton. See the below code: In the above code, we have used the elevation property that gives a shadow effect to the button. We have also used the Icon widget to give an icon to the button using preloaded Flutter SDK icons. The onPressed() property will be called when the user taps the button, and the statements “I am Floating Action Button” will be printed on the console. 5. backgroundColor: This property is used to set the background color of the whole Scaffold widget. 6. primary: It is used to tell whether the Scaffold will be displayed at the top of the screen or not. Its default value is true that means the height of the appBar extended by the height of the screen’s status bar. 7. persistentFooterButton: It is a list of buttons that are displayed at the bottom of the Scaffold widget. These property items are always visible; even we have scroll the body of the Scaffold. It is always wrapped in a ButtonBar widget. They are rendered below the body but above the bottomNavigationBar. In the above code, we have used the RaisedButton that displays at the bottom of the Scaffold. We can also use the FlatButton instead of the RaisedButton. 8. bottomNavigationBar: This property is like a menu that displays a navigation bar at the bottom of the Scaffold. It can be seen in most of the mobile applications. This property allows the developer to add multiple icons or texts in the bar as items. It should be rendered below the body and persistentFooterButtons. See the below code: In the above code, we have used the BottomNavigationBar widget to display the menu bar. The fixedColor property is used for the color of the active icon. The BottomNavigationBarItems widget is used to add items in the bar containing text and icon as its child property. We had also used onTap(int itemIndex) function to perform an action when we tapped on the items, which works according to their index position. 9. endDrawer: It is similar to a drawer property, but they are displayed at the right side of the screen by default. It can be swiped right to left or left to right. 10. resizeToAvoidBottomInset: If it is true, the body and the Scaffold’s floating widgets should adjust their size themselves to avoid the onscreen keyboard. The bottom property defines the onscreen keyboard height. 11. floatingActionButtonLocation: By default, it is positioned at the bottom right corner of the screen. It is used to determine the position of the floatingActionButton. It contains many predefined constants, such as centerDocked, centerFloat, endDocked, endFloat, etc. That’s all about the Scaffold’s various properties that give us an overview of the Scaffold widget. Its main purpose is to get familiar with different properties and their usage in Flutter application. If we want to learn it in more detail, refer to the flutter documentation here. Let us see the example where we have tried to use most of the Scaffold properties to understand this widget quickly and easily. In this

October 12, 2024 / 0 Comments
read more

Posts pagination

Previous 1 … 109 110 111 … 445 Next