반응형
코드 작성
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Weather App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: WeatherPage(),
);
}
}
class WeatherPage extends StatefulWidget {
@override
_WeatherPageState createState() => _WeatherPageState();
}
class _WeatherPageState extends State<WeatherPage> {
String _cityName = 'New York';
String _apiKey = 'YOUR_API_KEY'; // Replace this with your OpenWeatherMap API key
String _apiUrl =
'http://api.openweathermap.org/data/2.5/weather?q=$_cityName&appid=$_apiKey&units=metric';
var _weatherData;
Future<void> fetchWeatherData() async {
final response = await http.get(Uri.parse(_apiUrl));
final decodedResponse = json.decode(response.body);
setState(() {
_weatherData = decodedResponse;
});
}
@override
void initState() {
super.initState();
fetchWeatherData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Weather App'),
),
body: _weatherData == null
? Center(child: CircularProgressIndicator())
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'City: $_cityName',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
SizedBox(height: 10),
Text(
'Temperature: ${_weatherData['main']['temp']} °C',
style: TextStyle(fontSize: 18),
),
SizedBox(height: 10),
Text(
'Description: ${_weatherData['weather'][0]['description']}',
style: TextStyle(fontSize: 18),
),
],
),
);
}
}
반응형
'개발 > Flutter' 카테고리의 다른 글
Flutter 플러터 Rest Api 호출 통신 간단 구현 (0) | 2024.03.16 |
---|---|
Flutter 플러터 Textfield 사용법 간단 구현 (0) | 2024.03.15 |
Flutter 플러터 음악 앱 만들기 Music (0) | 2024.03.15 |
플러터 Flutter 스크롤 뷰 간단 구현 Scroll View (0) | 2023.11.28 |
플러터 Flutter 스톱워치 간단 구현 Stopwatch (0) | 2023.05.30 |
플러터 Flutter run : arch -x86_64 sudo gem install ffi 에러 (0) | 2023.04.01 |
플러터 Flutter Cannot find xcode build 에러가 뜰경우 (0) | 2023.04.01 |
플러터 Flutter 콘솔 유용한 명령어 모음 VSCode Console (0) | 2023.03.31 |
댓글