Home > Software engineering >  Need to create logic in dart to display icon based on a string and true boolean parameter
Need to create logic in dart to display icon based on a string and true boolean parameter

Time:09-02

Need someone to develop logic if string coming is pending show one icon tick if confirmed show two ticks and so on. Here is what I have made that display a tick when button is clicked need to make it in a way to get a string and boolean value to show tick mark similar to it. Please help. Thanks

// ignore_for_file: constant_identifier_names

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class CheckStatus extends StatefulWidget {
  const CheckStatus({Key? key}) : super(key: key);

  @override
  State<CheckStatus> createState() => _CheckStatusState();
}

enum Status { Pending, Confirmed, Shipped, Received }

class _CheckStatusState extends State<CheckStatus> {
  int selectedItemIndex = 0;
  Status received = Status.Received;
  Status shipped = Status.Shipped;
  Status pending = Status.Received;
  Status confirmed = Status.Received;
  List<bool> orderStatusValue = [];
  @override
  void initState() {
    bool flagrecieved = true;
    bool flagshipped = true;
    bool flagconfirmed = true;
    bool flagpending = true;
    for (int i = 0; flagrecieved; i  ) {
      if (Status.values[i] == received) {
        flagrecieved = false;
      }
      else if
      (Status.values[i] == shipped) {
        flagshipped = false;
      }
      else if
      (Status.values[i] == confirmed) {
        flagconfirmed = false;
      }
      else if
      (Status.values[i] == pending) {
        flagpending = false;
      }
      orderStatusValue[i] = true;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          for (int i = 0; i < Status.values.length; i  )
            ElevatedButton(
              onPressed: () {
                selectedItemIndex = i;
                setState(() {});
              },
              child: Text("Order Status ${Status.values[i]}"),
            ),
          Row(
            children: [
              for (int i = 0; i <= selectedItemIndex; i  ) Icon(Icons.check),
              ElevatedButton(onPressed: () {}, child: Text("Back"))
            ],
          )
        ],
      ),
    );
  }
}

CodePudding user response:

Create function to pass data your string

 Icon getIcon(String data) {
        if (data == "1") {
          return Icon(Icons.navigate_next);
        } else if (data == 2) {
          return Icon(Icons.navigate_next);
        } else {
          return Icon(Icons.navigate_next);
        }
      }

Call it where you want to display getIcon("data")

CodePudding user response:

You can just use

Expanded(
  child: Row(
    children: [
      for (int i = 0; i <= selectedItemIndex; i  ) Icon(Icons.check),
      ElevatedButton(onPressed: () {}, child: Text("Back"))
    ],
  ),
)

If you like to generate list of bool, you can use inline method

class CheckStatus extends StatefulWidget {
  const CheckStatus({Key? key}) : super(key: key);

  @override
  State<CheckStatus> createState() => _CheckStatusState();
}

enum Status { Pending, Confirmed, Shipped, Received }

class _CheckStatusState extends State<CheckStatus> {
  int selectedItemIndex = 0;
  List<bool> orderStatusValue = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          for (int i = 0; i < Status.values.length; i  )
            ElevatedButton(
              onPressed: () {
                selectedItemIndex = i;
                setState(() {});
              },
              child: Text("Order Status ${Status.values[i]}"),
            ),
          Text("${orderStatusValue}"),
          Expanded(
            child: Row(
              children: [
                ...() {
                  List<Widget> items = [];
                  orderStatusValue =
                      List.generate(Status.values.length, (index) => false);
                  for (int i = 0; i <= selectedItemIndex; i  ) {
                    items.add(Icon(Icons.check));
                    orderStatusValue[i] = true;
                  }
                  return items;
                }(),
                ElevatedButton(onPressed: () {}, child: Text("Back"))
              ],
            ),
          )
        ],
      ),
    );
  }
}
  • Related