天气api页面
请帮助我,我在代码中遇到问题,我想从mycity变量中更改myURL变量,我有一套如何做到这一点的城市:
编辑器错误:在构造函数_WeatherState#823a5中调用了setState()(生命周期状态:已创建,未安装任何小部件)
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:marwa_app/component/myResponsiveLibrary.dart';
import 'package:marwa_app/component/Logo.dart';
import 'package:marwa_app/component/MyDrawer.dart';
import 'package:http/http.dart' as http;
class Weather extends StatefulWidget {
@override
_WeatherState createState() => _WeatherState();
}
class _WeatherState extends State<Weather> {
List <String> mycity = ['aqaba','amman'];
int i= 0;
void cityValue(value) {
setState(() {
i = value;
});
}
@override
Future<Map> getData() async{
String myURL = await 'http://api.openweathermap.org/data/2.5/forecast?q=${mycity[i]}&appid=41a6cd80a840ce00a3c77b8ba1a04022&units=metric';
http.Response response = await http.get(myURL);
return json.decode(response.body);
}
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: MainModel().mainColor,
appBar: AppBar(
backgroundColor: Color(0xff323266),
leading: IconButton(
icon: Icon(
Icons.notifications_active,
color: MainModel().thirdColor,
),
onPressed: () => {}),
title: Logo(),
centerTitle: true,
actions: [
Builder(
builder: (context) => IconButton(
icon: Icon(
Icons.menu,
color: MainModel().thirdColor,
),
onPressed: () => Scaffold.of(context).openEndDrawer(),
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
),
),
],
),
endDrawer: Drawer(
child: MyDrawer(),
),
body: widgetMyData()
);
}
}
Widget widgetMyData() {
return FutureBuilder(future: _WeatherState().getData(),
builder: (BuildContext context, AsyncSnapshot<Map> snapshop){
final widthScreen = MediaQuery.of(context).size.width;
if(snapshop.hasData){
Map content = snapshop.data;
return Container(
color: MainModel().secondColor,
child: Center(
child: ListView(
physics: NeverScrollableScrollPhysics(),
children: [
Padding(padding: EdgeInsets.only(top: 20)),
Container(
margin: EdgeInsets.symmetric(horizontal: MainModel().setPadding(MainModel().largePadding, widthScreen)),
height: 300,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.15),
spreadRadius: 5,
blurRadius: 5,
offset: Offset(2, 2), // changes position of shadow
),],
borderRadius: BorderRadius.all(Radius.circular(40)),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
MainModel().mainColor, MainModel().secondColor
])
),
child: Column(
children: [
Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().smallPadding, widthScreen)),),
Image.network('http://openweathermap.org/img/w/${content["list"][0]["weather"][0]["icon"]}.png',
color: MainModel().whiteColor.withOpacity(0.7),
fit: BoxFit.contain,
width: 120,),
Text('${content['list'][0]['weather'][0]['description']}',style: TextStyle(
fontSize: MainModel().setFont(MainModel().middleFont, widthScreen),
color: MainModel().whiteColor.withOpacity(0.7),
),),
Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding * 1.5, widthScreen)),),
Text("${content['list'][0]['main']['temp']}\u00b0C",
style: TextStyle(
fontSize: MainModel().setFont(MainModel().largeFont * 1.5, widthScreen),
color: MainModel().whiteColor,
fontWeight: FontWeight.bold
),),
Container(
alignment: Alignment.center,
child: Text("${content['city']['name']}",style: TextStyle(
fontSize: MainModel().setFont(MainModel().middleFont, widthScreen),
color: MainModel().whiteColor,
),)
),
Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding * 1.5, widthScreen)),),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text("${content['list'][0]['main']['temp_max']}\u00b0C",style: TextStyle(
fontSize: MainModel().setFont(MainModel().middleFont, widthScreen),
color: MainModel().whiteColor,
fontWeight: FontWeight.bold
),),
Text("${content['list'][0]['main']['temp_min']}\u00b0C",style: TextStyle(
fontSize: MainModel().setFont(MainModel().middleFont, widthScreen),
color: MainModel().whiteColor,
fontWeight: FontWeight.bold
),),
],
),
],
),
),
Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding * 2, widthScreen))),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),
Container(
height: MainModel().setSize(MainModel().largeSize, widthScreen),
width: MainModel().setSize(MainModel().largeSize, widthScreen),
decoration: BoxDecoration(
color: Color(0xffFFB20F),
borderRadius: BorderRadius.all(Radius.circular(40.0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding, widthScreen))),
],
)
),
Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),
Container(
height: MainModel().setSize(MainModel().largeSize, widthScreen),
width: MainModel().setSize(MainModel().largeSize, widthScreen),
decoration: BoxDecoration(
color: Color(0xff0DA6A6),
borderRadius: BorderRadius.all(Radius.circular(40.0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding, widthScreen))),
],
)
),
Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),
Container(
height: MainModel().setSize(MainModel().largeSize, widthScreen),
width: MainModel().setSize(MainModel().largeSize, widthScreen),
decoration: BoxDecoration(
color: Color(0xff6D5DF1),
borderRadius: BorderRadius.all(Radius.circular(40.0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding, widthScreen))),
],
)
),
Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),
FlatButton(onPressed: () {
_WeatherState().cityValue(1);
},
child: Container(
height: MainModel().setSize(MainModel().largeSize, widthScreen),
width: MainModel().setSize(MainModel().largeSize, widthScreen),
decoration: BoxDecoration(
color: Color(0xffFF0F97),
borderRadius: BorderRadius.all(Radius.circular(40.0)),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text('click'),
Padding(padding: EdgeInsets.only(top: MainModel().setPadding(MainModel().largePadding, widthScreen))),
],
)
),
),
Padding(padding: EdgeInsets.only(left: MainModel().setPadding(MainModel().middlePadding, widthScreen))),
],
))
]),
),
);
}else{
return Container(
child: Text('sorry you data is not found'),
);
}
}
);
}
如果单击_WeatherState()。cityValue(1); 更改api城市名称
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
您可以将getData()
和cityValue
传递给widgetMyData(Future<Map> _future, Function callback)
代码段
Future<Map> _future;
...
@override
initState() {
_future = getData();
super.initState();
}
...
body: widgetMyData(_future, cityValue));
...
Widget widgetMyData(Future<Map> _future, Function callback) {
return FutureBuilder(
future: _future,
...
FlatButton(
onPressed: () {
callback(1);
},
child: Text('click'))
工作演示
完整代码
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
class Weather extends StatefulWidget {
@override
_WeatherState createState() => _WeatherState();
}
class _WeatherState extends State<Weather> {
List<String> mycity = ['aqaba', 'amman'];
int i = 0;
Future<Map> _future;
void cityValue(value) {
setState(() {
i = value;
_future = getData();
});
}
@override
initState() {
_future = getData();
super.initState();
}
Future<Map> getData() async {
String myURL =
await 'https://api.openweathermap.org/data/2.5/forecast?q=${mycity[i]}&appid=41a6cd80a840ce00a3c77b8ba1a04022&units=metric';
http.Response response = await http.get(myURL);
return json.decode(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
//backgroundColor: MainModel().mainColor,
appBar: AppBar(
backgroundColor: Color(0xff323266),
leading: IconButton(
icon: Icon(
Icons.notifications_active,
//color: MainModel().thirdColor,
),
onPressed: () => {}),
title: Text("Logo()"),
centerTitle: true,
actions: [
Builder(
builder: (context) => IconButton(
icon: Icon(
Icons.menu,
//color: MainModel().thirdColor,
),
onPressed: () => Scaffold.of(context).openEndDrawer(),
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
),
),
],
),
body: widgetMyData(_future, cityValue));
}
}
Widget widgetMyData(Future<Map> _future, Function callback) {
return FutureBuilder(
future: _future,
builder: (BuildContext context, AsyncSnapshot<Map> snapshop) {
final widthScreen = MediaQuery.of(context).size.width;
if (snapshop.hasData) {
Map content = snapshop.data;
return Container(
child: Center(
child:
ListView(physics: NeverScrollableScrollPhysics(), children: [
Padding(padding: EdgeInsets.only(top: 20)),
Container(
height: 300,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.15),
spreadRadius: 5,
blurRadius: 5,
offset: Offset(2, 2), // changes position of shadow
),
],
borderRadius: BorderRadius.all(Radius.circular(40)),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.blue, Colors.red])),
child: Column(
children: [
Image.network(
'https://openweathermap.org/img/w/${content["list"][0]["weather"][0]["icon"]}.png',
//color: MainModel().whiteColor.withOpacity(0.7),
fit: BoxFit.contain,
width: 120,
),
Text(
'${content['list'][0]['weather'][0]['description']}',
),
Text(
"${content['list'][0]['main']['temp']}\u00b0C",
),
Container(
alignment: Alignment.center,
child: Text(
"${content['city']['name']}",
)),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Text(
"${content['list'][0]['main']['temp_max']}\u00b0C",
),
Text(
"${content['list'][0]['main']['temp_min']}\u00b0C",
),
],
),
],
),
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
FlatButton(
onPressed: () {
callback(1);
},
child: Text('click')),
],
))
]),
),
);
} else {
return Container(
child: Text('sorry you data is not found'),
);
}
});
}
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Weather(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}