week2@flutterUI (2)
week2@flutterUI (2)
EXPERIMENT-2
AIM:
DESCRIPTION:
Flutter Widgets:
In Flutter, the application is itself a widget. The application is the top- level widget and
its UI is build using one or more children (widgets), which again build using its children
widgets. This composability feature helps us to create a user interface of any complexity.
For example, the widget hierarchy of the hello world application (created in previous
chapter) is as specified in the following diagram –
Types of Widget:
Visible widget
The visible widgets are related to the user input and output data. Some of the important types of
this widget are:
a. Text:
A Text widget holds some text to display on the screen. We can align the text widget by using
textAlign property, and style property allow the customization of Text that includes font, font
weight, font style, letter spacing, color, and many more. We can use it as like below code
snippets.
CODE:
new Text(
'Hello VBIT!',
textAlign: TextAlign.center,
b. Image:
This widget holds the image which can fetch it from multiple sources like from the asset folder
or directly from the URL. It provides many constructors for loading image, which are given
below:
To add an image in the project, you need first to create an assets folder where you keep your
images and then add the below line in pubspec.yaml file.
assets:
- assets/
Now, add the following line in the dart file.
SYNTAX: Image.asset('assets/computer.png')
The complete source code for adding an image is shown below in the hello world example.
CODE:
OUTPUT:
b) Implement different layout structures using Row, Column, and Stack widgets.
The single child layout widget is a type of widget, which can have only one child widget inside
the parent layout widget. These widgets can also contain special layout functionality.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// It is the root widget of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Multiple Layout Widget',
debugShowCheckedModeBanner: false,
theme: ThemeData(
// This is the theme of your application.
primarySwatch: Colors.green,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("FittedBox Widget")),
body: Center(
child: FittedBox(child: Row(
children: <Widget>[
Container(
child: Image.asset('assets/computer.png'),
),
Container(
child: Text("This is a widget"),
)
],
),
fit: BoxFit.contain,
)
),
);
}
}
OUTPUT:
The multiple child widgets are a type of widget, which contains more than one child widget, and
the layout of these widgets are unique
Code:
import 'package:flutter/material.dart';
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
@override
return Center(
child: Container(
alignment: Alignment.center,
color: Colors.white,
child: Row(
children: <Widget>[
Expanded(
Expanded(
),
Expanded(
child: FittedBox(
),
),
],
),
),
);
}
}
OUTPUT: