Flutter中使用Center与GridView时出现过多位置参数错误求助
解决Flutter中
extra_positional_arguments_could_be_named错误 错误原因
Scaffold的body属性仅支持传入单个Widget,但你在代码中直接为body连续传入了Center和GridView.count两个Widget,这属于传入了多余的位置参数,触发了Dart的语法检查错误。
修复方案
将多个Widget用一个父容器包裹,比如使用Column,同时为了避免GridView与Column的滚动冲突,需要用Expanded包裹GridView,确保它能合理占据剩余空间。
修改后的完整代码
import 'package:flutter/material.dart'; import 'package:flutter_svg/flutter_svg.dart'; class HomePage extends StatelessWidget { const HomePage({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: appBar(), body: Column( children: [ Center( child: Padding( padding: EdgeInsets.only(top: 10), child: Text( 'CarVariants', style: TextStyle( color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold), )), ), Expanded( child: GridView.count( crossAxisCount: 2, children: List.generate(100, (index) { return Center( child: Text( 'Item $index', style: Theme.of(context).textTheme.headlineSmall, ), ); }), ), ), ], ), ); } AppBar appBar() { return AppBar( title: Text( 'CarBay', style: TextStyle( color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold), ), backgroundColor: Color.fromARGB(255, 79, 218, 125), elevation: 0.0, centerTitle: true, leading: Container( margin: EdgeInsets.all(10), alignment: Alignment.center, child: SvgPicture.asset( 'assets/icons/backButton.svg', height: 200, width: 200, ), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(10)), ), ); } }
关键修改点
- 用
Column作为body的父容器,将Center和GridView.count放入Column的children列表中,满足body仅接收单个Widget的要求 - 添加
Expanded包裹GridView.count,让它自动填充Column的剩余空间,避免布局溢出或滚动冲突问题
内容的提问来源于stack exchange,提问作者Muhammed Sibly B
相关产品推荐
相关产品推荐

