Skip to content

Instantly share code, notes, and snippets.

@theindianappguy
Created October 4, 2020 15:41
Show Gist options
  • Save theindianappguy/b5af8a97e7caa57136001bdb0869025d to your computer and use it in GitHub Desktop.
Save theindianappguy/b5af8a97e7caa57136001bdb0869025d to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:recipe_app/models/recipe_model.dart';
import 'package:recipe_app/views/recipe_view.dart';
import 'package:url_launcher/url_launcher.dart';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<RecipeModel> recipies = new List();
String ingridients;
bool _loading = false;
String query = "";
TextEditingController textEditingController = new TextEditingController();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [
const Color(0xff213A50),
const Color(0xff071930)
],
begin: FractionalOffset.topRight,
end: FractionalOffset.bottomLeft)),
),
SingleChildScrollView(
child: Container(
padding: EdgeInsets.symmetric(vertical: !kIsWeb ? Platform.isIOS? 60: 30 : 30, horizontal: 24),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: kIsWeb
? MainAxisAlignment.start
: MainAxisAlignment.center,
children: <Widget>[
Text(
"AppGuy",
style: TextStyle(
fontSize: 18,
color: Colors.white,
fontFamily: 'Overpass'),
),
Text(
"Recipes",
style: TextStyle(
fontSize: 18,
color: Colors.blue,
fontFamily: 'Overpass'),
)
],
),
SizedBox(
height: 60,
),
Text(
"What will you cook today?",
style: TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.w400,
fontFamily: 'Overpass'),
),
Text(
"Just Enter Ingredients you have and we will show the best recipe for you",
style: TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.w300,
fontFamily: 'OverpassRegular'),
),
SizedBox(
height: 40,
),
Container(
child: Row(
children: <Widget>[
Expanded(
child: TextField(
controller: textEditingController,
style: TextStyle(
fontSize: 16,
color: Colors.white,
fontFamily: 'Overpass'),
decoration: InputDecoration(
hintText: "Enter Ingridients",
hintStyle: TextStyle(
fontSize: 16,
color: Colors.white.withOpacity(0.5),
fontFamily: 'Overpass'),
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.white),
),
),
),
),
SizedBox(
width: 16,
),
InkWell(
onTap: () async {
if (textEditingController.text.isNotEmpty) {
setState(() {
_loading = true;
});
recipies = new List();
String url =
"https://api.edamam.com/search?q=${textEditingController.text}&app_id=0f21d949&app_key=8bcdd93683d********5cb95e64ab26";
var response = await http.get(url);
print(" $response this is response");
Map<String, dynamic> jsonData =
jsonDecode(response.body);
print("this is json Data $jsonData");
jsonData["hits"].forEach((element) {
print(element.toString());
RecipeModel recipeModel = new RecipeModel();
recipeModel =
RecipeModel.fromMap(element['recipe']);
recipies.add(recipeModel);
print(recipeModel.url);
});
setState(() {
_loading = false;
});
print("doing it");
} else {
print("not doing it");
}
},
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8),
gradient: LinearGradient(
colors: [
const Color(0xffA2834D),
const Color(0xffBC9A5F)
],
begin: FractionalOffset.topRight,
end: FractionalOffset.bottomLeft)),
padding: EdgeInsets.all(8),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(
Icons.search,
size: 18,
color: Colors.white
),
],
),
)),
],
),
),
SizedBox(
height: 30,
),
Container(
child: GridView(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
mainAxisSpacing: 10.0, maxCrossAxisExtent: 200.0),
shrinkWrap: true,
scrollDirection: Axis.vertical,
physics: ClampingScrollPhysics(),
children: List.generate(recipies.length, (index) {
return GridTile(
child: RecipieTile(
title: recipies[index].label,
imgUrl: recipies[index].image,
desc: recipies[index].source,
url: recipies[index].url,
));
})),
),
],
),
),
)
],
),
);
}
}
class RecipieTile extends StatefulWidget {
final String title, desc, imgUrl, url;
RecipieTile({this.title, this.desc, this.imgUrl, this.url});
@override
_RecipieTileState createState() => _RecipieTileState();
}
class _RecipieTileState extends State<RecipieTile> {
_launchURL(String url) async {
print(url);
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
@override
Widget build(BuildContext context) {
return Wrap(
children: <Widget>[
GestureDetector(
onTap: () {
if (kIsWeb) {
_launchURL(widget.url);
} else {
print(widget.url + " this is what we are going to see");
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => RecipeView(
postUrl: widget.url,
)));
}
},
child: Container(
margin: EdgeInsets.all(8),
child: Stack(
children: <Widget>[
Image.network(
widget.imgUrl,
height: 200,
width: 200,
fit: BoxFit.cover,
),
Container(
width: 200,
alignment: Alignment.bottomLeft,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.white30, Colors.white],
begin: FractionalOffset.centerRight,
end: FractionalOffset.centerLeft)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
widget.title,
style: TextStyle(
fontSize: 13,
color: Colors.black54,
fontFamily: 'Overpass'),
),
Text(
widget.desc,
style: TextStyle(
fontSize: 10,
color: Colors.black54,
fontFamily: 'OverpassRegular'),
)
],
),
),
)
],
),
),
),
],
);
}
}
class GradientCard extends StatelessWidget {
final Color topColor;
final Color bottomColor;
final String topColorCode;
final String bottomColorCode;
GradientCard(
{this.topColor,
this.bottomColor,
this.topColorCode,
this.bottomColorCode});
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Wrap(
children: <Widget>[
Container(
child: Stack(
children: <Widget>[
Container(
height: 160,
width: 180,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [topColor, bottomColor],
begin: FractionalOffset.topLeft,
end: FractionalOffset.bottomRight)),
),
Container(
width: 180,
alignment: Alignment.bottomLeft,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.white30, Colors.white],
begin: FractionalOffset.centerRight,
end: FractionalOffset.centerLeft)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
Text(
topColorCode,
style: TextStyle(fontSize: 16, color: Colors.black54),
),
Text(
bottomColorCode,
style: TextStyle(fontSize: 16, color: bottomColor),
)
],
),
),
)
],
),
),
],
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment