(我稍微修改了內容,ExpansionTile => GridView)
我正在創建一個包含許多 GridView 小部件的 ListView。這樣做的問題是 ListView 具有垂直滾動,但它不適用于 ListView 中的 GridView Widget。ListView 中的滾動僅適用于空白空間,不適用于子視窗小部件的區域。
Container(
height: size!.height * 0.6,
padding: EdgeInsets.all(common_padding),
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
width: 0.7,
),
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
child: ListView(
children: [
// GridView.builder() .... // many widgets
]
),
)
我的代碼簡要如上。如何將滾動應用到串列視圖中的子小部件?
uj5u.com熱心網友回復:
使用shrinkWrap: true和physics: const NeverScrollableScrollPhysics(),為 ListView.builder
試試這個例子:
import 'package:flutter/material.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: 30,
shrinkWrap: true,
itemBuilder: (BuildContext context, findex) {
return ExpansionTile(
key: Key(findex.toString()),
title: const Text("title",
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold,color: Colors.black),
),
children: [
ListView.builder(
itemCount: 10,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, sindex) {
return const ListTile(
title: Text(
"user tierl",
style: TextStyle(fontSize: 15.0, fontWeight: FontWeight.bold,color: Colors.black),
),
);
},
),
],
);
},
),
);
}
}
uj5u.com熱心網友回復:
我不明白擴展小部件是什么意思,但是您應該將串列中的每個專案逐一堆疊,而不是將所有專案打包在一個小部件中。例如,根據您的代碼,
MaterialApp(
home: Scaffold(
body: Container(
height: double.infinity,
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
width: 0.7,
),
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
child: ListView(children: [
//putting each item one by one
Container(
height: 500,
color: Colors.blue,
),
Container(
height: 500,
color: Colors.green,
),
Container(
height: 500,
color: Colors.red,
),
Container(
height: 500,
color: Colors.yellow,
),
]),
),
),
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/480806.html
上一篇:以編程方式呈現VC
