Home > Mobile >  how to onTap with logical condition with flutter id
how to onTap with logical condition with flutter id

Time:09-16

I'm here to show how when onTap is done, the data will be fetched dynamically based on the kontenId, and when you look at my source code, I'm still calling it statically by calling the kontenId one by one. So how do I get it when tapped to call the kontenId dynamically without me calling one by one, like an example

if (productController.productList[index].kontenId ==
                                "7") {
                              Get.to(
                                  () => ProductDetailListView(
                                        product: productController
                                            .productList[index],
                                      ),
                                  arguments: productController
                                      .productList[index].kontenId);
                            } else if (productController
                                    .productList[index].kontenId ==
                                "8") {
                              Get.to(
                                  () => ProductDetailListView(
                                        product: productController
                                            .productList[index],
                                      ),
                                  arguments: productController
                                      .productList[index].kontenId);

Here's an example of my source code

product_list_view.dart

class ProductListView extends GetView<ProductController> {
  final productController = Get.put(ProductController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(AppString.productList),
      ),
      body: Container(
        child: ListProduct(productController: productController),
      ),
    );
  }
}

class ListProduct extends StatelessWidget {
  const ListProduct({
    Key key,
    @required this.productController,
  }) : super(key: key);

  final ProductController productController;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Obx(
          () {
            if (productController.isLoading.value)
              return Center(
                child: CircularProgressIndicator(),
              );
            else
              return Expanded(
                child: ListView.builder(
                  itemCount: productController.productList.length,
                  itemBuilder: (context, index) {
                    return Column(
                      children: [
                        GestureDetector(
                          onTap: () {
                    
                            if (productController.productList[index].kontenId ==
                                "7") {
                              Get.to(
                                  () => ProductDetailListView(
                                        product: productController
                                            .productList[index],
                                      ),
                                  arguments: productController
                                      .productList[index].kontenId);
                            } else if (productController
                                    .productList[index].kontenId ==
                                "8") {
                              Get.to(
                                  () => ProductDetailListView(
                                        product: productController
                                            .productList[index],
                                      ),
                                  arguments: productController
                                      .productList[index].kontenId);
                            } else if (productController
                                    .productList[index].kontenId ==
                                "9") {
                              Get.to(
                                  () => ProductDetailListView(
                                        product: productController
                                            .productList[index],
                                      ),
                                  arguments: productController
                                      .productList[index].kontenId);
                            } else {
                              Get.snackbar("Data Kosong", "Data masih kosong");
                            }
                          },
                        
                          child: Card(
                            // color: Colors.blue[900],
                            elevation: 5,
                            margin: const EdgeInsets.all(10),
                            child: ListTile(
                              title: Text(
                                productController.productList[index].kontenMenu,
                                style: TextStyle(
                                  fontSize: 20,
                                ),
                              ),
                            ),
                          ),
                        )
                      ],
                    );
                  },
                ),
              );
          },
        ),
      ],
    );
  }
}

product_detail.dart

class ProductDetailListView extends GetView<ProductController> {
  // final ProductController productController = Get.put(ProductController());
  final productController = Get.put(ProductController());
  final controller = Get.find();
  final ProductModel product;
  ProductDetailListView({this.product});

  final data = Get.arguments;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(product.kontenMenu),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.vertical(
            bottom: Radius.circular(30),
          ),
        ),
      ),
      body: Container(
        child: Column(
          children: [
            // Text('Border'),

            // ignore: unnecessary_null_comparison

            if (productController.productList != null)
              Expanded(
                child: ListView.builder(
                  itemCount: data == "7"
                      ? productController.productList.length
                      : data == "8"
                          ? productController.productList2.length
                          : data == "9"
                              ? productController.productList3.length
                              : productController.productList2.length,
                  itemBuilder: (BuildContext context, int index) {
                    return GestureDetector(
                      onTap: () {},
                      child: Card(
                        // color: Color.fromARGB(255, 203, 199, 199),
                        elevation: 5,
                        margin: const EdgeInsets.all(10),

                        child: ListTile(
                          title: Text(
                            data == '7'
                                ? productController
                                    .productList1[index].kontenMenu
                                : data == '8'
                                    ? productController
                                        .productList2[index].kontenMenu
                                    : data == '9'
                                        ? productController
                                            .productList3[index].kontenMenu
                                        : 'Belum ada data',
                          ),
                        ),
                      ),
                    );
                  },
                ),
              ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

Is this what you're looking for? Your question could be clearer...

onTap: () {
  if ({"7", "8", "9"}.contains(productController.productList[index].kontenId)) {
  Get.to(
    () => ProductDetailListView(
          product: productController
              .productList[index],
        ),
    arguments: productController
        .productList[index].kontenId);
  } else {
  Get.snackbar("Data Kosong", "Data masih kosong");
  }
}

CodePudding user response:

You can use conditional operator (?:) if condition is true then onTap will work otherwise set null or empty function.

CodePudding user response:

Simpilified, your code sort of looks like this

if (a == "7") {
  doSomething();
} else if (a == "8") {
  doSomething();
} else if (a == "9") {
  doSomething();
} else {
  doSomethingElse();
}

You can make it shorter to just combine the conditions like this:

if (a == "7" || a == "8" || a == "9") {
  doSomething();
} else {
  doSomethingElse();
}

Or even shorter

if ({"7", "8", "9"}.contains(a)) {
  doSomething();
} else {
  doSomethingElse();
}

Alternatively, use a switch statement:

switch(a) {
  case "7":
  case "8":
  case "9":
    doSomething();
    break;
  default:
    doSomethingElse();
}
  • Related