smbbb
smbbb

Reputation: 123

Change the background color of wrap when I click

I have a GestureDector under Wrap and I would like to change the color of the background from white to Orange when I select one. I don't know what to add on onTap to implement. Would you be able to help? Here is my code

        Wrap(
      children: [
        "Computer Science",
        "Engineering",
        "Kinesiology",
        "English Literature",
        "Finance",
        "Economics",
        "Physics",
        "Pre-med",
        "Biochemistry",
        "Football Game",
        "Fraternity"
      ]
          .map((f) => GestureDetector(
                child: Container(
                  padding: EdgeInsets.symmetric(
                      horizontal: 20.0, vertical: 10.0),
                  margin: EdgeInsets.only(
                      left: 5.0, right: 5.0, top: 10.0, bottom: 10.0),
                  decoration: BoxDecoration(
                    border:
                        Border.all(color: Color(0xFF282f61), width: 2.0),
                    borderRadius: BorderRadius.all(Radius.circular(
                            10.0) //                 <--- border radius here
                        ),
                  ),
                  child: Text(
                    f,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.0,
                    ),
                  ),
                ),
                onTap: () {
                  setState(() {});
                },
              ))
          .toList(),
        
    ),

I did declare int _selectedIndex = -1; in the beginning of the state class but I don't know how should I use it. Thank you so much.

Upvotes: 1

Views: 860

Answers (1)

Nabin Dhakal
Nabin Dhakal

Reputation: 2192

You need to create different widget so as to make them behave differently. Try as follows:

   class CustomListView_frequentlyUsed extends StatefulWidget {
  @override
  State<CustomListView_frequentlyUsed> createState() =>
      _CustomListView_frequentlyUsedState();
}

class _CustomListView_frequentlyUsedState
    extends State<CustomListView_frequentlyUsed> {
 

 
  @override
  initState() {
    super.initState();
  }
 @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
            child: Wrap(
      children: [
        "Computer Science",
        "Engineering",
        "Kinesiology",
        "English Literature",
        "Finance",
        "Economics",
        "Physics",
        "Pre-med",
        "Biochemistry",
        "Football Game",
        "Fraternity"
      ]
          .map((f) =>CommonItems(f) )
          .toList(),
    ),));
  }
}

Then create the CommonItems as follows:

    class CommonItems extends StatefulWidget {
  final String f;
  
  CommonItems(this.f);
  
  
  @override
  _CommonItemsState createState() => _CommonItemsState();
}

class _CommonItemsState extends State<CommonItems> {
  bool isClicked=false;
  
 @override
  Widget build(BuildContext context) {
    return GestureDetector(
                    child: Container(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 20.0, vertical: 10.0),
                      margin: const EdgeInsets.only(
                          left: 5.0, right: 5.0, top: 10.0, bottom: 10.0),
                      decoration: BoxDecoration(
                        color:isClicked?Colors.green: Colors.transparent,
                        border: Border.all(
                          color:isClicked?Colors.green: Color(0xFF282f61),
                          width: 2.0,
                      
                        ),
                        borderRadius:
                            const BorderRadius.all(Radius.circular(10.0) //
                                ),
                      ),
                      child: Text(
                        widget.f,
                        style: const TextStyle(
                          color: Colors.black,
                          fontSize: 16.0,
                        ),
                      ),
                    ),
                    onTap: () {
                      setState(() {
                        isClicked = !isClicked;
                      });
                    },
                  );
  }
}

Upvotes: 1

Related Questions