何在Flutter中使用两个crossAxisCount做一个G

何在Flutter中使用两个crossAxisCount做一个G

本文介绍了如何在Flutter中使用两个crossAxisCount做一个GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是预期输出的示例

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

For the first row, the crossAxisCount will be 3 and the second row, the crossAxisCount will be 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');
                                    }),

推荐答案

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

You can set crossAxisCount to 1 and childAspectRatio to number you need, I use 2
In itemBuilder check index is Odd and return Row with 2 or 3 asset icon
You can copy paste run full code below

代码段

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 {

工作演示

完整代码

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

这篇关于如何在Flutter中使用两个crossAxisCount做一个GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:14