Home > OS >  Showing a tick icon based on enum value in Dart
Showing a tick icon based on enum value in Dart

Time:09-01

Need to make logic to show a tick icon if based on enum value with if value is pending and true show a tick, if confirmed and true show icon two times and so on. Please help.

   enum Status { Pending, Confirmed, Shipped, Received }






Widget build(BuildContext context) {
    return Directionality(
     
      child: Row(
        children: [
          widget.title == 'Shipped'
   ],
      ),
    );
  }
}

CodePudding user response:

You can achieve that be creating an extension on your enum
note that to use describeEnum you have to add this import to your dart file
import 'package:flutter/foundation.dart';

extension on Status {
    String get value => describeEnum(this);
}

and based on it, you can show an icon or a badge

 Row(
    children: [
        if(widget.title.value == 'Shipped')
          Icon(Icons.done)
    ]
 ),

note that I assumed that the sent title is an enum value

CodePudding user response:

You can track the selected value index, and render that much widget.


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;
  @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("btn ${Status.values[i]}"),
            ),
          Row(
            children: [
              for (int i = 0; i <= selectedItemIndex; i  ) Icon(Icons.check)
            ],
          )
        ],
      ),
    );
  }
}

  • Related