Week3-6
Week3-6
import 'package:flutter/material.dart';
Step 1: First, we need to create a new folder inside the root of the Flutter project and named it assets.
We can also give it any other name if you want.
Step 3: Update the pubspec.yaml file. Suppose the image name is tablet.png, then pubspec.yaml file
is:
assets:
- assets/tablet.png
- assets/background.png
If the assets folder contains more than one image, we can include it by specifying the directory name
with the slash (/) character at the end.
flutter:
assets:
- assets/
Step 4: Finally, open themain.dart file and insert the following code.
import 'package:flutter/material.dart';
Step 5: Now, run the app. You will get something like the screen below.
Flutter Container Example
A basic container has a margin, border, and padding properties surrounding its child widget, as shown
in the below image:
import 'package:flutter/material.dart';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Flutter Container Example"),
),
body: Container(
padding: EdgeInsets.all(35),
margin: EdgeInsets.all(20),
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 4),
borderRadius: BorderRadius.circular(8),
boxShadow: [
new BoxShadow(color: Colors.green, offset: new Offset(6.0, 6.0),),
],
),
child: Text("Hello! I am in the container widget decoration box!!",
style: TextStyle(fontSize: 30)),
),
),
);
}
}
Week-4
Implement different layout structures using Row, Column, and Stack widgets.
Flutter row
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.
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:
import 'package:flutter/material.dart';
Output:
When we run this app, we should get the UI as the below screenshot.
Flutter 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.
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:
import 'package:flutter/material.dart';
Output:
When we run this app, we should get the UI as the below screenshot.
Flutter Stack widgets
The stack is a widget in Flutter that contains a list of widgets and positions them on top of the other.
In other words, the stack allows developers to overlap multiple widgets into a single screen and
renders them from bottom to top. Hence, the first widget is the bottommost item, and the last
widget is the topmost item
Stack(
children: <Widget>[
// Max Size
Container(
color: Colors.green,
),
Container(
color: Colors.blue,
),
Container(
color: Colors.yellow,
)
],
),
The below code explains how to use the stack widget in Flutter. In this code, we are going to try most
of the essential attributes of the stack widget.
import 'package:flutter/material.dart';
Output:
When we run the app, we should get the UI of the screen similar to the below screenshot:
Week-5
This example uses a LayoutBuilder to build a different widget depending on the available width.
Resize the DartPad window to see LayoutBuilder in action!
import 'package:flutter/material.dart';
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: LayoutBuilderExample(),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('LayoutBuilder Example')),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
if (constraints.maxWidth > 600) {
return _buildWideContainers();
} else {
return _buildNormalContainer();
}
},
),
);
}
Widget _buildNormalContainer() {
return Center(
child: Container(
height: 100.0,
width: 100.0,
color: Colors.red,
),
);
}
Widget _buildWideContainers() {
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
height: 100.0,
width: 100.0,
color: Colors.red,
),
Container(
height: 100.0,
width: 100.0,
color: Colors.yellow,
),
],
),
);
}
}
Week-6
You can use MediaQuery to retrieve the size (width/height) and orientation (portrait/ landscape) of the
screen.
return Scaffold(
body: Container(
color: CustomColors.android,
child: Center(
child: Text(
'View\n\n' +
'[MediaQuery width]: ${screenSize.width.toStringAsFixed(2)}\n\n' +
'[MediaQuery orientation]: $orientation',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
);
}
}