flutter 如何在卡片控件中对齐图标和按钮?

vmdwslir  于 2023-01-18  发布在  Flutter
关注(0)|答案(3)|浏览(141)

我有麻烦的问题,使我的图标按钮对齐到中间的卡。正如你可以看到的图片,图标按钮是在顶部附加到顶部的卡。
我怎样使它在卡片的一半大小处相遇?有什么建议吗?(我希望按钮位于卡片的中间)
尝试过:我尝试过在Row小部件中添加填充或换行,但没有效果。

child: new Container(
            height: 150.0,
            width: MediaQuery.of(context).size.width,
            child: new Card(
              color: Colors.white,
              elevation: 8.0,
              child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Column(
                      children: [
                        IconButton(
                            onPressed: () {},
                            icon: Icon(Icons.sticky_note_2),
                            iconSize: 35,
                            color: Colors.lightBlue),
                        Text(
                          "Notes",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                          ),
                        ),
                      ],
                    ),
                    Column(
                      children: [
                        IconButton(
                          onPressed: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => QuizRoute()),
                            );
                          },
                          icon: Icon(Icons.quiz),
                          iconSize: 35,
                        ),
                          Text(
                          "Quizzes",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                          ),
                        ),
                      ],
                    ),
                    Column(
                      children: [
                        IconButton(
                          onPressed: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => EbooksRoute()),
                            );
                          },
                          icon: Icon(Icons.auto_stories),
                          iconSize: 35,
                        ),
                          Text(
                          "Ebooks",
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 15,
                          ),
                        ),
                      ],
                    ),
                  ]),
            ),
          ),
        ),
      ],
    ),

68bkxrlz

68bkxrlz1#

尝试以下代码并将mainAxisAlignment: MainAxisAlignment.center,设置为每个Column小工具。请参阅layoutAlign

Container(
  height: 150.0,
  width: MediaQuery.of(context).size.width,
  child: Card(
    color: Colors.white,
    elevation: 8.0,
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IconButton(
                onPressed: () {},
                icon: Icon(Icons.sticky_note_2),
                iconSize: 35,
                color: Colors.lightBlue),
            Text(
              "Notes",
              style: TextStyle(
                color: Colors.black,
                fontSize: 15,
              ),
            ),
          ],
        ),
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IconButton(
              onPressed: () {},
              icon: Icon(Icons.quiz),
              iconSize: 35,
            ),
            Text(
              "Quizzes",
              style: TextStyle(
                color: Colors.black,
                fontSize: 15,
              ),
            ),
          ],
        ),
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            IconButton(
              onPressed: () {},
              icon: Icon(Icons.auto_stories),
              iconSize: 35,
            ),
            Text(
              "Ebooks",
              style: TextStyle(
                color: Colors.black,
                fontSize: 15,
              ),
            ),
          ],
        ),
      ],
    ),
  ),
),

结果屏幕-〉

zed5wv10

zed5wv102#

问题是什么?

您在row中使用了居中对齐,但在column内部没有使用。现在column取整行的高度,但column的子代没有对齐center

溶液:

尝试在每列中添加mainAxisAlignment: MainAxisAlignment.center

当前代码:
Row
mainAxisAlignment:MainAxisAlignment.center
|_Column
|_Column
|_Column
所需代码:
Row
mainAxisAlignment:MainAxisAlignment.center
|_Column
  mainAxisAlignment:MainAxisAlignment.center 👈 Add these to every column
|_Column
  mainAxisAlignment:MainAxisAlignment.center
|_Column
  mainAxisAlignment:MainAxisAlignment.center

有关完整代码,请参阅:@拉文德拉的回答

r1zhe5dt

r1zhe5dt3#

用下面的代码替换你的"Row"小部件,这将解决你的问题。

    • 溶液**:让您的列居中对齐

主轴对准:www.example.comMainAxisAlignment.center

child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                      onPressed: () {},
                      icon: const Icon(Icons.sticky_note_2),
                      iconSize: 35,
                      color: Colors.lightBlue),
                  const Text(
                    "Notes",
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 15,
                    ),
                  ),
                ],
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                    onPressed: () {
                      // Navigator.push(
                      //   context,
                      //   MaterialPageRoute(
                      //       builder: (context) => QuizRoute()),
                      // );
                    },
                    icon: const Icon(Icons.quiz),
                    iconSize: 35,
                  ),
                  const Text(
                    "Quizzes",
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 15,
                    ),
                  ),
                ],
              ),
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                    onPressed: () {
                      // Navigator.push(
                      //   context,
                      //   MaterialPageRoute(
                      //       builder: (context) => EbooksRoute()),
                      // );
                    },
                    icon: const Icon(Icons.auto_stories),
                    iconSize: 35,
                  ),
                  const Text(
                    "Ebooks",
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 15,
                    ),
                  ),
                ],
              ),
            ]),

相关问题