英文:
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
英文:
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
答案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,
),
)
,))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论