这是一个世界时间应用程序,从主屏幕开始显示默认位置,然后您可以更改位置,API 会为您获取该新位置的时间。它工作正常,但是当我点击“位置”栏中的后退按钮时,它给我一个错误!
这是我的主页代码:
class _HomeState extends State<Home> {
Map data = {};
@override
Widget build(BuildContext context) {
//the argument method is taking data from another route
//I use this
data = data.isNotEmpty ? data : ModalRoute.of(context).settings.arguments;
//data = ModalRoute.of(context).settings.arguments;
//isDaytime is a bool
String bgImage = data['isDaytime'] ? 'day.png' : 'night.png';
Color bar = data['isDaytime'] ? Colors.blue[300] : Colors.indigo[900];
return Scaffold(
backgroundColor: bar,
body: SafeArea(
child: Container(
width: 415.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/$bgImage'),
fit: BoxFit.cover,
),
),
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 120.0, 0, 0),
child: Column(
children: <Widget>[
FlatButton.icon(
onPressed: () async {
dynamic result =
await Navigator.pushNamed(context, '/location');
setState(() {
data = result;
});
},
icon: Icon(
Icons.edit_location,
color: Colors.grey[200],
size: 25.0,
),
label: Text(
'Edit Location',
style: TextStyle(
color: Colors.grey[200],
fontSize: 20.0,
),
),
),
RichText(
text: TextSpan(
text: data['location'],
style: TextStyle(
color: Colors.white,
fontSize: 40.0,
),
),
),
RichText(
text: TextSpan(
text: data['time'],
style: TextStyle(
color: Colors.white,
fontSize: 70.0,
),
),
),
],
),
),
),
),
);
} }
这是我的位置屏幕代码:
class location extends StatefulWidget {
@override
_locationState createState() => _locationState();
}
class _locationState extends State<location> {
List<Worldtime> locations = [
Worldtime(url: 'Africa/Cairo', location: 'Cairo', flag: 'egypt.png'),
Worldtime(url: 'Europe/London', location: 'London', flag: 'uk.png'),
Worldtime(url: 'America/New_York', location: 'New york', flag: 'usa.png'),
Worldtime(url: 'Europe/Berlin', location: 'Berlin', flag: 'germany.png')
];
void updateTime(index) async {
Worldtime instance = locations[index];
await instance.getTime();
Navigator.pop(context, {
'location' : instance.location,
'flag' : instance.flag,
'time' : instance.time,
'isDaytime' : instance.isDaytime,
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: locations.length,
itemBuilder: (context, index) {
return Padding(
padding:
const EdgeInsets.symmetric(vertical: 1.0, horizontal: 4.0),
child: Card(
child: ListTile(
title: Text(locations[index].location),
leading: CircleAvatar(
backgroundImage:
AssetImage('assets/${locations[index].flag}'),
),
onTap: () {
updateTime(index);
},
),
),
);
}),
appBar: AppBar(
title: Text('Choose Location'),
backgroundColor: Colors.grey[800],
elevation: 0,
),
);
}
}