Topic 5 - Flutter Screeen Components
Topic 5 - Flutter Screeen Components
TOPIC 5
Flutter Screen Components
Topic 5: Outline
2
Purpose:
Organizes the main structure of a page,
including key UI elements like the navigation,
action buttons, and body content.
Typical components include:
AppBar: The top bar with actions.
Body: The main screen content.
Drawer: A slide-out menu from the left.
BottomNavigationBar: For navigation
between major sections.
FloatingActionButton: A prominent action
button.
Scaffold .. basic example
5
Scaffold(
appBar: AppBar(
title: Text('My App'),
),
body: Center(
child: Text('Hello, world!'),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
);
Scaffold .. Drawer
6
Drawer:
A side navigation menu that slides in from the left.
Useful for apps with multiple navigation sections or options.
Basic example
Scaffold(
drawer: Drawer(
child: ListView(
children: [
DrawerHeader(child: Text('Header')),
ListTile(
title: Text('Item 1'),
onTap: () {
// Handle navigation
}, ), ], ), ), );
Scaffold .. Bottom Navigation
Bar
7
BottomNavigationBar:
Used for switching between major app sections at the bottom of
the screen.
Basic example
Scaffold(
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home', ),
BottomNavigationBarItem(
icon: Icon(Icons.settings),
label: 'Settings', ), ],
onTap: (int index) {
// Handle navigation
}, ), );
Scaffold .. Floating Action
8
FloatingActionButton:
A circular button often used for the primary
action on a page (e.g., adding a new item).
Example
FloatingActionButton(
onPressed: () {
// Perform action
},
child: Icon(Icons.add),
);
Scaffold .. Snackbar
9
Snackbar:
A lightweight, temporary notification displayed at
the bottom of the screen.
Example
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('This is a snackbar'),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
// Handle action
}, ), ), );
Scaffold Example with All
Components
10
Scaffold(
appBar: AppBar(
title: Text('Scaffold Example'), ),
drawer: Drawer(
child: ListView(
children: [DrawerHeader(child: Text('Header')), ListTile(title:
Text('Item 1'))], ), ),
body: Center(child: Text('Main content')),
floatingActionButton: FloatingActionButton(onPressed: () {}, child:
Icon(Icons.add)),
bottomNavigationBar: BottomNavigationBar(
items: [
BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
BottomNavigationBarItem(icon: Icon(Icons.settings), label:
'Settings'),
],
), );
Layouts in Flutter
11
Flutter does not have a RelativeLayout widget, but you can achieve
relative positioning using Stack and Align or Positioned widgets.
Flutter uses Align and Stack:
Aligns widgets relative to the parent or other widgets.
Use Align when you want relative positioning without specifying exact
coordinates. You can position widgets at different points like topLeft, center,
bottomRight, etc.
Stack:
Allows widgets to be placed on top of each other, and you can position child
widgets relatively using Positioned or Align.
Example Stack(
children: [
Align( Container(width: 300, height: 300,
alignment: color: Colors.blue),
Alignment.topRight, Positioned( top: 20, left: 50,
child: Text('Top Right'), child: Text('Top-Left'),
); ), ], );
Common Layout Types .. Frame
Layout
16
User Input
Form Widgets: Capture user input like
text, numbers, or selections.
Common Widgets:
TextField: For text input.
Checkbox, Switch, RadioButton: For
Boolean inputs.
TextField(
decoration: InputDecoration(hintText:
'Enter your name'),
);
27
Form Validation:
Use Form and GlobalKey for input
validation.
Example
final _formKey = GlobalKey<FormState>();
Form(
key: _formKey,
child: TextFormField(validator: (value) =>
value.isEmpty ? 'Required' : null),
);
Creating the User Interface
Programmatically
28
GestureDetector:
Capture gestures like swipes or taps.
Example
GestureDetector(
onTap: () { print('Tapped'); },
child: Container(color: Colors.blue, height:
50, width: 50),
);
Listening for UI Notifications …
31
StreamBuilder:
Respond to asynchronous data changes.
Example
StreamBuilder(
stream: myStream,
builder: (context, snapshot) {
return Text(snapshot.hasData ?
snapshot.data : 'Loading...');
},
);
32