5. Stack, Padding, Center, Icon, TextField, CheckBox, Switch, Image Widgets
5. Stack, Padding, Center, Icon, TextField, CheckBox, Switch, Image Widgets
Stack(
alignment: Alignment.center,
children: [
Positioned(
bottom: 10,
right: 10,
), ], ),
1-3 Padding and Center Widget
Padding widget: This widget adds
empty space around its child widget. It's
a great way to create gaps between
widgets or from the screen edge.
void main() {
runApp(MyApp());
}
Icon(
Icons.favorite, // The icon itself, here it's a heart symbol
color: Colors.red, // The color of the icon
size: 30.0, // The size of the icon
)
1-6 TextField widget
The TextField widget in Flutter is used for user input. It
allows users to enter text, and you can customize its
behavior, style, and validation. Below is an example
demonstrating how to use the TextField widget in a
Flutter application.
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Name’,
hintText: 'e.g. John Doe’,
prefixIcon: Icon(Icons.person), // Optional: adds an icon
),
onChanged: (text) {
// Handle text changes print('Name entered: $text’);
},
)
1-7 CheckBox & Switch Example
1. Checkbox Widget Example:
bool isChecked = false;
Checkbox(
value: isChecked, // Whether the checkbox is
checked or not
onChanged: (bool? newValue) {
setState(() {
isChecked = newValue!;
});
},)
2. Switch Widget Example:
bool isSwitched = false;
Switch(
value: isSwitched, // Whether the switch is on or
off
onChanged: (bool newValue) {
setState(() {
isSwitched = newValue;
});},)
1-8 Image widget
1. Displaying an Asset Image
To use an image stored in your app's assets, add the image to your project
and reference it in pubspec.yaml.
Step 1: Add the image to your assets folder
Create an assets folder in your project directory.
Place your image file (e.g., my_image.png) inside the assets folder.
Step 2: Update pubspec.yaml
assets: - lib/assets/my_image.png
Image.asset(
‘lib/assets/my_image.png',
width: 200,
height: 200,
fit: BoxFit.cover,
)
1-8 Image widget
2. Displaying a Network Image
For images from a URL, use Image.network.
Image.network(
'https://example.com/my_image.png',
width: 200, height: 200,
fit: BoxFit.cover,
loadingBuilder: (BuildContext context, Widget child, ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: CircularProgressIndicator(),
);
},
errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
return Text('Failed to load image');
},
)
Thanks
Do you have any
questions?
!
Resources
https://flutter.dev