简体   繁体   English

如何将我的文本对齐到按钮内?

[英]How do i align my text to be inside a button?

i need help with my app since iam new to flutter.我的应用程序需要帮助,因为我是 flutter 的新手。 so i have a button that suppossed to have a text inside it, but when i run my app, the text isnt inside the button, i dont know how to fix this.所以我有一个按钮,里面应该有一个文本,但是当我运行我的应用程序时,文本不在按钮内,我不知道如何解决这个问题。 so here is my app when i run it:所以这是我运行时的应用程序:

在此处输入图像描述

i use MyButton for the button and text, here is my botton code我使用 MyButton 作为按钮和文本,这是我的按钮代码


_addTaskBar(){
    return Container(
            margin: const EdgeInsets.only(left: 20, right: 20, top: 5),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  margin: const EdgeInsets.symmetric(horizontal: 20),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(DateFormat.yMMMMd().format(DateTime.now()),
                      style: subHeadingStyle,
                      ),
                      Text("Today", 
                      style: headingStyle,
                      )
                    ],
                  ),
                ),
                MyButton(label: "Add Reminder", onTap: ()=>Get.to(AddReminderPage()))
              ],
            ),
          );
  }

here is my MyButton code这是我的 MyButton 代码

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:medreminder/Reminder/ui/theme.dart';

class MyButton extends StatelessWidget {
  final String label;
  final Function()? onTap;
  const MyButton({super.key, required this.label, required this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child:Container(
        width: 100,
        height: 50,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          color: Color(0xFFFB7B8E),
        ),
        child: Text(
          label,
          style: TextStyle(
            color: Colors.white,
          ),
        ),
      ) ,
    );
  }
}

any help would mean so much to me.任何帮助对我来说意义重大。 thankyou谢谢你

Your text is now aligned with the left side of the container.您的文本现在与容器的左侧对齐。 It only looks outside because you have a rounded button, but the text is in fact exactly inside the container.它只是看起来外面,因为你有一个圆形按钮,但文本实际上恰好在容器内。 What you might want is center the text.您可能想要的是将文本居中。 To center it horizontally in the column, replace crossAxisAlignment.start with crossAxisAlignment.center To center vertically, add a line at the below this one, with mainAxisAlignment.center要使其在列中水平居中,请将crossAxisAlignment.start替换为crossAxisAlignment.center要垂直居中,请在此行下方添加一行,并使用mainAxisAlignment.center

If the text still overflow, you might need to make the button bigger如果文本仍然溢出,您可能需要将按钮变大

If instead you would like the text to wrap, you can put it in a Flexible component, like in this question: Flutter- wrapping text相反,如果您希望文本换行,您可以将它放在一个Flexible的组件中,就像这个问题: Flutter-wrapping text

Have a look at this page on how to align elements in columns and row.查看此页面,了解如何对齐列和行中的元素。 https://docs.flutter.dev/codelabs/layout-basics https://docs.flutter.dev/codelabs/layout-basics

The Text widget in MyButton can be added to the Center widget MyButton 中的 Text 小部件可以添加到 Center 小部件中

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:medreminder/Reminder/ui/theme.dart';

class MyButton extends StatelessWidget {
  final String label;
  final Function()? onTap;
  const MyButton({super.key, required this.label, required this.onTap});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child:Container(
        width: 100,
        height: 50,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
          color: Color(0xFFFB7B8E),
        ),
        child: Center(
      child:Text(
          label,
          style: TextStyle(
            color: Colors.white,
          )),
        ),
      ) ,
    );
  }
}
Center(
child: Text("add reminder"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM