Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

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;

const String catFactsApiUrl = 'https://catfact.ninja/fact';

void main() {
tz.initializeTimeZones();
runApp(CatFactsApp());
}

class CatFactsApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cat Facts',
theme: ThemeData(
primaryColor: Color.fromARGB(255, 179, 199, 218),
fontFamily: 'Roboto',
),
home: CatFactsPage(),
);
}
}

class CatFactsPage extends StatefulWidget {


@override
_CatFactsPageState createState() => _CatFactsPageState();
}

class _CatFactsPageState extends State<CatFactsPage> {


List<String> catFactsList = [];
bool isLoading = false;
bool isError = false;

@override
void initState() {
super.initState();
fetchCatFact();
}

Future<void> fetchCatFact() async {


setState(() {
isLoading = true;
});

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;
});
}
}

Widget buildCatFactCard(String fact) {


return Card(
elevation: 8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
fact,
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
);
}

@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;

const String catFactsApiUrl = 'https://catfact.ninja/fact';


void main() {
tz.initializeTimeZones();
runApp(CatFactsApp());
}

class CatFactsApp extends StatelessWidget {


@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Cat Facts',
theme: ThemeData(
primaryColor: Color.fromARGB(255, 179, 199, 218),
fontFamily: 'Roboto',
),
home: CatFactsPage(),
);
}
}

class CatFactsPage extends StatefulWidget {


@override
_CatFactsPageState createState() => _CatFactsPageState();
}

class _CatFactsPageState extends State<CatFactsPage> {


List<String> catFactsList = [];
bool isLoading = false;
bool isError = false;

@override
void initState() {
super.initState();
fetchCatFact();
}

Future<void> fetchCatFact() async {


setState(() {
isLoading = true;
});

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;
});
}
}

void shareCatFact(String fact) {


// Implement the logic to share the cat fact
// For example, you can use the 'share' package to share the fact through
various apps.
}

void removeCatFact(int index) {


setState(() {
catFactsList.removeAt(index);
});
}

Widget buildCatFactCard(String fact, int index) {


return Card(
elevation: 8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Text(
fact,
style: TextStyle(fontSize: 24.0, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
SizedBox(height: 8),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () => shareCatFact(fact),
child: Text('Share'),
),
SizedBox(width: 8),
ElevatedButton(
onPressed: () => removeCatFact(index),
child: Text('Remove'),
style: ElevatedButton.styleFrom(),
),
],
),
],
),
),
);
}

@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),
),
],
),
),
),
],
),
);
}
}

You might also like