flutter - how to set width when wrapping on gridview?

3,947

Solution 1

Demo of Chips widget

class MyHomePage extends StatelessWidget {
  var tags = [
    "love",
    "instagood",
    "photooftheday",
    "beautiful",
    "fashion",
    "happy",
    "tbt",
    "cute",
    "followme",
    "like4like",
    "follow",
    "picoftheday",
    "me",
    "selfie",
    "summer",
    "instadaily"
  ];

  var selected_tags = ["love", "me", "summer"];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Wrap(
        spacing: 8.0, // gap between adjacent chips
        runSpacing: 4.0, // gap between lines
        children: <Widget>[...generate_tags()],
      ),
    );
  }

  generate_tags() {
    return tags.map((tag) => get_chip(tag)).toList();
  }

  get_chip(name) {
    return FilterChip(
        selected: selected_tags.contains(name),
        selectedColor: Colors.blue.shade800,
        disabledColor: Colors.blue.shade400,
        labelStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
        label: Text("#${name}"));
  }
}

Solution 2

You are trying to make a Chip (from material):
https://material.io/design/components/chips.html#

You should use the Chip Widget (or one of the listed variants):
https://api.flutter.dev/flutter/material/Chip-class.html

Them you should change the grid to a Wrap Widget (witch will contain the list of Chips): https://api.flutter.dev/flutter/widgets/Wrap-class.html

Share:
3,947
Muhammad Imanudin
Author by

Muhammad Imanudin

Updated on December 11, 2022

Comments

  • Muhammad Imanudin
    Muhammad Imanudin over 1 year

    I have an issue when want to make a like Tags Widget using GridView, see result:

    image

    I want to make it as shown below: image

    but the width of the button is set automatically from GridView wrapping.

    how to make the width set dynamically?

    any answer will be appreciated.

  • Sagar Karwande
    Sagar Karwande almost 5 years
    You can use Chip variant as per you requirement from: Chips
  • Muhammad Imanudin
    Muhammad Imanudin almost 5 years
    Sory im forgot attach my code. but your answer above is the solution I'm looking for
  • Muhammad Imanudin
    Muhammad Imanudin almost 5 years
    I just heard about Chip Widget. this is what I'm looking for