[英]ListView.builder inside a Column inside a SingleChildScrollView
all.全部。 I am trying to add a
ListView.builder
to a Column
that is inside a SingleChildScrollView
.我正在尝试将
ListView.builder
添加到SingleChildScrollView
内的Column
中。 However, I am getting an exception, likely due to the fact that there is no constraint for the ListView.builder
.但是,我遇到了一个异常,可能是因为
ListView.builder
没有约束。 Here is my code:这是我的代码:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListView.builder(
itemBuilder: (context, index) => const Text('a'),
itemCount: 2,
),
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
If I use a Container
and set a defined height, the code above works.如果我使用
Container
并设置定义的高度,上面的代码就可以工作。 However, I am trying to get the ListView.builder
to not have a fixed size.但是,我试图让
ListView.builder
没有固定大小。 I've tried using the Expanded
widget and I still get this error.我试过使用
Expanded
小部件,但仍然出现此错误。 Is there a way to make this work without a defined height?有没有办法在没有定义高度的情况下进行这项工作? Thanks
谢谢
In the column widget add mainAxisSize:MainAxisSize.min
and in List view.builder add shrinkWrap:true
and physics:NeverScrollablePhysics()
.在列小部件中添加
mainAxisSize:MainAxisSize.min
并在 List view.builder 中添加shrinkWrap:true
和physics:NeverScrollablePhysics()
。 That should solve the issue and instead of center widget use SafeArea or a container with specific height.这应该可以解决问题,而不是中心小部件使用 SafeArea 或具有特定高度的容器。
Here's your working code这是你的工作代码
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ListView.builder(
shrinkWrap:true,// -> Add this here
physics:NeverScrollablePhysics(),// -> And this one
itemBuilder: (context, index) => const Text('a'),
itemCount: 2,
),
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.