我想从gridview flutter中的资产获取图像,并想使其可点击,但我遇到此代码错误

olhwl3o2  于 2023-01-02  发布在  Flutter
关注(0)|答案(1)|浏览(99)
I want to take images from asset in gridview flutter and want to make it clickable, but I am getting error with this code
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../home1_page.dart';

class ItemdetailAccelero extends StatefulWidget {
  const ItemdetailAccelero({
    Key? key,
  }) : super(key: key);

  @override
  State<ItemdetailAccelero> createState() => _ItemdetailAcceleroState();
}

class _ItemdetailAcceleroState extends State<ItemdetailAccelero> {
  
  
   List<String> images = [  
    "assets/images/freerun.png",  
    "assets/images/hammer.png",  
    "assets/images/soccer.png",  
    "assets/images/linefollower.png"  
  ];  

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: Color.fromARGB(255, 212, 235, 249),
      appBar: AppBar(
          /* actions: [IconButton(onPressed: () => Get.off(const HomeScreen()), icon: const Icon(Icons.arrow_back_ios))], */ backgroundColor:
              Colors.transparent,
          elevation: 0,
          title: Text('Accelero', style: TextStyle(fontSize: 20))),
      body: SafeArea(
        child: Stack(children: [
          Hero(
            tag: 'Accelero',
            child: Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.width * 0.8,
              decoration: const BoxDecoration(
                color: Color.fromARGB(255, 197, 228, 248),
                image: DecorationImage(
                  image: AssetImage(
                    'assets/images/Accelero.png',
                  ),
                  fit: BoxFit.contain,
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.bottomCenter,
            child: Container(
              height: MediaQuery.of(context).size.height * .6,
              width: MediaQuery.of(context).size.width,
              decoration: BoxDecoration(
                  color: Colors.white.withOpacity(.1),
                  borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(40),
                    topRight: Radius.circular(40),
                  ),
                  boxShadow: [
                    BoxShadow(
                        color: Color.fromARGB(255, 231, 43, 43).withOpacity(.2),
                        offset: Offset(0, -7),
                        blurRadius: 0.2),
                  ]),
              child:  GridView.builder(  
              itemCount: images.length,  
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(  
                  crossAxisCount: 2,  
                  crossAxisSpacing: 4.0,  
                  mainAxisSpacing: 4.0  
              ),  
              itemBuilder: (BuildContext context, int index){  
                return Image.asset(images[index]);  
              },  
            ),
            ),
          )
        ]),
      ),
    );
  }
}

图像资源服务捕获异常无法加载资产:assets/images/soccer.png.足球俱乐部/足球俱乐部/足球俱乐部
图像资源服务捕获异常无法加载资产:assets/images/linefollower.png.网络资源/图片资源/linefollower.png.
微件库捕获到异常。ParentDataWidget的使用不正确。

siv3szwd

siv3szwd1#

  • 首先检查pubspace.yml文件是否添加了此路径,也没有拼写错误,您已写入文件夹名称和图像名称资产:
  • 资产/图像/
  • flutter cleanflutter pub get命令之后
  • 然后停止应用程序运行模式并再次运行应用程序

这肯定管用

相关问题