трепетать, получать предметы из sqflite и показывать в списке с помощью ontap - PullRequest
0 голосов
/ 31 октября 2018

мои вопросы о флаттере и sqflite.

Я хочу получить все элементы из sqflite и показать их в listview.builder. во второй части я хочу приклеить на элемент списка и перейти на другую страницу с одним параметром, а на новой странице создать новый listview.builder с новыми элементами.

домашняя страница:

body: Center(
      child: ListView.builder(
        itemCount: _categories.length,
        itemBuilder: (_, index) {
          return Directionality(
            textDirection: TextDirection.rtl,
            child: Column(
              children: <Widget>[
                ListTile(
                  title: Text(
                    "${Category.map(_categories[index]).name}",
                    style: TextStyle(
                      fontSize: 25.0,
                      color: Colors.deepOrangeAccent,
                    ),
                  ),
                  onTap: () {
                    Navigator.of(context).push(MaterialPageRoute(
                        builder: (context) => FoodListPage(
                            id: Category.map(_categories[index]).id,
                            catName:
                                Category.map(_categories[index]).name)));
                  },
                ),
                Divider(
                  height: 5.0,
                ),
              ],
            ),
          );
        },
      ),
    ),

вторая страница:

List foods = [];
void fillFoodsList(int id) async {
  var db = new DatabaseHelper();
  foods = await db.getAllFoods(id);
  print('////////////////////////');
  foods.forEach((category) => print(category['ImageName']));
  print(foods.length);
}

class FoodListPage extends StatefulWidget {
  //get 'id' from home page stateLess
  final int id;
  final String catName;
  FoodListPage({this.id, this.catName});
  //end

  @override
  State<StatefulWidget> createState() {
    return new _FoodListPageState();
  }
}

class _FoodListPageState extends State<FoodListPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      fillFoodsList(widget.id); // call and fill foods
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('لیست خوراکی ها' + "${widget.catName}"),
      ),
      // body: Center(child: Text(foods.length.toString()),),
      body: Center(
        child: ListView.builder(
          itemCount: foods.length,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              title: Text(
                "${Food.map(foods[index]).name}"
                    // "vvvvvvvvvvvv"
                    ,
                style: TextStyle(
                  fontSize: 25.0,
                  color: Colors.deepOrangeAccent,
                ),
              ),
              onTap: () {
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => FoodSinglePage(
                          id: Food.map(foods[index]).id,
                          mezaj: Food.map(foods[index]).mezaj,
                          name: Food.map(foods[index]).name,
                          value: Food.map(foods[index]).value,
                          description: Food.map(foods[index]).description,
                          image: Food.map(foods[index]).image,
                          favorite: Food.map(foods[index]).favotite,
                        )));
              },
            );
          },
        ),
      ),
    );
  }
}

с этими кодами я нашел решение, но просмотр списка на второй странице кеширует данные предыдущий запрос!

...