Professional Documents
Culture Documents
Finalintoy
Finalintoy
dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
void main() {
tz.initializeTimeZones();
runApp(CatFactsApp());
}
@override
void initState() {
super.initState();
fetchCatFact();
}
try {
http.Response response = await http.get(Uri.parse(catFactsApiUrl));
if (response.statusCode == 200) {
Map<String, dynamic> responseData = json.decode(response.body);
if (responseData.containsKey('fact')) {
setState(() {
catFactsList.insert(0, responseData['fact']); // Add the new fact at
the beginning
isError = false;
});
} else {
setState(() {
isError = true;
});
}
} else {
setState(() {
isError = true;
});
}
} catch (e) {
setState(() {
isError = true;
});
} finally {
setState(() {
isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
children: [
Icon(Icons.pets), // Cat icon in the app bar
SizedBox(width: 8),
Text('Cat Facts'),
],
),
),
body: Stack(
children: [
// Background image container
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/pp.jpg'), // Add your image here
fit: BoxFit.cover,
),
),
),
Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
isLoading
? CircularProgressIndicator()
: catFactsList.isEmpty
? Text('No cat facts available.')
: Expanded(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: catFactsList
.map((fact) => Padding(
padding: const
EdgeInsets.symmetric(vertical: 8.0),
child: buildCatFactCard(fact),
))
.toList(),
),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: isLoading ? null : fetchCatFact,
child: isLoading
? CircularProgressIndicator()
: Text('Fetch New Cat Fact'),
),
SizedBox(height: 10),
if (isError)
Text(
'Failed to fetch cat fact. Please try again later.',
style: TextStyle(color: Colors.red),
),
],
),
),
),
],
),
);
}
}
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
@override
void initState() {
super.initState();
fetchCatFact();
}
try {
http.Response response = await http.get(Uri.parse(catFactsApiUrl));
if (response.statusCode == 200) {
Map<String, dynamic> responseData = json.decode(response.body);
if (responseData.containsKey('fact')) {
setState(() {
catFactsList.insert(0, responseData['fact']); // Add the new fact at
the beginning
isError = false;
});
} else {
setState(() {
isError = true;
});
}
} else {
setState(() {
isError = true;
});
}
} catch (e) {
setState(() {
isError = true;
});
} finally {
setState(() {
isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Row(
children: [
Icon(Icons.pets), // Cat icon in the app bar
SizedBox(width: 8),
Text('Cat Facts'),
Icon(Icons.pets), // Cat icon in the app bar
],
),
),
body: Stack(
children: [
// Background image container
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/pp.jpg'), // Add your image here
fit: BoxFit.cover,
),
),
),
Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
isLoading
? CircularProgressIndicator()
: catFactsList.isEmpty
? Text('No cat facts available.')
: Expanded(
child: RefreshIndicator(
onRefresh: fetchCatFact, // Implement the
refresh logic
child: SingleChildScrollView(
child: Column(
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: catFactsList
.asMap()
.map((index, fact) => MapEntry(
index,
Padding(
padding: const
EdgeInsets.symmetric(vertical: 8.0),
child: buildCatFactCard(fact,
index),
),
))
.values
.toList(),
),
),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: isLoading ? null : fetchCatFact,
child: isLoading
? CircularProgressIndicator()
: Text('Fetch New Cat Fact'),
),
SizedBox(height: 10),
if (isError)
Text(
'Failed to fetch cat fact. Please try again later.',
style: TextStyle(color: Colors.red),
),
],
),
),
),
],
),
);
}
}