如何在Flutter中使用两个crossAxisCount做一个GridView?

时间:2019-12-10 01:31:54

标签: flutter

This is the example of what I want to achieve

对于第一行,crossAxisCount将为3,第二行,crossAxisCount将为2。

GridView.builder(
                                  physics: NeverScrollableScrollPhysics(),
                                    gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
                                      crossAxisCount: 3,
                                      childAspectRatio: 0.75,
                                      mainAxisSpacing: 2.0,
                                      crossAxisSpacing: 1.0,
                                    ),
                                    itemCount: int.parse(snapshot.data.result[num].collected),
                                    itemBuilder:
                                        (BuildContext context, int i) {
                                      return Image.asset(
                                          'assets/coin.png');
                                    }),

1 个答案:

答案 0 :(得分:0)

您可以将crossAxisCount设置为1,将childAspectRatio设置为您需要的号码,我用2
itemBuilder中,检查索引为Odd,并返回带有2或3个资产图标的Row
您可以在

下复制粘贴运行完整代码

代码段

GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 1, childAspectRatio: 2),
        itemCount: _icons.length,
        itemBuilder: (context, index) {
          if (index.isOdd) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Container(
                  child: Image.asset('assets/coin.png'),
                ),
                Container(
                  child: Image.asset('assets/coin.png'),
                ),
                Container(
                  child: Image.asset('assets/coin.png'),
                ),
              ],
            );
          } else {

工作演示

enter image description here

完整代码

    import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Expanded(flex: 1, child: InfiniteGridView()),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

class InfiniteGridView extends StatefulWidget {
  @override
  _InfiniteGridViewState createState() => new _InfiniteGridViewState();
}

class _InfiniteGridViewState extends State<InfiniteGridView> {
  List<IconData> _icons = [];

  @override
  void initState() {
    _retrieveIcons();
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 1, childAspectRatio: 2),
        itemCount: _icons.length,
        itemBuilder: (context, index) {
          if (index.isOdd) {
            return Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Container(
                  child: Image.asset('assets/coin.png'),
                ),
                Container(
                  child: Image.asset('assets/coin.png'),
                ),
                Container(
                  child: Image.asset('assets/coin.png'),
                ),
              ],
            );
          } else {
            return Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Container(
                  child: Image.asset('assets/coin.png'),
                ),
                Container(
                  child: Image.asset('assets/coin.png'),
                ),
              ],
            );
          }
        });
  }

  void _retrieveIcons() {
    Future.delayed(Duration(milliseconds: 200)).then((e) {
      setState(() {
        _icons.addAll([
          Icons.ac_unit,
          Icons.airport_shuttle,
          Icons.all_inclusive,
          Icons.beach_access,
          Icons.cake,
          Icons.free_breakfast
        ]);
      });
    });
  }
}