dart Center()和GridVew()有太多的位置参数沿着?

mzsu5hc0  于 2023-10-13  发布在  其他
关注(0)|答案(2)|浏览(90)

我是新来的。当我尝试构建应用程序时,错误显示为

[{  
    "resource": "/d:/Learn Fluter    
 Self/Flutter_Projects/Demo_App/flutter_application_1/lib/home.dart",   
    "owner": "_generated_diagnostic_collection_name_#1",  
    "code": {  
        "value": "extra_positional_arguments_could_be_named",  
        "target": {  
            "$mid": 1,  
            "path": "/diagnostics/extra_positional_arguments_could_be_named",  
            "scheme": "https",  
            "authority": "dart.dev"  
        }    
    },    
    "severity": 8,  
    "message": **"Too many positional arguments: 0 expected, but 1 found.\nTry removing the extra positional arguments, or specifying the name for named arguments."**,  
    "source": "dart",  
    "startLineNumber": 25,  
    "startColumn": 7,  
    "endLineNumber": 38,  
    "endColumn": 8  
}]
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),
            )),
      ),
      GridView.count(
        // Create a grid with 2 columns. If you change the scrollDirection to
        // horizontal, this produces 2 rows.
        crossAxisCount: 2,
        // Generate 100 widgets that display their index in the List.
        children: List.generate(100, (index) {
          return Center(
            child: Text(
              'Item $index',
              style: Theme.of(context).textTheme.headlineSmall,
            ),
          );
        }),
      ),
      // Container(
      //   child: Image.asset('assets/images/sedan.png'),
      // ),
      // Container(
      //   child: Image.asset('assets/images/suv.png'),
      // ),
      //   ],
      // ),
    );
  }

  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)),
      ),
    );
  }
}
nhn9ugyo

nhn9ugyo1#

问题出在你的架子和身体上。body参数只接受一个子对象,body有一个Center和一个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(
              // Create a grid with 2 columns. If you change the scrollDirection to
              // horizontal, this produces 2 rows.
              crossAxisCount: 2,
              // Generate 100 widgets that display their index in the List.
              children: List.generate(100, (index) {
                return Center(
                  child: Text(
                    'Item $index',
                    style: Theme.of(context).textTheme.headline6,
                  ),
                );
              }),
            ),
          ),
        ],
      ),
    );
  }

  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),
        ),
      ),
    );
  }
}
6pp0gazn

6pp0gazn2#

为什么要评论body:Column及其子[]??还要确保使用SizedBox Package GridView.count,并给予sizedBox一个高度

return Scaffold(
        body: Column(children: [
      const Center(
        child: Padding(
            padding: EdgeInsets.only(top: 10),
            child: Text(
              'CarVariants',
              style: TextStyle(
                  color: Colors.black,
                  fontSize: 18,
                  fontWeight: FontWeight.bold),
            )),
      ),
      SizedBox(
        height: 400,
        child: GridView.count(
          // Create a grid with 2 columns. If you change the scrollDirection to
          // horizontal, this produces 2 rows.
          crossAxisCount: 2,
          // Generate 100 widgets that display their index in the List.
          children: List.generate(item.length, (index) {
            return Text(
              'Item ${item[index]}',
              style: Theme.of(context).textTheme.headlineSmall,
            );
          }),
        ),
      ),
    ]));

相关问题