Flutter Tutorial - World Time Project

Merhaba arkadaşlar, bu yazımda sizlere Flutter ile World Time Project isimli bir uygulama yaparak seçeceğimiz ülke ve şehire göre verileri nasıl görüntüleyebiliriz bundan bahsedeceğim...

main.dart

import 'package:flutter/material.dart'; import 'package:worldtime/pages/home.dart'; import 'package:worldtime/pages/loading.dart'; import 'package:worldtime/pages/choose_location.dart'; void main() => runApp(MaterialApp( initialRoute: '/', routes: { '/': (context) => Loading(), '/home': (context) => Home(), '/location': (context) => ChooseLocation(), }, ));

services/world_time.dart

import 'package:http/http.dart'; import 'dart:convert'; //import 'package:intl/intl.dart'; class WorldTime { String location; // Location name for th UI String time; // The time in that location String flag; // Url to an asset flag icon String url; // Location url for api endpoint bool isDayTime; // True or False if daytime or not WorldTime ({ this.location, this.flag, this.url }); Future getTime() async { try { // Make the request Response response = await get('http://www.worldtimeapi.org/api/timezone/$url'); Map data = json.decode(response.body); //print(data); // Get properties from data String datetime = data['datetime'].substring(11,16); //String datetime = data['datetime']; //String offset = data['utc_offset'].substring(1,3); //print("DateTime: " + datetime); //print("utc_offset: " + offset); // Create DateTime object //DateTime now = DateTime.parse(datetime); //now = now.add(Duration(hours: int.parse(offset))); String now = datetime.substring(0,2); int nowInt = int.parse(now); // Set the time property isDayTime = nowInt >= 6 && nowInt <= 20 ? true : false; //time = DateFormat.jm().format(nowInt); time = datetime; } catch (e) { print('Caught Error: $e'); time = 'Could not get time data, please try again later!'; } } }

pages/home.dart

import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class Home extends StatefulWidget { @override _HomeState createState() => _HomeState(); } class _HomeState extends State { Map data = {}; @override Widget build(BuildContext context) { data = data.isNotEmpty ? data : ModalRoute.of(context).settings.arguments; print(data); String bgImage = data['isDayTime'] ? 'day.jpg' : 'night.jpg'; Color bgColor = data['isDayTime'] ? Colors.purple[200] : Colors.purple[400]; return Scaffold( backgroundColor: bgColor, body: SafeArea( child: Container( decoration: BoxDecoration( image: DecorationImage( image: AssetImage('assets/$bgImage'), fit: BoxFit.cover, ), ), child: Padding( padding: const EdgeInsets.fromLTRB(0, 75.0, 0, 0), child: Column( children: [ FlatButton.icon( onPressed: () async { dynamic result = await Navigator.pushNamed(context, '/location'); setState(() { data = { 'time': result['time'], 'location': result['location'], 'isDayTime': result['isDayTime'], 'flag': result['flag'], }; }); }, icon: Icon( Icons.edit_location, color: Colors.grey[300], ), label: Text( "Edit Location", style: TextStyle( color: Colors.grey[300], ), ), ), SizedBox(height: 20.0), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text( data['location'], style: TextStyle( fontSize: 28.0, letterSpacing: 2.0, color: Colors.white, ), ), ], ), SizedBox(height: 20.0), Text( data['time'], style: TextStyle( fontSize: 66.0, letterSpacing: 2.0, color: Colors.white, ), ), ], ), ), ), ), ); } }

pages/loading.dart

import 'package:flutter/material.dart'; import 'package:worldtime/services/world_time.dart'; import 'package:flutter_spinkit/flutter_spinkit.dart'; class Loading extends StatefulWidget { @override _LoadingState createState() => _LoadingState(); } class _LoadingState extends State { void setupWorldTime() async { WorldTime instance = WorldTime(location: 'Istanbul', flag: 'turkey.png', url: 'Europe/Istanbul'); await instance.getTime(); Navigator.pushReplacementNamed(context, '/home', arguments: { 'location': instance.location, 'flag': instance.flag, 'time': instance.time, 'isDayTime': instance.isDayTime, }); } @override void initState() { super.initState(); setupWorldTime(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.blue[900], body: Center( child: SpinKitCubeGrid( color: Colors.white, size: 80.0, ), ), ); } }

pages/choose_location.dart

import 'package:flutter/material.dart'; import 'package:worldtime/services/world_time.dart'; class ChooseLocation extends StatefulWidget { @override _ChooseLocationState createState() => _ChooseLocationState(); } class _ChooseLocationState extends State { List locations = [ WorldTime(url: 'Europe/Istanbul', location: 'Istanbul', flag: 'tr.png'), WorldTime(url: 'America/New_York', location: 'New York', flag: 'us.png'), WorldTime(url: 'Asia/Baku', location: 'Baku', flag: 'az.png'), WorldTime(url: 'Australia/Sydney', location: 'Sydney', flag: 'au.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( backgroundColor: Colors.grey[200], appBar: AppBar( backgroundColor: Colors.blue[900], title: Text("Choose a Location"), centerTitle: true, elevation: 0, ), 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( onTap: (){ updateTime(index); }, title: Text(locations[index].location), leading: Image( image: AssetImage('assets/${locations[index].flag}'), ), ), ), ); }, ), ); } }

Yorum Gönder

Daha yeni Daha eski