dart 未更新项目计数

zy1mlcev  于 2023-01-10  发布在  其他
关注(0)|答案(2)|浏览(171)

我有问题在这里,我想所有的行显示,但当我运行应用程序时,它只显示一行,但我有6或7行在我的数据库中,我试图运行代码的输出来像这个截图:-


我希望输出如下截图:-

这是我的代码:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class my_ads extends StatefulWidget {
  const my_ads({Key? key}) : super(key: key);

  @override
  State<my_ads> createState() => _my_adsState();
}

List list = [];
int select_item = 0;

class _my_adsState extends State<my_ads> {
  @override
  Future ReadData() async {
    var url = "https://***.***.***.**/getData.php";
    var res = await http.get(Uri.parse(url));

    if (res.statusCode == 200) {
      var red = jsonDecode(res.body);

      setState(() {
        list.addAll(red);
      });

      print(list);
    }
  }

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

    GetData();
  }

  GetData() async {
    await ReadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ListView.builder(
            itemCount: list.length,
            itemBuilder: ((cts, i) {
              return Container(
                  height: 300,
                  child: ListView(
                    children: [
                      Container(
                        margin: EdgeInsets.only(left: 70, right: 60),
                        height: 54.0,
                        width: 224.0,
                        child: Container(
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color: Color(0xffF4AC47), width: 5),
                                color: Color(0xff42A9D2),
                                borderRadius: BorderRadius.only(
                                    bottomLeft: Radius.circular(40),
                                    bottomRight: Radius.circular(40))),
                            child: new Center(
                              child: new Text(
                                "MyAds",
                                style: TextStyle(
                                    fontSize: 25,
                                    color: Color(0xff072A52),
                                    fontFamily: 'Cairo'),
                                textAlign: TextAlign.center,
                              ),
                              //end logo
                            )),
                      ),

                      ///end logo

                      SizedBox(
                        height: 35,
                      ),

                      ///start Section
                      Container(
                        margin: EdgeInsets.only(left: 10, right: 10),
                        height: 180.0,
                        width: 430.0,
                        child: Container(
                            decoration: BoxDecoration(
                                border: Border.all(
                                    color: Color(0xff42A9D2), width: 5),
                                borderRadius: BorderRadius.circular(8)),
                            child: new Container(
                                child: Row(
                              children: [
                                Expanded(
                                    child: Image(
                                  image: AssetImage("assets/book.jpg"),
                                )),
                                Container(
                                  margin: EdgeInsets.only(
                                      left: 110, top: 30, right: 13),
                                  child: Column(
                                    children: [
                                      Text(
                                        "${list[i]["book_name"]}",
                                        style: TextStyle(
                                            fontSize: 20,
                                            color: Colors.black87),
                                      ),
                                      SizedBox(
                                        height: 20,
                                      ),
                                      Row(
                                        children: [
                                          Text("${list[i]["collage"]}"),
                                          Icon(Icons.perm_identity_rounded)
                                        ],
                                      ),
                                      SizedBox(
                                        height: 5,
                                      ),
                                      Row(
                                        children: [
                                          Text("${list[i]["loc"]}"),
                                          Column(
                                            children: [Icon(Icons.store)],
                                          )
                                        ],
                                      ),
                                    ],
                                  ),
                                )
                              ],
                            )
                                //end logo

                                )),
                      ),
                      SizedBox(
                        height: 35,
                      ),
                    ],
                  ));
            })));
  }
}

我试图使标志在另一行(),但我不知道它是如何工作的

t40tm48m

t40tm48m1#

你正在使用listViewlistView而不是column的主要问题。你还使用了一些小部件不正确,这将不会正确的响应移动的应用程序。尝试这个我更新了你的代码。
对于顶部的广告视图,您需要将其放置在ListView之外,这样它将只显示一次。

| Scaffold          |
  | - Column[..       |
  |     AdsView       | 
  |     Expanded      |  
  |      ListView ..] |

请尝试此代码:

Scaffold(
  body: Column(
    children: [
      Container(
        margin: const EdgeInsets.only(left: 70, right: 60),
        height: 54.0,
        width: 224.0,
        child: Container(
            decoration: BoxDecoration(
                border:
                    Border.all(color: const Color(0xffF4AC47), width: 5),
                color: const Color(0xff42A9D2),
                borderRadius: const BorderRadius.only(
                    bottomLeft: Radius.circular(40),
                    bottomRight: Radius.circular(40))),
            child: const Center(
              child: Text(
                "MyAds",
                style: TextStyle(
                    fontSize: 25,
                    color: Color(0xff072A52),
                    fontFamily: 'Cairo'),
                textAlign: TextAlign.center,
              ),
              //end logo
            )),
      ),
      const SizedBox(
        height: 35,
      ),
      Expanded(
        child: ListView.builder(
          itemCount: list.length,
          itemBuilder: ((cts, i) {
            return Column(
              children: [
                Container(
                  margin: const EdgeInsets.symmetric(horizontal: 10),
                  height: 180.0,
                  child: Container(
                    decoration: BoxDecoration(
                        border: Border.all(
                            color: const Color(0xff42A9D2), width: 5),
                        borderRadius: BorderRadius.circular(8)),
                    child: Row(
                      children: [
                        const Expanded(
                          flex: 3,
                          child: Image(
                            image: AssetImage("assets/book.jpg"),
                          ),
                        ),
                        Expanded(
                          flex: 6,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.end,
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              const Text(
                                // "${list[i]["book_name"]}",

                                "book_name",
                                style: TextStyle(
                                    fontSize: 20, color: Colors.black87),
                              ),
                              const SizedBox(height: 12),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  Text("${list[i]["collage"]}"),
                                  const Icon(Icons.perm_identity_rounded)
                                ],
                              ),
                              const SizedBox(height: 12),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  Text("${list[i]["loc"]}"),
                                  const Icon(Icons.store)
                                ],
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                const SizedBox(height: 16),
              ],
            );
          }),
        ),
      ),
    ],
  ),
)
flseospp

flseospp2#

问题是因为你把我的广告放在ListView.builder中,你需要把我的广告从ListView.builder中移除。
正确的方法应该是

-Column
-Your AdsView text
-ListView.builder

相关问题