繁体   English   中英

Flutter、Dart - 如何使用另一个文件中的类属性

[英]Flutter, Dart - How to use a class property from another file

所以我有 3 个单独的文件,其中 1 个称为driver_picture_carousel.dart用于构建列表视图小部件。 另一个名为driver_picture_carousel_model.dart文件包含我想要在列表中显示的所有图片。 另一个名为driver_model.dart文件用于保存有关驱动程序的所有其他信息。

driver_picture_carousel_model.dart

class PictureCarousel {
  String maxVerstappen;
  String lewisHamilton;
  String sergioPerez;
  String landoNorris;
  String valterriBottas;
  String charlesLeclerc;
  String carlosSainz;
  String danielRicciardo;
  String pierreGasly;
  String sebastianVettel;
  String fernandoAlonso;
  String lanceStroll;
  String estebanOcon;
  String yukiTsunoda;
  String kimiRaikkonen;
  String antonioGiovinazzi;
  String georgeRussel;
  String mickSchumacher;
  String nikitaMazepin;
  String nicholasLatifi;

  PictureCarousel({
    required this.maxVerstappen,
    required this.lewisHamilton,
    required this.sergioPerez,
    required this.landoNorris,
    required this.valterriBottas,
    required this.charlesLeclerc,
    required this.carlosSainz,
    required this.danielRicciardo,
    required this.pierreGasly,
    required this.sebastianVettel,
    required this.fernandoAlonso,
    required this.lanceStroll,
    required this.estebanOcon,
    required this.yukiTsunoda,
    required this.kimiRaikkonen,
    required this.antonioGiovinazzi,
    required this.georgeRussel,
    required this.mickSchumacher,
    required this.nikitaMazepin,
    required this.nicholasLatifi,
  });
}

List<PictureCarousel> pictureCarousels = [
  PictureCarousel(
    maxVerstappen: 'assets/images/MaxVerstappen1.jpg',
    lewisHamilton: 'assets/images/LewisHamilton1.jpg',
    sergioPerez: 'assets/images/SergioPerez1.jpg',
    landoNorris: 'assets/images/LandoNorris1.jpg',
    valterriBottas: 'assets/images/ValtteriBottas1.jpg',
    charlesLeclerc: 'assets/images/CharlesLeclerc1.jpg',
    carlosSainz: 'assets/images/CarlosSainz1.jpg',
    danielRicciardo: 'assets/images/DanielRicciardo1.jpg',
    pierreGasly: 'assets/images/PierreGasly1.jpg',
    sebastianVettel: 'assets/images/SebastianVettel1.jpg',
    fernandoAlonso: 'assets/images/FernandoAlonso1.jpg',
    lanceStroll: 'assets/images/LanceStroll1.jpg',
    estebanOcon: 'assets/images/EstebanOcon1.jpg',
    yukiTsunoda: 'assets/images/YukiTsunoda1.jpg',
    kimiRaikkonen: 'assets/images/KimiRaikkonen1.jpg',
    antonioGiovinazzi: 'assets/images/AntonioGiovinazzi1.jpg',
    georgeRussel: 'assets/images/GeorgeRussel1.jpg',
    mickSchumacher: 'assets/images/MickSchumacher1.jpg',
    nikitaMazepin: 'assets/images/NikitaMazepin1.jpg',
    nicholasLatifi: 'assets/images/NicholasLatiffi1.jpg',
  ),
  PictureCarousel(
    maxVerstappen: 'assets/images/MaxVerstappen2.jpg',
    lewisHamilton: 'assets/images/LewisHamilton2.jpg',
    sergioPerez: 'assets/images/SergioPerez2.jpg',
    landoNorris: 'assets/images/LandoNorris2.jpg',
    valterriBottas: 'assets/images/ValtteriBottas2.jpg',
    charlesLeclerc: 'assets/images/CharlesLeclerc2.jpg',
    carlosSainz: 'assets/images/CarlosSainz2.jpg',
    danielRicciardo: 'assets/images/DanielRicciardo2.jpg',
    pierreGasly: 'assets/images/PierreGasly2.jpg',
    sebastianVettel: 'assets/images/SebastianVettel2.jpg',
    fernandoAlonso: 'assets/images/FernandoAlonso2.jpg',
    lanceStroll: 'assets/images/LanceStroll2.jpg',
    estebanOcon: 'assets/images/EstebanOcon2.jpg',
    yukiTsunoda: 'assets/images/YukiTsunoda2.jpg',
    kimiRaikkonen: 'assets/images/KimiRaikkonen2.jpg',
    antonioGiovinazzi: 'assets/images/AntonioGiovinazzi2.jpg',
    georgeRussel: 'assets/images/GeorgeRussel2.jpg',
    mickSchumacher: 'assets/images/MickSchumacher2.jpg',
    nikitaMazepin: 'assets/images/NikitaMazepin2.jpg',
    nicholasLatifi: 'assets/images/NicholasLatiffi2.jpg',
  ),
  PictureCarousel(
    maxVerstappen: 'assets/images/MaxVerstappen3.jpg',
    lewisHamilton: 'assets/images/LewisHamilton3.jpg',
    sergioPerez: 'assets/images/SergioPerez3.jpg',
    landoNorris: 'assets/images/LandoNorris3.jpg',
    valterriBottas: 'assets/images/ValtteriBottas3.jpg',
    charlesLeclerc: 'assets/images/CharlesLeclerc3.jpg',
    carlosSainz: 'assets/images/CarlosSainz3.jpg',
    danielRicciardo: 'assets/images/DanielRicciardo3.jpg',
    pierreGasly: 'assets/images/PierreGasly3.jpg',
    sebastianVettel: 'assets/images/SebastianVettel3.jpg',
    fernandoAlonso: 'assets/images/FernandoAlonso3.jpg',
    lanceStroll: 'assets/images/LanceStroll3.jpg',
    estebanOcon: 'assets/images/EstebanOcon3.jpg',
    yukiTsunoda: 'assets/images/YukiTsunoda3.jpg',
    kimiRaikkonen: 'assets/images/KimiRaikkonen3.jpg',
    antonioGiovinazzi: 'assets/images/AntonioGiovinazzi3.jpg',
    georgeRussel: 'assets/images/GeorgeRussel3.jpg',
    mickSchumacher: 'assets/images/MickSchumacher3.jpg',
    nikitaMazepin: 'assets/images/NikitaMazepin3.jpg',
    nicholasLatifi: 'assets/images/NicholasLatiffi3.jpg',
  ),
],

driver_model.dart

import 'package:flutter/material.dart';
import 'dart:ui';
import 'package:formula1_app/models/driver_picture_carousel_model.dart';

class Driver {
  String name;
  String rank;
  String team;
  String points;
  Color? color;
  String number;
  String firstName;
  String lastName;
  String wins;
  String podiums;
  String fastestLap;
  String gpEntered;
  String worldchampionships;
  String driverImageUrl;
  String teamImageUrl;
  String teamCarUrl;
  String biography;
  String carousel;

  Driver({
    required this.name,
    required this.rank,
    required this.team,
    required this.points,
    required this.color,
    required this.number,
    required this.firstName,
    required this.lastName,
    required this.wins,
    required this.podiums,
    required this.fastestLap,
    required this.gpEntered,
    required this.worldchampionships,
    required this.driverImageUrl,
    required this.teamImageUrl,
    required this.teamCarUrl,
    required this.biography,
    required this.carousel,
  });
}


List <Driver> drivers = [
  Driver(
    name: 'Max Verstappen',
    rank: '1',
    team: 'Red Bull Racing',
    points: '182',
    color: Colors.blue[800],
    number: '33',
    firstName: 'Max ',
    lastName: 'Verstappen',
    wins: '15',
    podiums: '50',
    fastestLap: '14',
    gpEntered: '128',
    worldchampionships: '0',
    driverImageUrl: 'assets/images/MaxVerstappen_img.png',
    teamImageUrl: 'assets/images/RedBull_img.png',
    teamCarUrl: 'assets/images/RedBull_car.png',
    carousel: pictureCarousels.maxVerstappen,
    biography: 
    "He’s Max by name, and max by nature. \n\n"
    "Arriving as Formula 1’s youngest ever competitor at just 17 years old, Verstappen pushed his car, his rivals and the sport’s record books to the limit. The baby-faced Dutchman with the heart of a lion took the Toro Rosso – and then the Red Bull – by the horns with his instinctive racing style. \n\n"
    "F1’s youngest points scorer soon became its youngest race winner – at the age of 18 years and 228 days – with an opportunistic but controlled drive on debut for Red Bull in Barcelona 2016. A true wheel-to-wheel racer, another stunning drive in Brazil from the back of the pack to the podium on a treacherous wet track kept the plaudits coming.\n\n"
    "Verstappen’s no-holds-barred attitude and hard defending have sometimes landed him in hot water with his peers and paymasters. But the mistakes that initially marred his potential have given way to maturity, while the bravado and energy that make him a blockbuster talent have remained – and the victories have kept on coming.\n\n"
    "The son of former F1 driver Jos Verstappen and super-quick karting Mum Sophie Kumpen, racing runs through his genes. Despite moving out of Dad’s house to live in Monaco, Verstappen remains close to his family, and though he’s not afraid to speak his mind, he can still be surprisingly shy. \n\n"
    "The expectations for the next generation’s leading light are sky high – but with Verstappen there’s a feeling that the sky’s the limit."
  ),
  Driver(
    name: 'Lewis Hamilton',
    rank: '2',
    team: 'Mercedes',
    points: '150',
    color: Colors.tealAccent,
    number: '44',
    firstName: 'Lewis ',
    lastName: 'Hamilton',
    wins: '98',
    podiums: '171',
    fastestLap: '56',
    gpEntered: '275',
    worldchampionships: '7',
    driverImageUrl: 'assets/images/LewisHamilton_img.png',
    teamImageUrl: 'assets/images/Mercedes_img.png',
    teamCarUrl: 'assets/images/Mercedes_car.png',
    carousel: pictureCarousels.lewisHamilton,
    biography:
    "‘Still I Rise’ – these are the words emblazoned across the back of Lewis Hamilton’s helmet and tattooed across his shoulders, and ever since annihilating expectations with one of the greatest rookie performances in F1 history in 2007, that’s literally all he’s done: risen to the top of the all-time pole positions list ahead of his hero Ayrton Senna, surged into first place in the wins column surpassing the inimitable Michael Schumacher, and then matched the legendary German’s seven world titles.\n\n"
    "Is he the G.O.A.T? Few would deny that he’s in the conversation – and what’s more he’s got there his way, twinning his relentless speed with a refusal to conform to stereotypes for how a racing driver should think, dress or behave.\n\n"
    "Respect is hard earned in F1, but Hamilton – now Sir Lewis Hamilton to be precise – has it from every one of his peers. Why? Because they know that whatever the track, whatever the conditions, whatever the situation, when his visor goes down and the lights go out, it’s Hammertime."
  ),
  Driver(
    name: 'Sergio Perez',
    rank: '3',
    team: 'Red Bull Racing',
    points: '104',
    color: Colors.blue[800],
    number: '11',
    firstName: 'Sergio ',
    lastName: 'Perez',
    podiums: '12',
    wins: '2',
    fastestLap: '4',
    gpEntered: '202',
    worldchampionships: '0',
    driverImageUrl: 'assets/images/SergioPerez_img.png',
    teamImageUrl: 'formula1_app/assets/images/RedBull_img.png',
    teamCarUrl: 'assets/images/RedBull_car.png',
    carousel: pictureCarousels.sergioPerez,
    biography: 
    "He’s the fighter with a gentle touch from the land of the Lucha Libre.\n\n"
    "Perez’s reputation in F1 has been built on opposite approaches to Grand Prix racing. On the one hand, he is a punchy combatant who wrestles his way through the pack and into the points. Never afraid to add a bit of spice to his on-track encounters, even his team mates don’t always escape the Mexican’s heat.\n\n"
    "Then on the other hand, Perez is a smooth operator, a master at managing tyres to eke out extra performance and give him the upper hand on strategy. A firm favourite on the grid after his time with Sauber, McLaren, Force India and Racing Point, Perez has matured into an analytical racer and team leader.\n\n"
    "A proud countryman, the Guadalajara gunslinger has amassed more points than any other Mexican in the history of F1. In Sakhir 2020 he also matched hero and compatriot Pedro Rodriguez by taking the chequered flag in first – a performance that landed him a shot at the title with Red Bull.\n\n"
    "More victories may not be assured, especially with Max Verstappen as team mate. But Perez working hard and racing with his heart are.\n\n"
  ),
]

在轮播中:picturecarousels.____ 我收到一个错误The getter 'lewisHamilton' isn't defined for the type 'List<PictureCarousel>'. Try importing the library that defines 'lewisHamilton', correcting the name to the name of an existing getter, or defining a getter or field named 'lewisHamilton'. The getter 'lewisHamilton' isn't defined for the type 'List<PictureCarousel>'. Try importing the library that defines 'lewisHamilton', correcting the name to the name of an existing getter, or defining a getter or field named 'lewisHamilton'.

driver_picture_carousel.dart

import 'package:flutter/material.dart';
import 'package:formula1_app/models/driver_model.dart';
import 'package:formula1_app/models/driver_picture_carousel_model.dart';

class DriverPictureCarousel extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 230,
      color: Colors.white,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: pictureCarousels.length,
        itemBuilder: (BuildContext context, int index) {
          PictureCarousel pictureCarousel = pictureCarousels[index];
          return Container(
            margin: EdgeInsets.fromLTRB(8, 8, 0, 8),
            width: 300,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(20)
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(20),
              child: Image(
                image: AssetImage(drivers.carousel),
                fit: BoxFit.cover,
              ),
            ),
          );
        }
      ),
    );
  }
}

image: AssetImage(drivers.carousel),我收到一条错误消息,指出The getter 'carousel' isn't defined for the type 'List<Driver>'. Try importing the library that defines 'carousel', correcting the name to the name of an existing getter, or defining a getter or field named 'carousel'. The getter 'carousel' isn't defined for the type 'List<Driver>'. Try importing the library that defines 'carousel', correcting the name to the name of an existing getter, or defining a getter or field named 'carousel'.

您需要先创建该类的对象,然后才能在列表视图中使用它才能使用它。 使用对象变量作为列表和属性的长度。

这应该在您的无状态小部件中。

List<PictureCarousel> pictureCarousels = [
  PictureCarousel(
    maxVerstappen: 'assets/images/MaxVerstappen1.jpg',
    lewisHamilton: 'assets/images/LewisHamilton1.jpg',
    sergioPerez: 'assets/images/SergioPerez1.jpg',
    landoNorris: 'assets/images/LandoNorris1.jpg',
    valterriBottas: 'assets/images/ValtteriBottas1.jpg',
    charlesLeclerc: 'assets/images/CharlesLeclerc1.jpg',
   // rest of the data
],

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM