更改图像大小以在 flutter gridview 中获取整个单元格

时间:2021-06-03 15:42:57

标签: flutter gridview cell

我是 flutter 的新手,我在 flutter 中使用 gridview 来创建棋盘 (7x7) 游戏。我在一些单元格中有图像,这些图像随着播放的移动而变化。我希望图像与单元格大小相同,而不是更小。该图像是方形图像,应与单元格大小相同。如何实现这一目标?

这是我当前的代码:

main class:

import 'package:flutter/material.dart';
import 'BoardField.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  final List<List<int>> gridState = [
    [1, 0, 0, 0, 0, 0, 2],
    [0, 0, 0, 0, -1, 0, 0],
    [0, -1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, -1, 0],
    [0, 0, 0, -1, 0, 0, 0],
    [2, 0, 0, 0, 0, 0, 1]
  ];

  int selectedIndex = -1;

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Container(
          child: GridView.count(
        physics: NeverScrollableScrollPhysics(),
        childAspectRatio: 1,
        crossAxisCount: 7,
        children: List.generate(49, (index) {
          return BoardField(
            gridState: gridState,
            index: index,
            onButtonPressed: () {
              setState(() {
                int x = (index / gridState.first.length).floor();
                int y = (index % gridState.first.length);
                if (gridState[x][y] == 1 || gridState[x][y] == 2) {
                  if (selectedIndex != -1) {
                    gridState[(selectedIndex / gridState.first.length).floor()]
                        [(selectedIndex % gridState.first.length)] -= 2;
                  }
                  selectedIndex = index;
                  gridState[x][y] += 2;
                }
              });
            },
          );
        }),
      )),
    );
  }
}

Second Class:

import 'package:flutter/material.dart';

class BoardField extends StatelessWidget {
  BoardField(
      {Key? key,
      required this.gridState,
      required this.index,
      required this.onButtonPressed})
      : super(key: key);

  final List<List<int>> gridState;
  final int index;
  final VoidCallback onButtonPressed;

  @override
  Widget build(BuildContext context) {
    int x, y = 0;
    x = (index / gridState.first.length).floor();
    y = (index % gridState.first.length);
    bool dark = gridState[x][y] == -1;

    return Container(
      decoration: BoxDecoration(
          border: Border.all(color: Colors.black, width: 0.5),
          color: dark ? Colors.black : Colors.white),
      child: Center(
        child: BoardItem(
          gridState: gridState,
          itemIdentifier: gridState[x][y],
          onButtonPressed: onButtonPressed,
        ),
      ),
    );
  }
}

class BoardItem extends StatelessWidget {
  BoardItem(
      {Key? key,
      required this.gridState,
      required this.itemIdentifier,
      required this.onButtonPressed})
      : super(key: key);

  final List<List<int>> gridState;
  final int itemIdentifier;
  final VoidCallback onButtonPressed;

  @override
  Widget build(BuildContext context) {
    switch (itemIdentifier) {
      case 1:
        return TextButton(
            onPressed: onButtonPressed,
            child: Image.asset(
              'images/Red Sphere.jpg',
            ));

      case 2:
        return TextButton(
            onPressed: onButtonPressed,
            child: Image.asset(
              'images/Blue Sphere.jpg',
            ));

      case 3:
        return TextButton(
            onPressed: () {},
            child: Image.asset(
              'images/Red Sphere_highlight.jpg',
            ));

      case 4:
        return TextButton(
            onPressed: () {},
            child: Image.asset(
              'images/Blue Sphere_highlight.jpg',
            ));

      default:
        return Container(width: 0.0, height: 0.0);
    }
  }
}

谢谢。

0 个答案:

没有答案
相关问题