如何在flutter中创建分隔符gridview?

时间:2019-08-17 16:36:59

标签: flutter

我需要用水平线和垂直线分隔GridView的所有项目。 谢谢

         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()
                     ]
                   ],

                 )),

2 个答案:

答案 0 :(得分:0)

编辑,您需要GridTile

代码段

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

完整代码

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

答案 1 :(得分:0)



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,
        ),
      ),
    );
  }
}