flutter 将有边框和无边框的容器构件居中

webghufk  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(196)

在我的应用程序中,我需要四个Containers,第一行没有边框(我通过提供与Container相同的颜色来删除边框)。第二行的边框如下所示(在真实的设备上测试):-

然而,如果你仔细观察,似乎第一行的蓝色容器稍微向红色容器扩展,使它看起来好像第一行没有相等地扩展,但如果你缩放足够,你会看到蓝色容器与红色容器重叠,请参考下图:-

如何将容器居中或使其看起来像是居中对齐?

请随时提出不同的方法来获得所需的输出。
我使用的代码:-

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Box(),
    );
  }
}

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

  @override
  State<Box> createState() => _BoxState();
}

class _BoxState extends State<Box> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              for (int i = 0; i < 2; i++) ...[
                Container(
                  height: 100,
                  width: 100,
                  decoration: BoxDecoration(
                      color: Colors.red,
                      border: Border(
                        bottom: BorderSide(
                            width: i == 1 ? 1 : 0,
                            color: i == 1 ? Colors.black : Colors.red),
                        left: BorderSide(
                            width: i == 0 ? 0 : 1,
                            color: i == 0 ? Colors.red  : Colors.black),
                        top: BorderSide(
                            width: i == 0 ? 0 : 1,
                            color: i == 0 ? Colors.red : Colors.black),
                        right: BorderSide(
                            width: i == 0 ? 0 : 0.5,
                            color: i == 0 ? Colors.red : Colors.black),
                      )),
                ),
              ]
            ],
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              for (int i = 0; i < 2; i++) ...[
                Container(
                  height: 100,
                  width: 100,
                  decoration: BoxDecoration(
                      color: Colors.blue,
                      border: Border(
                        bottom: BorderSide(
                            width: i == 1 ? 1 : 0,
                            color: i == 1 ? Colors.black : Colors.blue),
                        left: BorderSide(
                            width: i == 0 ? 0 : 0.5,
                            color: i == 0 ? Colors.blue : Colors.black),
                        top: BorderSide(
                            width: i == 0 ? 0 : 1,
                            color: i == 0 ? Colors.blue : Colors.black),
                        right: BorderSide(
                            width: i == 0 ? 0 : 1,
                            color: i == 0 ? Colors.blue : Colors.black),
                      )),
                ),
              ]
            ],
          ),
        ],
      ),
    ));
  }
}
lx0bsm1f

lx0bsm1f1#

在边框宽度为0的情况下,可以将颜色设置为Colors.transparent
试试这个:

import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Box(),
    );
  }
}

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

  @override
  State<Box> createState() => _BoxState();
}

class _BoxState extends State<Box> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: Row(

        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              for (int i = 0; i < 2; i++) ...[
                Container(
                  height: 100,
                  width: 100,
                  decoration: BoxDecoration(
                      color: Colors.red,
                      border: Border(
                        bottom: BorderSide(
                            width: i == 1 ? 1 : 0,
                            color: i == 1 ? Colors.black : Colors.transparent),
                        left: BorderSide(
                            width: i == 0 ? 0 : 1,
                            color: i == 0 ? Colors.transparent  : Colors.black),
                        top: BorderSide(
                            width: i == 0 ? 0 : 1,
                            color: i == 0 ? Colors.transparent : Colors.black),
                        right: BorderSide(
                            width: i == 0 ? 0 : 0.5,
                            color: i == 0 ? Colors.transparent : Colors.black),
                      )),
                ),
              ]
            ],
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              for (int i = 0; i < 2; i++) ...[
                Container(
                  height: 100,
                  width: 100,
                  decoration: BoxDecoration(
                      color: Colors.blue,
                      border: Border(
                        bottom: BorderSide(
                            width: i == 1 ? 1 : 0,
                            color: i == 1 ? Colors.black : Colors.transparent),
                        left: BorderSide(
                            width: i == 0 ? 0 : 0.5,
                            color: i == 0 ? Colors.transparent : Colors.black),
                        top: BorderSide(
                            width: i == 0 ? 0 : 1,
                            color: i == 0 ? Colors.transparent : Colors.black),
                        right: BorderSide(
                            width: i == 0 ? 0 : 1,
                            color: i == 0 ? Colors.transparent : Colors.black),
                      )),
                ),
              ]
            ],
          ),
        ],
      ),
    ));
  }
}

相关问题