无法在Flutter中将包含Material、TextButton和IconButton小部件的容器居中。

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

Unable to center my container that contains Material, TextButton, and IconButton widgets in Flutter

问题

请问你需要关于代码的帮助吗?

英文:

I'm trying to make my buttons look like this:

无法在Flutter中将包含Material、TextButton和IconButton小部件的容器居中。

Currently, it's looking like this (I'm just focusing on implementing one icon and text button which are side by side correctly and then I can just copy and paste for the others):

无法在Flutter中将包含Material、TextButton和IconButton小部件的容器居中。

I'm trying to center-align the profile and icon buttons, I used TextButton and IconButton to do these, but I haven't been able to. I've also tried wrapping them in a Center widget but that didn't work. I'm assuming I organized my code and widgets wrong, here's my code:

Container( // container holding the main setting buttons
            child: Padding(
              padding: const EdgeInsets.only(top: 20, bottom: 0, left: 30, right: 30),
              child: Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.black, width: 2.5),
                  borderRadius: BorderRadius.circular(25)
                ),
                padding: const EdgeInsets.only(left: 7, top: 7, right: 10, bottom: 8),
                child: Column(
                  children: [
                    Container(
                      alignment: Alignment.center,
                      child: Material(
                        color: Colors.transparent,
                        child: Row(
                          children: [
                            IconButton( // TODO: CHANGE ICON TO EXACT FROM FIGMA
                              iconSize: 20,
                              padding: EdgeInsets.zero,
                              constraints: BoxConstraints(),
                              icon: const Icon(Icons.add_circle_outline),
                              onPressed: () {
                                // TODO: NO FUNCTIONALITY FOR BUTTON
                              },
                            ),
                            
                            TextButton(
                              
                              style: TextButton.styleFrom(
                                foregroundColor: Colors.black, // color of text
                                textStyle: const TextStyle(fontSize: 17, fontWeight: FontWeight.w500),
                              ),
                              onPressed: () {
                                
                              },
                              child: const Text("Privacy"), // TODO: CHANGE FONT
                            ),
                          ],
                        ),
                      ),
                    ),
                    
                    ElevatedButton(onPressed: () => {}, child: const Text("Terms of Service")),
                    ElevatedButton(onPressed: () => {}, child: const Text("Community Rules")),
                    ElevatedButton(onPressed: () => {}, child: const Text("Report a problem")),
                    ElevatedButton(onPressed: () => {}, child: const Text("Become a Pioneer")),
                    ElevatedButton(onPressed: () => {}, child: const Text("Log out")),
                    ElevatedButton(onPressed: () => {}, child: const Text("Manage account"))
                  ]
                ),
              ),
            ),
          ),

答案1

得分: 2

你可以只用一个ListTile来替换ElevatedButtons。

示例: 删除

ElevatedButton(onPressed: () => {}, child: const Text("Terms of Service")),

然后添加

ListTile(onTap:(){},title:Text("Terms of Service"),leading: Icon(Icons.add))
ListTile(onTap:(){},title:Text("Community Rules"),leading: Icon(Icons.add))

如果你将这些ListTiles包裹在一个SizedBox中,你可以根据需要设置宽度。

但是如果你只想要居中这些小部件:

那么Column应该具有

mainAxisAlignment: MainAxisAlignment.center

而你放置第一个按钮的那一行应该具有:

mainAxisAlignment: MainAxisAlignment.center
英文:

You can just replace the ElevatedButtons by a ListTile

Example: remove the

ElevatedButton(onPressed: () => {}, child: const Text("Terms of Service")),

then add

ListTile(onTap:(){},title:Text("Terms of Service"),leading: Icon(Icons.add))
ListTile(onTap:(){},title:Text("Community Rules" ),leading: Icon(Icons.add))

and if you wrap the ListTiles on a SizedBox you can set the width as you wish.


but if you just want to center those widgets:

then the Column should have a

> mainAxisAlignment: MainAxisAlignment.center

and the row you put the first button should have :

> mainAxisAlignment: MainAxisAlignment.center

huangapple
  • 本文由 发表于 2023年6月29日 06:06:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576975.html
匿名

发表评论

匿名网友

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

确定