Lab 3 - Drawer Form
Lab 3 - Drawer Form
import 'package:flutter/material.dart';
import 'package:formapp/formtask.dart';
import 'package:formapp/formvalid.dart';
import 'package:formapp/regform.dart';
import 'package:formapp/ratecourse.dart';
Page 1 of 15
[Document title]
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
@override
State<MyHomePage> createState() => _MyHomePageState();
}
Page 2 of 15
[Document title]
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: _widgetOptions[_selectedIndex],
),
drawer: Drawer(
// Add a ListView to the drawer. This ensures the user can scroll
// through the options in the drawer if there isn't enough vertical
// space to fit everything.
child: ListView(
// Important: Remove any padding from the ListView.
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text('Drawer Header'),
),
ListTile(
title: const Text('Home'),
selected: _selectedIndex == 0,
onTap: () {
// Update the state of the app
_onItemTapped(0);
// Then close the drawer
Navigator.pop(context);
},
),
ListTile(
title: const Text('My Registration'),
selected: _selectedIndex == 1,
onTap: () {
// Update the state of the app
_onItemTapped(1);
// Then close the drawer
// Navigator.pop(context);
Navigator.push(context,
MaterialPageRoute(builder: (context) => const RegForm(title: 'Registration Form',)));
},
),
ListTile(
title: const Text('My To Do Tasks'),
selected: _selectedIndex == 2,
onTap: () {
// Update the state of the app
_onItemTapped(2);
// Then close the drawer
// Navigator.pop(context);
Navigator.push(context,
MaterialPageRoute(builder: (context) => const FormTasks()));
Page 3 of 15
[Document title]
},
),
ListTile(
title: const Text('My Rating'),
selected: _selectedIndex == 3,
onTap: () {
// Update the state of the app
_onItemTapped(3);
// Then close the drawer
// Navigator.pop(context);
Navigator.push(context,
MaterialPageRoute(builder: (context) => const RateCourse()));
},
),
ListTile(
title: const Text('Validation Form'),
selected: _selectedIndex == 4,
onTap: () {
// Update the state of the app
_onItemTapped(3);
// Then close the drawer
// Navigator.pop(context);
Navigator.push(context,
MaterialPageRoute(builder: (context) => const FormValid()));
},
),
],
),
),
);
}
}
Page 4 of 15
[Document title]
@override
State<FormTasks> createState() => _FormTasksState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Daily Work Tasks"),
actions: [
InkWell(
child: const Padding(
padding: EdgeInsets.all(8.0),
child: Icon(Icons.tab_rounded),
),
onTap: () => itinerariesDialog(context),
),
],
),
body: Form(
key: _formKey,
child: ListView(
shrinkWrap: true,
children: [
fieldCount == 0
? const Padding(
padding: EdgeInsets.all(15.0),
child: Align(
alignment: Alignment.center,
child: Text(
"No Tasks!",
style: TextStyle(
fontSize: 33, fontWeight: FontWeight.bold),
),
),
)
: Column(
children: [
ListView.builder(
itemCount: list.length,
shrinkWrap: true,
Page 5 of 15
[Document title]
itinerariesDialog(BuildContext context) {
showDialog(
barrierDismissible: true,
context: context,
builder: (context) {
return AlertDialog(
title: const Text("Work Tasks"),
content: SizedBox(
width: double.maxFinite,
child: ListView(
shrinkWrap: true,
children: items.map((e) => Text(e["TodoList"].trim())).toList(),
),
),
);
});
}
Widget buildField(int i) {
return ListTile(
leading: CircleAvatar(
child: Text((i + 1).toString()),
),
title: TextFormField(
decoration: InputDecoration(
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
Page 6 of 15
[Document title]
items.removeAt(i);
});
},
),
);
}
Page 7 of 15
[Document title]
import 'package:flutter/material.dart';
import 'package:formapp/nextpage.dart';
Page 8 of 15
[Document title]
// Button to go to nextpage.
ElevatedButton(
onPressed: () {
// Navigator to the next page.
Navigator.of(context).push(
MaterialPageRoute(
5. Create nextpage.dart
import 'package:flutter/material.dart';
@override
State<NextPage> createState() => _NextPageState();
}
return Scaffold(
appBar: AppBar(
Page 9 of 15
[Document title]
Page 10 of 15
[Document title]
6. Create ratecourse.dart
import 'package:flutter/material.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
@override
State<RateCourse> createState() => _RateCourseState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Rate Course'),
),
body: Padding(
padding: const EdgeInsets.all(25),
child: Center(
child: Column(
children: [
const Text(
'Rate Course?',
style: TextStyle(fontSize: 24),
),
const SizedBox(height: 25),
// implement the rating bar
RatingBar(
initialRating: 0,
direction: Axis.horizontal,
allowHalfRating: true,
itemCount: 5,
ratingWidget: RatingWidget(
full: const Icon(Icons.star, color: Colors.orange),
half: const Icon(
Page 11 of 15
[Document title]
Icons.star_half,
color: Colors.orange,
),
empty: const Icon(
Icons.star_outline,
color: Colors.orange,
)),
onRatingUpdate: (value) {
setState(() {
_ratingValue = value;
});
}),
const SizedBox(height: 25),
// Display the rate in number
Container(
width: 200,
height: 200,
decoration: const BoxDecoration(
color: Colors.red, shape: BoxShape.circle),
alignment: Alignment.center,
child: Text(
_ratingValue != null ? _ratingValue.toString() : 'Rate it!',
style: const TextStyle(color: Colors.white, fontSize: 30),
),
)
],
),
),
));
}
}
7. Create formvalid.dart
import 'package:flutter/material.dart';
@override
Page 12 of 15
[Document title]
void _validateInputs() {
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
} else {
setState(() {
_autoValidate = AutovalidateMode.always;
});
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Attendance Form'),
),
body: SingleChildScrollView(
child: Container(
margin: const EdgeInsets.all(15.0),
child: Form(
key: _formKey,
autovalidateMode: _autoValidate,
child: formUI(),
),
),
),
),
);
}
Widget formUI() {
return Column(
children: <Widget>[
TextFormField(
decoration: const InputDecoration(labelText: 'Name'),
keyboardType: TextInputType.text,
validator: validateName,
onSaved: (String? val) {
name.text = val!;
},
),
TextFormField(
decoration: const InputDecoration(labelText: 'Mobile'),
Page 13 of 15
[Document title]
keyboardType: TextInputType.phone,
validator: validateMobile,
onSaved: (String? val) {
mobile.text = val!;
},
),
TextFormField(
decoration: const InputDecoration(labelText: 'Email'),
keyboardType: TextInputType.emailAddress,
validator: validateEmail,
onSaved: (String? val) {
email.text = val!;
},
),
const SizedBox(
height: 10.0,
),
OutlinedButton(
onPressed: _validateInputs,
child: const Text('Submit'),
)
],
);
}
Page 14 of 15
[Document title]
Then before you run the codes, you are required to run this command to get all the Flutter
dependencies.
TASKS:
Modify the formvalid.dart so the information key in the form will display in the next page when the
form does not show any errors.
Page 15 of 15