6. Develop an application to Check the Weather in Countries Across the world (Weather app).
Step to Run
- Open your Flutter project in VS Code.
- Open
lib/main.dart. - Delete the existing code.
- Paste the program code given below.
- Save the file.
- Run the program using
flutter runcmd
PROGRAM:
import 'package:flutter/material.dart';
void main() {
runApp(const WeatherApp());
}
class WeatherApp extends StatelessWidget {
const WeatherApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Lab Program 6',
debugShowCheckedModeBanner: false,
home: const WeatherScreen(),
);
}
}
class WeatherScreen extends StatefulWidget {
const WeatherScreen({super.key});
@override
State<WeatherScreen> createState() => _WeatherScreenState();
}
class _WeatherScreenState extends State<WeatherScreen> {
final TextEditingController countryController = TextEditingController();
String country = '';
String temperature = '';
String condition = '';
String message = 'Enter a country name to check weather';
final Map<String, Map<String, String>> weatherData = {
'india': {
'country': 'India',
'temperature': '29°C',
'condition': 'Sunny',
},
'usa': {
'country': 'USA',
'temperature': '22°C',
'condition': 'Cloudy',
},
'uk': {
'country': 'United Kingdom',
'temperature': '15°C',
'condition': 'Rainy',
},
'canada': {
'country': 'Canada',
'temperature': '10°C',
'condition': 'Cold',
},
'australia': {
'country': 'Australia',
'temperature': '26°C',
'condition': 'Clear Sky',
},
'japan': {
'country': 'Japan',
'temperature': '18°C',
'condition': 'Windy',
},
'germany': {
'country': 'Germany',
'temperature': '14°C',
'condition': 'Foggy',
},
};
void checkWeather() {
String input = countryController.text.trim().toLowerCase();
setState(() {
if (weatherData.containsKey(input)) {
country = weatherData[input]!['country']!;
temperature = weatherData[input]!['temperature']!;
condition = weatherData[input]!['condition']!;
message = '';
} else {
country = '';
temperature = '';
condition = '';
message = 'Weather data not found for this country';
}
});
}
void clearWeather() {
setState(() {
countryController.clear();
country = '';
temperature = '';
condition = '';
message = 'Enter a country name to check weather';
});
}
@override
void dispose() {
countryController.dispose();
super.dispose();
}
IconData getWeatherIcon(String condition) {
if (condition == 'Sunny') {
return Icons.wb_sunny;
} else if (condition == 'Rainy') {
return Icons.water_drop;
} else if (condition == 'Cloudy') {
return Icons.cloud;
} else if (condition == 'Cold') {
return Icons.ac_unit;
} else if (condition == 'Windy') {
return Icons.air;
} else {
return Icons.wb_cloudy;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Program 6: Weather App'),
centerTitle: true,
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(
Icons.cloud,
size: 80,
color: Colors.blue,
),
const SizedBox(height: 20),
TextField(
controller: countryController,
decoration: const InputDecoration(
labelText: 'Enter Country Name',
hintText: 'Example: India',
border: OutlineInputBorder(),
prefixIcon: Icon(Icons.public),
),
),
const SizedBox(height: 20),
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: checkWeather,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 14),
),
child: const Text(
'Check Weather',
style: TextStyle(fontSize: 18),
),
),
),
const SizedBox(height: 15),
ElevatedButton(
onPressed: clearWeather,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
),
child: const Text('Clear'),
),
const SizedBox(height: 30),
if (message.isNotEmpty)
Text(
message,
textAlign: TextAlign.center,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
if (country.isNotEmpty)
Card(
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
children: [
Icon(
getWeatherIcon(condition),
size: 70,
color: Colors.orange,
),
const SizedBox(height: 15),
Text(
country,
style: const TextStyle(
fontSize: 28,
fontWeight: FontWeight.bold,
color: Colors.blue,
),
),
const SizedBox(height: 10),
Text(
temperature,
style: const TextStyle(
fontSize: 36,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
Text(
condition,
style: const TextStyle(
fontSize: 22,
color: Colors.black54,
),
),
],
),
),
),
],
),
),
);
}
}OUTPUT:

