پیش به سوی تابستان؛ با تخفیفهای داغ راکت!
بزن بریم!سلام وقت بخیر من یک لیست ویو دارم که میخوام لوکال سرچ روش انجام بشه کد های صفحه اصلی برابر هست با :
import 'dart:convert';
import 'package:eregim_flutter/models/calories.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:eregim_flutter/main.dart';
import 'package:http/http.dart' as http;
class CaloriesPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return CaloriesState();
}
}
class CaloriesState extends State<CaloriesPage> {
List<Calories> _calories = List<Calories>();
List<Calories> _caloriesForDisplay = List<Calories>();
Future<List<Calories>> fetchNotes() async {
var url =
'https://**********.com/*********';
var response = await http.get(url);
var calories = List<Calories>();
if (response.statusCode == 200) {
var notesJson = json.decode(response.body);
for (var noteJson in notesJson) {
calories.add(Calories.fromJson(noteJson));
}
}
return calories;
}
@override
void initState() {
fetchNotes().then((value) {
setState(() {
_calories.addAll(value);
_caloriesForDisplay = _calories;
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return _calories.isEmpty || _calories == null
? loading()
: Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
backgroundColor: Color(0XffEBEEF2),
appBar: AppBar(
backgroundColor: Color(0XffFFFFFF),
leading: GestureDetector(
child: Icon(
Icons.arrow_back_ios,
color: Color(0Xff7FCD91),
),
onTap: () {
Navigator.pop(context);
},
),
title: Text(
"کالری یاب",
style: TextStyle(
fontSize: 18,
color: Color(0Xff464646),
fontFamily: "Shabnam"),
),
),
body: ListView.builder(
itemBuilder: (context, index) {
return index == 0 ? _searchBar() : _listItem(index - 1);
},
itemCount: _caloriesForDisplay.length + 1,
)
)
);
}
_searchBar() {
Padding(
padding: const EdgeInsets.only(right: 8, left: 8, top: 9, bottom: 1),
child: TextField(
onChanged: (text) {
text = text.toLowerCase();
setState(() {
_caloriesForDisplay = _calories.where((calory) {
var noteTitle = calory.food_name.toLowerCase();
return noteTitle.contains(text);
}).toList();
});
},
decoration: InputDecoration(
labelText: "نام ماده غذایی",
hintStyle: TextStyle(fontFamily: "Shabnam", color: Colors.black),
labelStyle: TextStyle(fontFamily: "Shabnam", color: Colors.black),
hintText: "نام ماده غذایی را وارد نمایید",
prefixIcon: Icon(Icons.search),
border: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(25.0)))),
),
);
}
_listItem(index) {
Padding(
padding: const EdgeInsets.only(top: 12, left: 10, right: 10),
child: Container(
height: 80,
width: double.infinity,
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.circular(15.0),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.only(right: 20),
child: Text(
_caloriesForDisplay[index].food_name,
style: TextStyle(
color: Colors.blue, fontFamily: "Shabnam", fontSize: 18),
textAlign: TextAlign.right,
),
),
),
Expanded(
flex: 1,
child: Text(
_caloriesForDisplay[index].value,
style: TextStyle(
color: Colors.green, fontFamily: "Shabnam", fontSize: 16),
textAlign: TextAlign.center,
),
),
Expanded(
flex: 1,
child: Text(
_caloriesForDisplay[index].nutritional_value + ' ' + 'کالری',
style: TextStyle(
color: Colors.black, fontFamily: "Shabnam", fontSize: 16),
textAlign: TextAlign.center,
),
)
],
),
),
);
}
}
و خروجی جیسون من بصورت زیر هست:
{
"food_name": "آلبالو (کمپوت)",
"value": "100گرم",
"nutritional_value": "90"
},
{
"food_name": "آلبالو با هسته",
"value": "یک لیوان",
"nutritional_value": "60"
},
{
"food_name": "ماءالشعیر",
"value": "100گرم",
"nutritional_value": "40"
},
{
"food_name": "آلوی زرد",
"value": "100گرم",
"nutritional_value": "70"
},
و مدل هم بصورت زیر هست :
class Calories {
String food_name;
String value;
String nutritional_value;
Calories(this.food_name,this.value,this.nutritional_value);
Calories.fromJson(Map<String, dynamic> json) {
food_name = json['food_name'];
value = json['value'];
nutritional_value = json['nutritional_value'];
}
}
داخل دیباگ لیست پر میشه اما هیچ چیزی نمایش داده نمیشه ممنون میشم راهنمایی کنید که مشکل کجاست؟
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟