Flutter code for lab practice
Flutter code for lab practice
import
'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends
StatelessWidget {
@override
Widget build(BuildContext
context) {
return MaterialApp(
debugShowCheckedModeBanner:
false,
home:
NetworkImageScreen(),
);
}
}
@override
Widget build(BuildContext
context) {
return Scaffold(
appBar: AppBar(title:
Text('displaying Network Image
Example')),
body: Center(
child: Image.network(
imageUrl,
loadingBuilder:
(context, child,
loadingProgress) {
if (loadingProgress
== null) return child;
return Center(
child:
CircularProgressIndicator(
value:
loadingProgress.expectedTotalByt
es != null ?
loadingProgress.cumulativeBytesL
oaded
/(loadingProgress.expectedTotalB
ytes ?? 1)
: null,
),
);
},
errorBuilder:
(context, error, stackTrace) {
return Text('Failed to load
image',
style: TextStyle(color:
Colors.red));
},
fit: BoxFit.cover,
width: 300,
height: 300,
),
),
);
}
}
Output…
To display a video from assets in a Flutter app,
you can use the video_player package. Here’s
how you can do it step by step.
Steps to Display Video from Assets:
Add the video_player package to your
pubspec.yaml:
dependencies:
flutter:
sdk: flutter
video_player: ^2.4.5 # Check for the latest
version
Run flutter pub get in your terminal to install
the package.
Output….
1. Flutter Text field
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
home: const MyApp(),
));
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
_State createState() => _State();
}
Output….
2. Validation of text form field
import 'package:flutter/material.dart';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Form Validation'),
),
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 = val;
},
),
TextFormField(
decoration: const InputDecoration(labelText:
'Mobile'),
keyboardType: TextInputType.phone,
validator: validateMobile,
onSaved: (String? val) {
_mobile = val;
},
),
TextFormField(
decoration: const InputDecoration(labelText:
'Email'),
keyboardType: TextInputType.emailAddress,
validator: validateEmail,
onSaved: (String? val) {
_email = val;
},
),
const SizedBox(
height: 10.0,
),
OutlinedButton(
onPressed: _validateInputs,
child: const Text('Validate'),
)
],
);
}
@override
State<RunMyApp> createState()
=> _RunMyAppState();
}
class _RunMyAppState extends
State<RunMyApp> {
@override
Widget build(BuildContext
context) {
return MaterialApp(
debugShowCheckedModeBanner:
false,
theme:
ThemeData(primarySwatch:
Colors.green),
home: Scaffold(
appBar: AppBar(
title: Text('Gradient
Button'),
),
body: Center(
child: Container(
height: 44.0,
decoration:
BoxDecoration(
gradient:
LinearGradient(
colors:
[Color.fromARGB(255, 2, 173,
102), Colors.blue])),
child:
ElevatedButton(
onPressed: () {},
style:
ElevatedButton.styleFrom(
backgroundColor:
Colors.transparent,
shadowColor:
Colors.transparent),
child:
Text('Elevated Button'),
),
),
),
),
);
}
}
Output…
12. Circular button
import
'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
class RunMyApp extends
StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState()
=> _RunMyAppState();
}
class _RunMyAppState extends
State<RunMyApp> {
@override
Widget build(BuildContext
context) {
return MaterialApp(
debugShowCheckedModeBanner:
false,
theme:
ThemeData(primarySwatch:
Colors.green),
home: Scaffold(
appBar: AppBar(
title: Text('Gradient
Button'),
),
body: Center(
child: Container(
height: 120.0, //
height of the button
decoration:
BoxDecoration(
shape:
BoxShape.circle, // shape makes
the circular button
gradient:
LinearGradient(// gives the
Gradient color
colors:
[Color.fromARGB(255, 2, 173,
102), Colors.blue])),
child:
ElevatedButton(
onPressed: () {},
style:
ElevatedButton.styleFrom(
backgroundColor:
Colors.transparent,
shadowColor:
Colors.transparent),
child:
Text('Elevated Button'),
),
),
),
),
);
}
}
Output…
13. Demonstration of row and column
import
'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context)
{
return MaterialApp(
home: Home(),
);
}
}
class Home extends StatelessWidget {
Home({super.key});
@override
Widget build(BuildContext context)
{
return Scaffold(
appBar: AppBar(
title:
const Text('Demonstration
for Column and Row Button'), // Fixed
typo
backgroundColor:
Colors.green,
),
body: Column(
children: [
const SizedBox(height: 15),
const SizedBox(
height: 20,
child:
Text('Demonstration of Row'),
),
Row(
children: [
Card(
margin: const
EdgeInsets.all(10),
elevation: 8,
child: Container(
padding: const
EdgeInsets.all(25),
child: const Text(
'IT third year',
style:
TextStyle(color: Colors.green),
),
),
),
const SizedBox(width:
2),
Card(
margin: const
EdgeInsets.all(10),
elevation: 8,
child: Container(
padding: const
EdgeInsets.all(25),
child: const Text(
'IT third year',
style:
TextStyle(color: Colors.green),
),
),
),
],
),
const SizedBox(height: 30),
const SizedBox(
height: 20,
child:
Text('Demonstration of Column'),
),
Column(
children: [
Card(
margin: const
EdgeInsets.all(10),
elevation: 8,
child: Container(
padding: const
EdgeInsets.all(25),
child: const Text(
'IT third year',
style:
TextStyle(color: Colors.green),
),
),
),
const SizedBox(height:
4), // Changed from width to height
Card(
margin: const
EdgeInsets.all(10),
elevation: 8,
child: Container(
padding: const
EdgeInsets.all(25),
child: const Text(
'IT third year',
style:
TextStyle(color: Colors.green),
),
),
),
],
)
],
),
);
}
}
Output…
14. Flutter table example
Corrections & Improvements:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
@override
return MaterialApp(
home: Scaffold(
appBar: AppBar(
),
body: Center(
Container(
margin: EdgeInsets.all(20),
child: Table(
defaultColumnWidth: FixedColumnWidth(120.0),
border: TableBorder.all(
TableRow(children: [
Column(children: [
]),
Column(children: [
]),
Column(children: [
Text('information technology',
]),
]),
TableRow(children: [
Column(children: [Text('Javatpoint')]),
Column(children: [Text('Flutter')]),
Column(children: [Text('5*')]),
]),
TableRow(children: [
Column(children: [Text('firebase')]),
Column(children: [Text('MySQL')]),
Column(children: [Text('5*')]),
]),
TableRow(children: [
Column(children: [Text('SQLlite')]),
Column(children: [Text('ReactJS')]),
Column(children: [Text('5*')]),
]),
],
),
),
]))),
);
Output……
void main() {
runApp(const MyApp());
}
// MyApp as a StatefulWidget
class MyApp extends
StatefulWidget {
const MyApp({super.key});
@override
_MyAppState createState() =>
_MyAppState();
}
debugShowCheckedModeBanner:
false, // Removes the debug
banner
home: Scaffold(
appBar: AppBar(
title: const
Text('Flutter DataTable
Example'),
),
body: ListView(
children: <Widget>[
const Center(
child: Padding(
padding:
EdgeInsets.all(16.0),
child: Text(
'People-
Chart',
style:
TextStyle(fontSize: 25,
fontWeight: FontWeight.bold),
),
),
),
DataTable(
columnSpacing:
20.0,
headingRowColor:
MaterialStateColor.resolveWith(
(states) =>
Colors.grey.shade300),
border:
TableBorder.all(width: 1, color:
Colors.black54),
columns: const [
DataColumn(
label: Text(
'ID',
style:
TextStyle(fontSize: 18,
fontWeight: FontWeight.bold),
),
),
DataColumn(
label: Text(
'Name',
style:
TextStyle(fontSize: 18,
fontWeight: FontWeight.bold),
),
),
DataColumn(
label: Text(
'Profession',
style:
TextStyle(fontSize: 18,
fontWeight: FontWeight.bold),
),
),
],
rows: const [
DataRow(cells: [
DataCell(Text('1')),
DataCell(Text('Stephen')),
DataCell(Text('Actor')),
]),
DataRow(cells: [
DataCell(Text('5')),
DataCell(Text('John')),
DataCell(Text('Student')),
]),
DataRow(cells: [
DataCell(Text('10')),
DataCell(Text('Harry')),
DataCell(Text('Leader')),
]),
DataRow(cells: [
DataCell(Text('15')),
DataCell(Text('Peter')),
DataCell(Text('Scientist')),
]),
],
),
],
),
),
);
}
}
Output…..
16. Flutter table 2
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[100], //
Background color of scaffold
appBar: AppBar(
title: const Text("Flutter Table"),
backgroundColor: Colors.redAccent,
),
body: Container(
padding: const EdgeInsets.all(15),
child: Table(
border:
TableBorder.all(width: 1, color:
Colors.black45), // Table border
children: const [
TableRow(
decoration: BoxDecoration(color:
Colors.grey),
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Text("S/N",
style: TextStyle(fontWeight:
FontWeight.bold)),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text("Name",
style: TextStyle(fontWeight:
FontWeight.bold)),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text("Address",
style: TextStyle(fontWeight:
FontWeight.bold)),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text("Nation",
style: TextStyle(fontWeight:
FontWeight.bold)),
),
],
),
TableRow(
children: [
Padding(padding: EdgeInsets.all(8.0),
child: Text("1.")),
Padding(
padding: EdgeInsets.all(8.0), child:
Text("Krishna Karki")),
Padding(
padding: EdgeInsets.all(8.0),
child: Text("Nepal, Kathmandu")),
Padding(padding: EdgeInsets.all(8.0),
child: Text("Nepal")),
],
),
TableRow(
children: [
Padding(padding: EdgeInsets.all(8.0),
child: Text("2.")),
Padding(padding: EdgeInsets.all(8.0),
child: Text("John Wick")),
Padding(
padding: EdgeInsets.all(8.0), child:
Text("New York, USA")),
Padding(padding: EdgeInsets.all(8.0),
child: Text("USA")),
],
),
TableRow(
children: [
Padding(padding: EdgeInsets.all(8.0),
child: Text("3.")),
Padding(
padding: EdgeInsets.all(8.0), child:
Text("Fredrick May")),
Padding(
padding: EdgeInsets.all(8.0),
child: Text("Berlin, Germany")),
Padding(padding: EdgeInsets.all(8.0),
child: Text("Germany")),
],
),
],
),
),
);
}
}
Output…
17. Flutter table
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.blue[100], // Background
color of scaffold
appBar: AppBar(
title: const Text("Flutter Table"),
backgroundColor: Colors.redAccent,
),
body: Container(
padding: const EdgeInsets.all(15),
child: Table(
border: TableBorder.all(width: 1, color:
Colors.black45), // Table border
children: const [
TableRow(
decoration: BoxDecoration(color:
Colors.grey),
children: [
Padding(
padding: EdgeInsets.all(8.0),
child: Text("S/N", style:
TextStyle(fontWeight: FontWeight.bold)),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text("Name", style:
TextStyle(fontWeight: FontWeight.bold)),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text("Address", style:
TextStyle(fontWeight: FontWeight.bold)),
),
Padding(
padding: EdgeInsets.all(8.0),
child: Text("Nation", style:
TextStyle(fontWeight: FontWeight.bold)),
),
],
),
TableRow(
children: [
Padding(padding: EdgeInsets.all(8.0), child:
Text("1.")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("Krishna Karki")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("Nepal, Kathmandu")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("Nepal")),
],
),
TableRow(
children: [
Padding(padding: EdgeInsets.all(8.0), child:
Text("2.")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("John Wick")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("New York, USA")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("USA")),
],
),
TableRow(
children: [
Padding(padding: EdgeInsets.all(8.0), child:
Text("3.")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("Fredrick May")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("Berlin, Germany")),
Padding(padding: EdgeInsets.all(8.0), child:
Text("Germany")),
],
),
],
),
),
);
}
}
Output….
Summary of Fixes & Enhancements
1. Proper Widget Naming: _DataTableExample →
_MyAppState for better readability.
2. Added const for Performance: Reduces widget
rebuilds, making the app smoother.
3. Better UI with Padding & Colors: Tables now have
improved spacing and border styling.
4. Consistent TextStyle Usage: Ensured all text inside
tables follows a uniform font size and weight.
5. Fixed Duplicate Flutter Example Issue: Now two
examples are structured properly.
Now your Flutter tables are well-optimized and
formatted correctly!