串列沒有足夠的空間,似乎我以前這樣做過,一切正常,但現在不行。我最近開始在 Flutter 上進行開發,所以我可能遺漏了一些東西,如果你能幫助解決這個錯誤,我將不勝感激。
主螢屏
class HomePage extends StatelessWidget {
final todoRepository = TodoRepository();
@override
Widget build(BuildContext context) {
// final TodoBloc todoBloc = context.read<TodoBloc>();
return BlocProvider<TodoBloc>(
create: (context) => TodoBloc(todoRepository),
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter Todos'),
),
// floatingActionButton: FloatingActionButton(
// onPressed: () {
// // _addTodo(context);
// final newTodo = Todo(description: 'Todo 1');
// BlocProvider.of<TodoBloc>(context).add(CreateTodos(newTodo));
// },
// child: const Icon(Icons.add),
// ),
body: Column(
// crossAxisAlignment: CrossAxisAlignment.center,
children: [
ActionButton(),
TodoList(),
],
)),
);
}
list_screen
class TodoList extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Todo todo;
return BlocBuilder<TodoBloc, TodoState>(builder: (context, state) {
if (state is TodoEmptyState) {
return const Center(
child: Text(
'No Todo',
style: TextStyle(fontSize: 20.0),
),
);
}
if (state is TodoLoadingState) {
return const Center(child: CircularProgressIndicator());
}
// final todos = (state is TodoLoadedState);
// return Container();
if (state is TodoLoadedState) {
return ListView.builder(
shrinkWrap: true,
itemCount: state.loadedUser.length,
itemBuilder: (context, index) =>
// return _buildListTile(state.loadedUser[index]);
Expanded(
child: Container(
child: ListTile(
title: Column(children: [
Text('${state.loadedUser[index].description}')
])))));
}
return const SizedBox.shrink();
});
}

uj5u.com熱心網友回復:
嘗試像這樣更改您的主頁小部件代碼:
class HomePage extends StatelessWidget {
final todoRepository = TodoRepository();
@override
Widget build(BuildContext context) {
// final TodoBloc todoBloc = context.read<TodoBloc>();
return BlocProvider<TodoBloc>(
create: (context) => TodoBloc(todoRepository),
child: Scaffold(
appBar: AppBar(
title: const Text('Flutter Todos'),
),
// floatingActionButton: FloatingActionButton(
// onPressed: () {
// // _addTodo(context);
// final newTodo = Todo(description: 'Todo 1');
// BlocProvider.of<TodoBloc>(context).add(CreateTodos(newTodo));
// },
// child: const Icon(Icons.add),
// ),
body: SingleChildScrollView(
child: Column(
// crossAxisAlignment: CrossAxisAlignment.center,
children: [
ActionButton(),
TodoList(),
],
),
)),
);
}
}
并添加這一行
physics: const NeverScrollableScrollPhysics(),
到您的ListView.builder()小部件中的TodoList小部件
說明:當你的螢屏內容超過它的高度時,你需要用一個可滾動的小部件(如SingleChildScrollView)包裹它,以允許滾動訪問剩余的內容。但是,如果您的父可滾動小部件中有另一個可滾動小部件(您ListView在您的TodoList小部件中,那么滾動將會發生沖突。這就是為什么您可以添加NeverScrollableScrollPhysics()到子可滾動以允許僅滾動父可滾動。
更新:
對于Exception caught by widgets library Incorrect use of ParentDataWidget發生的錯誤,因為您有一個小部件作為小部件Expanded中小部件的直接子級。所以洗掉小部件將修復錯誤ListViewTodoListExpanded

return ListView.builder(
shrinkWrap: true,
itemCount: state.loadedUser.length,
itemBuilder: (context, index) => Container(
child: ListTile(
title: Column(
children: [
Text('${state.loadedUser[index].description}'),
],
),
),
),
);
注意:我保留了您的Container小部件,以防您出于向 ListTile 添加更多樣式的原因放置它,但如果您不打算對它做任何事情,我建議您將其洗掉。
uj5u.com熱心網友回復:
您可以嘗試 SingleChildScrollView 小部件來克服溢位問題。
和
用 Column 或 Row Widget 包裹 Expanded 小部件。
喜歡-
Column(
children: [
Expanded(child: Text("")),
],
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409981.html
標籤:
