How to pass the word of DropdownMenuItem to the main page? Flutter

Viewed 23

I'm new to programming

Any help to pass the selectedSize to the DetailsProduct? I was trying to save the dropdown value in selectedSize but it is returning "Size_Color(Unique, White, 0.00000, 1.138211, )", I just wanted the size.

if I save the value, do you have any suggestions to send this value to the DetailsProduct?

MODEL

import 'package:equatable/equatable.dart';

class Tamanho_Cor extends Equatable {
  final String sinc;
  final String reference;
  final String size;
  final String color;
  final String quantity;
  final String price;
  final String mainreference;

  Tamanho_Cor({
    required this.sinc,
    required this.reference,
    required this.size,
    required this.color,
    required this.quantity,
    required this.price,
    required this.mainreference,
  });

  factory Tamanho_Cor.fromJson(Map<String, dynamic> json) {
    return Tamanho_Cor(
      sinc: json['Sinc'],
      reference: json['Reference'],
      size: json['Size'],
      color: json['Color'],
      quantity: json['Quantity'],
      price: json['Price'],
      mainreference: json['MainReference'],
    );
  }

  @override
  // TODO: implement props
  List<Object?> get props => [size, color, quantity, price, mainreference];
}

Class SizeColor

import 'package:eztec/blocs/list/details/details_images/size_color_blocs.dart';
import 'package:eztec/blocs/list/details/details_images/size_color_events.dart';
import 'package:eztec/blocs/list/details/details_images/size_color_states.dart';
import 'package:eztec/data/list/size_color_data.dart';
import 'package:eztec/models/list/size_color_model.dart';
import 'package:eztec/screens/list/details_product.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class SizeColor extends StatefulWidget {
  final String ref;
  final String nome;
  const SizeColor({Key? key, required this.ref, required this.nome})
      : super(key: key);

  @override
  State<SizeColor> createState() => _SizeColorState(ref, nome);
}

class _SizeColorState extends State<SizeColor> {
  var ref;
  var nome;

  _SizeColorState(this.ref, this.nome);

  var selectedSize;

  String myActivityResultSize = '';

  final formKey = GlobalKey<FormState>();

  saveForm() {
    var form = formKey.currentState;
    if (form!.validate()) {
      form.save();
      setState(() {
        myActivityResultSize = selectedSize;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => SizeColorBloc(
          RepositoryProvider.of<SizeColorRepository>(context), ref)
        ..add(LoadSizeColorEvent()),
      child:
          BlocBuilder<SizeColorBloc, SizeColorState>(builder: (context, state) {
        if (state is SizeColorLoadingState) {
          return const Center(
            child: CircularProgressIndicator(),
          );
        }
        if (state is SizeColorLoadedState) {
          List<Tamanho_Cor> listsizecolor = state.sizecolor;

          return Form(
            key: formKey,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                const Text(
                  'Tamanho:',
                  style: TextStyle(
                      fontSize: 17,
                      color: Colors.black,
                      fontWeight: FontWeight.bold),
                ),
                const SizedBox(
                  height: 10,
                ),
                Container(
                  padding: const EdgeInsets.all(16),
                  child: DropdownButtonFormField(
                    value: selectedSize,
                    hint: const Text('Selecione o tamanho'),
                    onSaved: (value) {
                      setState(() {
                        selectedSize = value;
                      });
                    },
                    items: listsizecolor.map(
                      (list) {
                        return DropdownMenuItem(
                          value: list.toString(),
                          child: Text(list.size),
                        );
                      },
                    ).toList(),
                    onChanged: ((value) {
                      setState(() {
                        selectedSize = value.toString();

                        saveForm();
                      });
                    }),
                  ),
                ),
                Container(
                  padding: EdgeInsets.all(16),
                  child: Text(myActivityResultSize),
                ),
                const SizedBox(
                  height: 30,
                ),
              ],
            ),
          );
        }
        if (state is SizeColorErrorState) {
          return const Center(
            child: Text("Erro, por favor reinicie a app"),
          );
        }
        return const Center(
          child: Text(
              "Erro, por favor reinicie a app para carregar novamente os dados"),
        );
      }),
    );
  }
}

DETAILSPRODUCT

import 'package:eztec/blocs/list/details/details_blocs.dart';
import 'package:eztec/blocs/list/details/details_events.dart';
import 'package:eztec/blocs/list/details/details_states.dart';
import 'package:eztec/data/list/details_data.dart';
import 'package:eztec/models/cart_model.dart';
import 'package:eztec/models/list/details_model.dart';
import 'package:eztec/posts/widgets/post_list_item_disp.dart';
import 'package:eztec/screens/list/details/details_images.dart';
import 'package:eztec/screens/list/details/details_size_color.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class DetailsProduct extends StatefulWidget {
  final String nome;
  final String ref;

  const DetailsProduct({
    Key? key,
    required this.nome,
    required this.ref,
  }) : super(key: key);

  @override
  // ignore: no_logic_in_create_state
  State<DetailsProduct> createState() => _DetailsProductState(nome, ref);
}

class _DetailsProductState extends State<DetailsProduct> {
  final String nomef;
  final String ref;

  _DetailsProductState(this.nomef, this.ref);

  @override
  Widget build(BuildContext context) {
    final cart = Provider.of<Cart>(context);
    return BlocProvider(
      create: (context) =>
          DetailsBloc(RepositoryProvider.of<DetailsRepository>(context), nomef)
            ..add(LoadDetailsEvent()),
      child: Scaffold(
        backgroundColor: const Color(0xFFEDECF2),
        appBar: AppBar(
          backgroundColor: Colors.white,
          leading: IconButton(
            color: Colors.black,
            onPressed: () {
              Navigator.of(context).pop();
            },
            icon: const Icon(Icons.arrow_back_ios),
          ),
        ),
        body: BlocBuilder<DetailsBloc, DetailsState>(
          builder: (context, state) {
            if (state is DetailsLoadedState) {
              List<DetailsModel> listDetails = state.details;

              return ListView.builder(
                itemCount: listDetails.length,
                itemBuilder: (context, index) {
                  return ListTile(
                    title: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        const SizedBox(
                          height: 40,
                        ),
                        Text(
                          listDetails[index].named,
                          style: const TextStyle(
                              fontSize: 28,
                              color: Colors.black,
                              fontWeight: FontWeight.bold),
                        ),
                        const SizedBox(
                          height: 20,
                        ),
                        SizeColor(
                          ref: listDetails[index].referenced,
                          nome: listDetails[index].named,
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Text(
                                "${listDetails[index].priced.toStringAsFixed(2)}€",
                                style: const TextStyle(
                                    color: Colors.black, fontSize: 30)),
                          ],
                        ),
                        const SizedBox(
                          height: 20,
                        ),
                        SizedBox(
                          height: 58,
                          width: 200,
                          // ignore: deprecated_member_use
                          child: ElevatedButton(
                            onPressed: () {
                              cart.addItem(
                                  listDetails[index], 1, 'cor', 'tamanho');
                              showTopSnackBar(context);
                            },
                            child: const Text(
                              'Comprar',
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                        ),
                      ],
                    ),
                  );
                },
              );
            }
            if (state is DetailsErrorState) {
              return const Center(
                child: Text("Erro, ao carregar os detalhes"),
              );
            }
            return const Center(
              child: Text(""),
            );
          },
        ),
      ),
    );
  }
}


0 Answers
Related