【问题标题】:The operator '[]' isn't defined for the type 'Type'. Try defining the operator '[]'未为类型“Type”定义运算符“[]”。尝试定义运算符'[]'
【发布时间】:2020-10-10 07:06:57
【问题描述】:

我有一个主屏幕,在顶部我声明了一个列表视图,滚动方向从 list.dart 文件中获取信息。这个水平滚动屏幕为我带来了 5 张图片和每张图片中的文字。我想根据此列表中传递的信息插入一个指向其他屏幕的 onpress。示例:聊天,直接到 chat.screen。

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
  body: Stack(
    children: <Widget>[
      Container(
        width: double.infinity,
        height: MediaQuery.of(context).size.height * 4 / 7,
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [Color(0xff40dedf), Color(0xff0fb2ea)],
          ),
        ),
      ),

      Positioned(
        top: 100,
        left: 20,
        child: Container(
          height: 100,
          width: MediaQuery.of(context).size.width,
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemCount: categoryData.length,
            itemBuilder: (context, index) {
              bool isSelected = true;
              if (index == 0) {
                isSelected = true;
              }
              Navigator.push<dynamic>(
                                  context,

  MaterialPageRoute<dynamic>(
                                    builder: (BuildContext 
  context) =>

  HomeList[index].navigateScreen,
                                  ),
                                );
              return Row(
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      Container(
                        width: 65,
                        height: 65,
                        decoration: BoxDecoration(
                            color: isSelected
                                ? Colors.transparent
                                : Colors.transparent,
                            borderRadius: 
    BorderRadius.circular(16),
                            border: Border.all(
                              color: Colors.white,
                              width: 1,
                            ),
                            boxShadow: isSelected
                                ? [
                              BoxShadow(
                                  color: Color(0x14000000),
                                  blurRadius: 10)
                            ]
                                : null),
                        child: Center(
                          child: Image.asset(categoryData[index].imageUrl),
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      Text(
                        categoryData[index].name,
                        style: TextStyle(color: Colors.white, fontSize: 15),
                      ),

                    ],

                  ),

                  SizedBox(
                    width: 20,
                  )
                ],
              );
            },
          ),
        ),
      ),

家庭清单

 import 'package:flutter/material.dart';
 import 'package:projeto/pages/chat.screen.dart';





   class HomeList {
   HomeList({
   this.navigateScreen,
   this.imagePath = '',
  });

 Widget navigateScreen;
 String imagePath;

 static List<HomeList> homeList = [
  HomeList(
   imagePath: 'assets/page1/usuario.png',
   navigateScreen: ChatScreen(),
  ),
  HomeList(
   imagePath: 'assets/page1/entregas.png',
   navigateScreen: ChatScreen(),
  ),
  HomeList(
   imagePath: 'assets/page1/msg.png',
   navigateScreen: ChatScreen(),
  ),
  HomeList(
   imagePath: 'assets/page1/configurações.png',
   navigateScreen: ChatScreen(),
  ),
  HomeList(
   imagePath: 'assets/page1/sair.png',
   navigateScreen: ChatScreen(),
  ),
  ];
  }

【问题讨论】:

    标签: flutter flutter-layout flutter-dependencies flutter-animation flutter-test


    【解决方案1】:

    据我了解,您希望将 HomeList 文件中的数据转换为列表视图,单击其中一个项目会将您带到其相关页面,您可以使用带有 itemBuilder 和 itemCount 的 ListView.builder,代码下面展示了如何实现 listView,其中项目是带有文本的图像和 onTap 函数:

    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Container(
          child: ListView.builder(
            scrollDirection: Axis.horizontal,
            itemBuilder: (context, index) {
              return GestureDetector(
                child: Stack(
                  children: <Widget>[
                    Image.asset(
                      homeList[index].imagePath,
                    ),
                    Positioned(child: Text())
                  ],
                ),
                onTap: () => Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (context) => homeList[index].navigateScreen),
                ),
              );
            },
            itemCount: homeList.length,
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 2021-04-03
      • 1970-01-01
      • 2021-08-08
      相关资源
      最近更新 更多