I wanna display 6 squares in one container (height of the container is 1/3 of the screen) like the given image How to do?

huangapple go评论148阅读模式
英文:

I wanna display 6 squares in one container (height of the container is 1/3 of the screen) like the given image How to do?

问题

我想在一个容器中以固定大小显示6个正方形(容器的高度是屏幕的1/3),就像给定的图像一样。我已经得到了它,但当我运行多个模拟器时,正方形变成了矩形。我想要在一个固定大小的容器中精确显示6个正方形,如何做到并且如何使其响应式?

我想要像给定的图像一样显示(响应式)。

英文:

I wanna display 6 squares in one container (height of the container is 1/3 of the screen) like the given image How to do?

I want to display 6 squares in fixed size of a container (height of the container is 1/3 of the screen) like the given image I got it but its not responsive when I run multiple emulator the square changed to rectangle I want exact 6 squares in a fixed size of container how to do and how to make responsive?

I want to display like the given image(responsive)

答案1

得分: 0

你可以使用以下代码:
你的代码可能会溢出,因为你可能在使用填充。

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: Scaffold(
          body: Center(
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Container_(),
                  Container_(),
                  Container_(),
                ],
              ),
              Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
                Container_(),
                Container_(),
                Container_(),
              ])
            ]),
      )),
    );
  }
}

class Container_ extends StatelessWidget {
  const Container_({super.key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(3.0),
      child: Container(
        width: MediaQuery.of(context).size.width / 3 - 18,
        height: MediaQuery.of(context).size.width / 3 - 18,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12), color: Colors.indigo),
      ),
    );
  }
}

你还提供了一个图片链接,但我无法直接显示图片。

英文:

you can use the following code:<br>
you're code will overflow because you may be using padding

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State&lt;MyApp&gt; createState() =&gt; _MyAppState();
}

class _MyAppState extends State&lt;MyApp&gt; {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(useMaterial3: true),
      home: Scaffold(
          body: Center(
        child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Container_(),
                  Container_(),
                  Container_(),
                ],
              ),
              Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [
                Container_(),
                Container_(),
                Container_(),
              ])
            ]),
      )),
    );
  }
}

class Container_ extends StatelessWidget {
  const Container_({super.key});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(3.0),
      child: Container(
        width: MediaQuery.of(context).size.width / 3 - 18,
        height: MediaQuery.of(context).size.width / 3 - 18,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12), color: Colors.indigo),
      ),
    );
  }
}

https://i.stack.imgur.com/cMy0x.png

huangapple
  • 本文由 发表于 2023年3月9日 22:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定