改变Flutter ListView中的布局。

huangapple go评论56阅读模式
英文:

Change the layout in flutter listview

问题

I recently started building my first flutter application ever and I currently don't like the design I have and want to change it so that the picture displays on the left side of the container and the text right next to it. This will make each container smaller and easier to navigate. But I am stuck on how to do it. I first started off with a gridview and it was displaying correctly but I decided to go with a listview instead which would look better for the purpose of my app.

child: ListView.builder(
  itemCount: pdfData.length,
  itemBuilder: (context, index) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: InkWell(
        onTap: () {
          Navigator.of(context).push(MaterialPageRoute(builder: (context) => PdfViewerScreen(pdfUrl: pdfData[index]['url'])),);
        },
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Image.asset(
                "lib/images/woolworths.png",
                height: 120,
                width: 100,
              ),
              SizedBox(width: 10), // Add some space between image and text
              Text(
                pdfData[index]['name'],
                style: TextStyle(
                  fontSize: 18,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  },
)

PS. The text and file are filled from my firebase storage. Which works 改变Flutter ListView中的布局。

英文:

I recently started building my first flutter application ever and I currently don't like the design I have and want to change it so that the picture displays on the left side of the container and the text right next to it. This will make each container smaller and easier to navigate. But I am stuck on how to do it. I first started of with a gridview and it was displaying correctly but I decided to go with a listview instead which would look better for the purpose of my app.

child: ListView.builder( //gridview.builder
          itemCount: pdfData.length,
          //gridDelegate: 
          //SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
          itemBuilder: (context, index) {
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: InkWell(
                onTap: () {
                  Navigator.of(context).push(MaterialPageRoute(builder: (context) => PdfViewerScreen(pdfUrl: pdfData[index]['url'])),);
                },
                child: Container(
                  decoration: BoxDecoration(
                    border: Border.all(),
                  ),
                  
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      Image.asset(
                        "lib/images/woolworths.png",
                        height: 120,
                        width: 100,
                      ),
                      Text(
                        pdfData[index]['name'],
                        style: TextStyle(
                          fontSize: 18,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          },

PS. The text and file are filled from my firebase storage. Which works 改变Flutter ListView中的布局。

改变Flutter ListView中的布局。

答案1

得分: 0

Use Widget ListTile here.

ListTile(
  leading: Image.asset(
    "lib/images/woolworths.png",
    height: 120,
    width: 100,
  ),
  title: Text(
    pdfData[index]['name'],
    style: TextStyle(
      fontSize: 18,
    ),
  ),
)

Entire Code would look like this:

child: ListView.builder(
  itemCount: pdfData.length,
  itemBuilder: (context, index) {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: InkWell(
        onTap: () {
          Navigator.of(context).push(MaterialPageRoute(builder: (context) => PdfViewerScreen(pdfUrl: pdfData[index]['url'])),);
        },
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(),
          ),
          child: ListTile(
            leading: Image.asset(
              "lib/images/woolworths.png",
              height: 120,
              width: 100,
            ),
            title: Text(
              pdfData[index]['name'],
              style: TextStyle(
                fontSize: 18,
              ),
            ),
          ),
        ),
      ),
    );
  },
)

You can also wrap it in Card widget to Distinguish the tiles from each other.

Card(
  child: ListTile(
    leading: Image.asset(
      "lib/images/woolworths.png",
      height: 120,
      width: 100,
    ),
    title: Text(
      pdfData[index]['name'],
      style: TextStyle(
        fontSize: 18,
      ),
    ),
  ),
)
英文:

Use Widget ListTile here.

ListTile(
        leading: Image.asset(
                    "lib/images/woolworths.png",
                    height: 120,
                    width: 100,
                  ),
        title: Text(
                    pdfData[index]['name'],
                    style: TextStyle(
                      fontSize: 18,
                    ),
      ),)

Entire Code would look like this:

child: ListView.builder( //gridview.builder
      itemCount: pdfData.length,
      //gridDelegate: 
      //SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
      itemBuilder: (context, index) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: InkWell(
            onTap: () {
              Navigator.of(context).push(MaterialPageRoute(builder: (context) => PdfViewerScreen(pdfUrl: pdfData[index]['url'])),);
            },
            child: Container(
              decoration: BoxDecoration(
                border: Border.all(),
              ),
              
              child: ListTile(
       		 leading: Image.asset(
                    "lib/images/woolworths.png",
                    height: 120,
                    width: 100,
                  ),
      		  title: Text(
                    pdfData[index]['name'],
                    style: TextStyle(
                      fontSize: 18,
               ),
     	 ),)
            ),
          ),
        );
      },)

You can also wrap it in Card widget to Distinguish the tiles from each other.

Card( child: ListTile(
        leading: Image.asset(
                    "lib/images/woolworths.png",
                    height: 120,
                    width: 100,
                  ),
        title: Text(
                    pdfData[index]['name'],
                    style: TextStyle(
                      fontSize: 18,
                    ),
                   )
                 ,))

huangapple
  • 本文由 发表于 2023年5月6日 23:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定