Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter - how to set width when wrapping on gridview?

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.

like image 800
Muhammad Imanudin Avatar asked Jun 13 '19 17:06

Muhammad Imanudin


2 Answers

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}"));
  }
}
like image 55
Sagar Karwande Avatar answered Sep 22 '22 13:09

Sagar Karwande


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

like image 23
LgFranco Avatar answered Sep 21 '22 13:09

LgFranco