Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create separator gridview in flutter?

Tags:

flutter

I need to separate all item of GridView with line horizontal and vertical. thanks

         GridView(
                   shrinkWrap:true,
                   gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( 
                   childAspectRatio: 1.0,
                   mainAxisSpacing: 2.0,      
                   crossAxisSpacing: 2.0,
                   crossAxisCount: 3),
                   children: <Widget>[
                     GestureDetector(
                       onTap: () {
                         //_settingModalBottomSheet(context);
                       },
                       child: itemWidgetDialog()
                     ),
                     for (int i=0; i<10;i++) ...[
                      itemWidgetDialog()
                     ]
                   ],

                 )),
like image 524
samira salah Avatar asked Aug 17 '19 16:08

samira salah


People also ask

How do you add a separator line in Flutter?

If you have a list of widgets, you may need to add a separator between the widgets. In Flutter, there are two widgets suitable for that purpose: Divider and VerticalDivider . Divider is used to create a horizontal line divider, while VerticalDivider is used to create a vertical line divider.

How do I give spacing in GridView builder Flutter?

Flutter GridView – Spacing between Items To set spacing between items along main axis or cross axis, set the required double values for mainAxisSpacing property and crossAxisSpacing property respectively. The default value of mainAxisSpacing and crossAxisSpacing is zero.


2 Answers

Edit you need GridTile

code snippet

GridTile(
    child: Container(
  decoration:
      BoxDecoration(border: Border.all(color: Colors.black, width: 0.5)),

full code

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final title = "Grid List";

    return MaterialApp(
        title: title,
        home: Scaffold(
            appBar: AppBar(
              title: Text(title),
            ),
            body: GridView.count(
                crossAxisCount: 3,
                children: List.generate(choices.length, (index) {
                  return Center(
                    child: ChoiceCard(choice: choices[index]),
                  );
                }))));
  }
}

class Choice {
  const Choice({this.title, this.icon});

  final String title;
  final IconData icon;
}

const List<Choice> choices = const <Choice>[
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.drafts),
  const Choice(title: 'Boat', icon: Icons.dvr),
  const Choice(title: 'Bus', icon: Icons.copyright),
  const Choice(title: 'Train', icon: Icons.cloud_off),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.drafts),
  const Choice(title: 'Boat', icon: Icons.dvr),
  const Choice(title: 'Bus', icon: Icons.copyright),
  const Choice(title: 'Train', icon: Icons.cloud_off),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.directions_bike),
  const Choice(title: 'Boat', icon: Icons.directions_boat),
  const Choice(title: 'Bus', icon: Icons.directions_bus),
  const Choice(title: 'Train', icon: Icons.directions_railway),
  const Choice(title: 'Walk', icon: Icons.directions_walk),
  const Choice(title: 'Car', icon: Icons.directions_car),
  const Choice(title: 'Bicycle', icon: Icons.drafts),
  const Choice(title: 'Boat', icon: Icons.dvr),
  const Choice(title: 'Bus', icon: Icons.copyright),
  const Choice(title: 'Train', icon: Icons.cloud_off),
];

class ChoiceCard extends StatelessWidget {
  const ChoiceCard({Key key, this.choice}) : super(key: key);
  final Choice choice;

  @override
  Widget build(BuildContext context) {
    final TextStyle textStyle = Theme.of(context).textTheme.display1;
    return GridTile(
        child: Container(
      decoration:
          BoxDecoration(border: Border.all(color: Colors.black, width: 0.5)),
      child: Card(
          color: Colors.white,
          child: Center(
            child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Icon(choice.icon, size: 80.0, color: textStyle.color),
                  Text(choice.title, style: textStyle),
                ]),
          )),
    ));
  }
}

enter image description here

like image 127
chunhunghan Avatar answered Oct 16 '22 07:10

chunhunghan




import 'package:flutter/material.dart';

class GridList extends StatefulWidget {
  String title,productCount;
  List<String> sampleData;

  GridList(this.title, this.productCount, this.sampleData);
  @override
  _GridListState createState() => _GridListState();
}

class _GridListState extends State<GridList> {

  @override
  Widget build(BuildContext context) {
    int gridViewCrossAxisCount = 3;
    return Container(
        margin: EdgeInsets.all(25.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[

            GridView.builder(
              shrinkWrap: true,
              controller: new ScrollController(keepScrollOffset: false),
              itemCount: widget.sampleData.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: gridViewCrossAxisCount,
                childAspectRatio: (3 / 4),
              ),
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  decoration: myBoxDecoration(index, gridViewCrossAxisCount),

                );
              },
            ),
                   ],

        ), 
        );
  }

  BoxDecoration myBoxDecoration(int index, int gridViewCrossAxisCount) {
    index++;
    return BoxDecoration(
      color: Colors.green,
      border: Border(
        left: BorderSide(
          //                   <--- left side
          color: index % gridViewCrossAxisCount != 0 ? Colors.black12 : Colors.transparent,
          width: 1.5,
        ),
        top: BorderSide(
          //                   <--- left side
          color: index > gridViewCrossAxisCount ? Colors.black12 : Colors.transparent,
          width: 1.5,
        ),
      ),
    );
  }
}


like image 41
Hossein Sohan Avatar answered Oct 16 '22 09:10

Hossein Sohan