Lab Task 12: Submitted By: Qurat Ul Ain Registration No: Sp22-Bcs-007 Q1
Lab Task 12: Submitted By: Qurat Ul Ain Registration No: Sp22-Bcs-007 Q1
Q 1:
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
void generateRandomNumber() {
if (isGameOver) return;
setState(() {
if (randomNumber >= 1 && randomNumber <= 3) {
boxA++;
} else if (randomNumber >= 4 && randomNumber <= 6) {
boxB++;
} else if (randomNumber >= 7 && randomNumber <= 9) {
boxC++;
}
void _resetGame() {
setState(() {
boxA = 0;
boxB = 0;
boxC = 0;
isGameOver = false;
});
}
void _showGameOverDialog() {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Game Over'),
content: Text('One of the boxes has reached the limit!'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
_resetGame();
},
child: Text('Restart'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Row for the boxes A, B, and C
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
_buildBox('A', boxA, 'Range\n(1 to 3)'),
_buildBox('B', boxB, 'Range\n(4 to 6)'),
_buildBox('C', boxC, 'Range\n(7 to 9)'),
],
),
SizedBox(height: 50),
// Button
ElevatedButton(
onPressed: generateRandomNumber,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
side: BorderSide(color: Colors.black, width: 1.5)),
),
child: Text(
isGameOver ? 'Game Over' : 'Click Here',
style: TextStyle(fontSize: 18),
),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
padding: EdgeInsets.all(16),
width: 600,
height: 500, // Increased height for better spacing
decoration: BoxDecoration(
border: Border.all(color: Colors.black, width: 1.5),
),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text("Player 1",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold)),
Text("Player 2",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold)),
Text("Player 3",
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.bold)),
],
),
SizedBox(height: 40),
// Added spacing between rows
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
// Player 1 Column
Expanded(
child: Column(
children: [
Text("Current Score: $player1Score",
style: TextStyle(fontSize: 16)),
Text("Outcome: $player1Outcome",
style: TextStyle(fontSize: 16)),
Text("Target: 10", style: TextStyle(fontSize: 16)),
Text("Turn Number: $player1Turn",
style: TextStyle(fontSize: 16)),
SizedBox(height: 10), // Added space before button
ElevatedButton(
onPressed: () => rollDice(1),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey,
foregroundColor: Colors.black,
padding: EdgeInsets.symmetric(
horizontal: 50, vertical: 20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
side: BorderSide(
color: Colors.grey, width: 1.5),
),
),
child: Text('Player 1',
style: TextStyle(fontSize: 16)),
),
],
),
),
SizedBox(width: 20), // Increased space between columns
// Player 2 Column
Expanded(
child: Column(
children: [
Text("Current Score: $player2Score",
style: TextStyle(fontSize: 16)),
Text("Outcome: $player2Outcome",
style: TextStyle(fontSize: 16)),
Text("Target: 10", style: TextStyle(fontSize: 16)),
Text("Turn Number: $player2Turn",
style: TextStyle(fontSize: 16)),
SizedBox(height: 10), // Added space before button
ElevatedButton(
onPressed: () => rollDice(2),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey,
foregroundColor: Colors.black,
padding: EdgeInsets.symmetric(
horizontal: 50, vertical: 20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
side: BorderSide(
color: Colors.grey, width: 1.5),
),
),
child: Text('Player 2',
style: TextStyle(fontSize: 16)),
),
],
),
),
SizedBox(width: 20), // Increased space between columns
// Player 3 Column
Expanded(
child: Column(
children: [
Text("Current Score: $player3Score",
style: TextStyle(fontSize: 16)),
Text("Outcome: $player3Outcome",
style: TextStyle(fontSize: 16)),
Text("Target: 10", style: TextStyle(fontSize: 16)),
Text("Turn Number: $player3Turn",
style: TextStyle(fontSize: 16)),
SizedBox(height: 10), // Added space before button
ElevatedButton(
onPressed: () => rollDice(3),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey,
foregroundColor: Colors.black,
padding: EdgeInsets.symmetric(
horizontal: 50, vertical: 20),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5),
side: BorderSide(
color: Colors.grey, width: 1.5),
),
),
child: Text('Player 3',
style: TextStyle(fontSize: 16)),
),
],
),
),
],
),
SizedBox(height: 20), // Increased space after player rows
Text("Click a button to generate a random number from 1 to 6",
style: TextStyle(fontSize: 16)),
],
),
),
],
),
),
);
}
}
Q #3
import 'dart:math';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
void generateComputerValue() {
String computerChoice = generateRandomValue();
String winner = determineWinner(userValue, computerChoice);
setState(() {
computerValue = computerChoice;
result = winner;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
// User value box (with default value)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'User Value ',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
Container(
padding: EdgeInsets.all(8),
width: 180,
height: 40, // Added height
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
child: Text(
userValue,
style:
TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
),
],
),
SizedBox(height: 20),
child: Text(
computerValue,
style:
TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
),
],
),
SizedBox(height: 20),
child: Text(
result,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: result == 'Draw'
? Colors.grey
: (result == 'User' ? Colors.green : Colors.black),
),
),
),
],
),
SizedBox(height: 30),
Q #4
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: colors.map((color) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 4.0),
child: GestureDetector(
onTap: () {
setState(() {
selectedColor = color;
});
},
child: Container(
decoration: BoxDecoration(
color: color == "RED"
? Colors.red
: color == "GREEN"
? Colors.green
: const Color.fromARGB(255, 33, 96, 204),
borderRadius: BorderRadius.circular(5),
border: Border.all(
color: selectedColor == color
? Colors.black
: Colors.transparent,
width: 2,
),
),
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 20),
child: Text(
color,
style: TextStyle(
color: Colors.black,
fontWeight: selectedColor == color
? FontWeight.bold
: FontWeight.normal,
fontSize: selectedColor == color ? 18 : 14,
),
),
),
),
);
}).toList(),
),
SizedBox(height: 40),
Container(
decoration: BoxDecoration(
color: selectedColor == "RED"
? Colors.red
: selectedColor == "GREEN"
? Colors.green
: const Color.fromARGB(255, 33, 96, 204),
borderRadius: BorderRadius.circular(5),
),
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20),
child: Text(
"$selectedColor is clicked",
style: TextStyle(
color: Colors.black,
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
],
),
);
}
}
Q #6
import 'package:flutter/material.dart';
import 'dart:math';
void main() {
runApp(SnakesAndLaddersApp());
}
void rollDice() {
setState(() {
diceRoll = Random().nextInt(6) + 1; // Random dice roll (1-6)
int newPosition = playerPosition + diceRoll;
void resetGame() {
setState(() {
playerPosition = 1; // Reset player to the start position
diceRoll = 1; // Reset dice roll
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: LayoutBuilder(
builder: (context, constraints) {
// Ensure the board is square and fits within the screen
final double boardSize = min(constraints.maxWidth, constraints.maxHeight);
return Center(
child: Stack(
children: [
// Background board image
Container(
width: boardSize,
height: boardSize,
child: Image.asset(
'SnakesLaddersBoard.jpg',
fit: BoxFit.fill, // Make sure the board fills the container
),
),
// Player token
Positioned(
left: calculatePosition(playerPosition, boardSize).dx,
top: calculatePosition(playerPosition, boardSize).dy,
child: Image.asset(
'player.png',
width: boardSize / 10, // Scale player token relative to board size
height: boardSize / 10,
),
),
// Dice and Roll Button
Align(
alignment: Alignment.bottomCenter,
child: Container(
color: Colors.white.withOpacity(0.8),
padding: EdgeInsets.all(20),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'You rolled a $diceRoll',
style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
SizedBox(height: 10),
ElevatedButton(
onPressed: rollDice,
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 20, vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
child: Text(
'Throw the Dice',
style: TextStyle(fontSize: 20),
),
),
],
),
),
),
],
),
);
},
),
);
}
}
Q #8
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const MobileForm(),
debugShowCheckedModeBanner: false,
);
}
}
@override
_MobileFormState createState() => _MobileFormState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Center(
child: Container(
width: 350,
padding: const EdgeInsets.all(15.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.blue),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Email Section
const Text("Email"),
const SizedBox(height: 5),
TextField(
decoration: InputDecoration(
hintText: "30 chars max",
counterText: "", // Removes the length counter
border: OutlineInputBorder(),
),
maxLength: 30,
style: const TextStyle(fontSize: 14),
),
const SizedBox(height: 10),
// Name Section
const Text("Name"),
const SizedBox(height: 5),
TextField(
decoration: InputDecoration(
hintText: "50 chars max",
counterText: "",
border: OutlineInputBorder(),
),
maxLength: 50,
style: const TextStyle(fontSize: 14),
),
const SizedBox(height: 10),
// Gender Section
const Text("Gender"),
Row(
children: [
Radio(
value: "Male",
groupValue: _gender,
onChanged: (value) {
setState(() {
_gender = value.toString();
});
},
),
const Text("Male"),
const SizedBox(width: 20),
Radio(
value: "Female",
groupValue: _gender,
onChanged: (value) {
setState(() {
_gender = value.toString();
});
},
),
const Text("Female"),
],
),
const SizedBox(height: 10),
// Subjects Section
const Text("Subjects"),
Row(
children: [
Checkbox(
value: _isPhy,
onChanged: (value) {
setState(() {
_isPhy = value!;
});
},
),
const Text("Phy"),
Checkbox(
value: _isChem,
onChanged: (value) {
setState(() {
_isChem = value!;
});
},
),
const Text("Chem"),
Checkbox(
value: _isBio,
onChanged: (value) {
setState(() {
_isBio = value!;
});
},
),
const Text("Bio"),
],
),
const SizedBox(height: 10),
// Skills Section
const Text("Skills"),
Container(
height: 80,
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
),
child: const Text("C++\nJava\nJavascript\nC#"),
),
const SizedBox(height: 10),
// Address Section
const Text("Address"),
const SizedBox(height: 5),
SizedBox(
height: 40,
child: TextField(
decoration: InputDecoration(
border: OutlineInputBorder(),
),
maxLines: 1,
),
),
const SizedBox(height: 20),
// Submit Button
Center(
child: ElevatedButton(
onPressed: () {},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey,
minimumSize: const Size(100, 40),
),
child: const Text("Submit", style: TextStyle(color: Colors.white)),
),
),
],
),
),
),
),
),
);
}
}
Q #9
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
Widget _buildVerticalLine() {
return Container(
width: 1,
color: Colors.grey,
margin: EdgeInsets.symmetric(horizontal: 10),
);
}
Widget _buildGenderSection() {
return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Gender", style: TextStyle(fontWeight: FontWeight.bold)),
Row(
children: [
_radioButton("Male"),
SizedBox(width: 10),
_radioButton("Female"),
],
),
],
),
);
}
Widget _buildSubjectsSection() {
return Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Subjects", style: TextStyle(fontWeight: FontWeight.bold)),
Row(
children: [
_checkbox("Phy"),
SizedBox(width: 10),
_checkbox("Chem"),
SizedBox(width: 10),
_checkbox("Bio"),
],
),
],
),
);
}
Widget _buildSkillsBox() {
return Container(
width: 150,
height: 120,
decoration: BoxDecoration(
border: Border.all(color: Colors.black),
),
padding: EdgeInsets.all(10),
child: Text(
"C++\nJava\nJavascript\nC#",
style: TextStyle(fontSize: 14),
),
);
}
Widget _buildSubmitButton() {
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.grey[400],
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
),
onPressed: () {},
child: Text("Submit"),
);
}
}