Saya sedang mengerjakan proyek yang akan online dan saya ingin memanggil elemen 'merek' dari daftar ke AppBar tetapi saya tidak bisa melakukannya, itu berjalan nol. Saya menggunakan ListView.builder sebagai Daftar dan ketika saya mengklik elemen daftar, itu akan membuka halaman baru, saya ingin memanggil elemen 'merek' yang saya klik dari kelas lain ke AppBar. Kode di bawah ini di sini, tolong bantu saya:

final HAVUZ_DATA = [
  HavuzData(
    id: 'User1',
    imageUrl: 'assets/images/arac1.jpeg',
    brand: 'Volkswagen',
    model: 'MULTIVAN 2.5 TDI COMFORTLINE',
    licensePlate: '34AAA3434',
    year: '2011',
    city: 'İstanbul',
    district: 'Bahçelievler',
    authorities: '20040601419',
    status: 'Ekspertiz yapıldı, Dosya sorumlusuna gönderildi.',
  ),
  HavuzData(
    id: 'User2',
    imageUrl: 'assets/images/arac2.jpg',
    brand: 'BMW',
    model: '420',
    licensePlate: '34ABC3434',
    year: '2018',
    city: 'İstanbul',
    district: 'Bakırköy',
    authorities: '20040601419',
    status: 'Ekspertiz yapıldı, Dosya sorumlusuna gönderildi.',
  )
];

class HavuzData {
  final String id;
  final String imageUrl;
  final String brand;
  final String model;
  final String licensePlate;
  final String year;
  final String city;
  final String district;
  final String authorities;
  final String status;

  const HavuzData({
    this.id,
    this.imageUrl,
    this.brand,
    this.model,
    this.licensePlate,
    this.year,
    this.city,
    this.district,
    this.authorities,
    this.status,
  });
}

Dan saya ingin menelepon dari kelas ini:

class HavuzDetay extends StatefulWidget {
  @override
  _HavuzDetayState createState() => _HavuzDetayState();
}

class _HavuzDetayState extends State<HavuzDetay> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Detay ' + HavuzData().brand,
        ),
      ),
      body: Container(
        child: Center(
          child: Text('Hello Detay'),
        ),
      ),
    );
  }
}

masukkan deskripsi gambar di sini

0
Ebrar 18 Juni 2020, 08:04

1 menjawab

Jawaban Terbaik

Anda dapat menyalin tempel menjalankan kode lengkap di bawah ini
Anda dapat mendorong dengan parameter HAVUZ_DATA[index] dan menangani HavuzData

Cuplikan kode

 Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => HavuzDetay(HAVUZ_DATA[index])),
            );
...                 
class HavuzDetay extends StatefulWidget {
  final HavuzData havuzData;

  HavuzDetay(this.havuzData);   

...
appBar: AppBar(
        title: Text(
          'Detay ' + widget.havuzData.brand,  

Demo kerja

enter image description here

Kode lengkap

import 'package:flutter/material.dart';

final HAVUZ_DATA = [
  HavuzData(
    id: 'User1',
    imageUrl: 'assets/images/arac1.jpeg',
    brand: 'Volkswagen',
    model: 'MULTIVAN 2.5 TDI COMFORTLINE',
    licensePlate: '34AAA3434',
    year: '2011',
    city: 'İstanbul',
    district: 'Bahçelievler',
    authorities: '20040601419',
    status: 'Ekspertiz yapıldı, Dosya sorumlusuna gönderildi.',
  ),
  HavuzData(
    id: 'User2',
    imageUrl: 'assets/images/arac2.jpg',
    brand: 'BMW',
    model: '420',
    licensePlate: '34ABC3434',
    year: '2018',
    city: 'İstanbul',
    district: 'Bakırköy',
    authorities: '20040601419',
    status: 'Ekspertiz yapıldı, Dosya sorumlusuna gönderildi.',
  )
];

class HavuzData {
  final String id;
  final String imageUrl;
  final String brand;
  final String model;
  final String licensePlate;
  final String year;
  final String city;
  final String district;
  final String authorities;
  final String status;

  const HavuzData({
    this.id,
    this.imageUrl,
    this.brand,
    this.model,
    this.licensePlate,
    this.year,
    this.city,
    this.district,
    this.authorities,
    this.status,
  });
}

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: ListView.builder(
            padding: EdgeInsets.only(left: 20, top: 10, right: 20, bottom: 10),
            itemCount: HAVUZ_DATA.length,
            itemBuilder: (BuildContext ctxt, int index) {
              return InkWell(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => HavuzDetay(HAVUZ_DATA[index])),
                    );
                  },
                  child: Card(
                    child: ListTile(
                      title: Text(HAVUZ_DATA[index].brand),
                      subtitle: Text(
                        HAVUZ_DATA[index].city,
                      ),
                    ),
                  ));
            }));
  }
}

class HavuzDetay extends StatefulWidget {
  final HavuzData havuzData;

  HavuzDetay(this.havuzData);

  @override
  _HavuzDetayState createState() => _HavuzDetayState();
}

class _HavuzDetayState extends State<HavuzDetay> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Detay ' + widget.havuzData.brand,
        ),
      ),
      body: Container(
        child: Center(
          child: Text('Hello Detay'),
        ),
      ),
    );
  }
}
0
chunhunghan 18 Juni 2020, 06:50