【问题标题】:Why RaisedButton and TextField do not inherit the dimensions of the parent Container widget为什么 RaisedButton 和 TextField 不继承父 Container 小部件的尺寸
【发布时间】:2020-09-10 10:11:22
【问题描述】:

为什么 RaisedButton 和 TextField 不继承父 Container 小部件的尺寸。 我希望能够准确地确定按钮和输入的大小。

Draw raisedButton and

我的两个小部件输入和按钮被初始化的行

          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              input(Icon(Icons.lock), 'Токен бота', _botTokenController, false,
                  widthPercent: 80,
                  height: 35,
                  context: context,
                  paddingLeft: 50,
                  paddingRight: 0),
              button('Добавить', () => {print('hello world')},
                  paddingLeft: 0,
                  widthPercent: 10,
                  context: context,
                  height: 35,
                  fontWeight: FontWeight.normal,
                  fontColor: Color.fromRGBO(0, 51, 103, 1),
                  fontSize: 14),
            ],
          ),

输入小部件(文本字段)

Widget input(
  Icon icon,
  String hint,
  TextEditingController controller,
  bool obscure, {
  double height = 50,
  double width = 460,
  double paddingLeft = 20,
  double paddingRight = 20,
  BuildContext context,
  double borderRadius = 0,
  double widthPercent = 0,
  double fontSize = 16,
}) {
  if (context != null) {
    if (widthPercent > 0 && widthPercent <= 100) {
      width = widthPercent / 100 * MediaQuery.of(context).size.width;
    } else {
      width = MediaQuery.of(context).size.width;
    }
  }

  return Container(
    width: width,
    height: height,
    padding: EdgeInsets.only(left: paddingLeft, right: paddingRight),
    alignment: Alignment.topLeft,
    color: Colors.amber,
    child: TextField(
      controller: controller,
      obscureText: obscure,
      style: TextStyle(fontSize: fontSize, color: textPrimaryColor),
      decoration: InputDecoration(
          border: OutlineInputBorder(),
          isDense: true, // Added this
          contentPadding: EdgeInsets.all(8),
          hintStyle: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: fontSize,
              color: Colors.black54),
          hintText: hint,
          focusedBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(borderRadius),
              borderSide: BorderSide(color: Colors.black87, width: 2)),
          enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(borderRadius),
              borderSide: BorderSide(color: Colors.black54, width: 1)),
          prefixIcon: Padding(
            padding: EdgeInsets.only(left: 10, right: 10),
            child: IconTheme(
              data: IconThemeData(color: Colors.black54),
              child: icon,
            ),
          )),
    ),
  );
}

按钮小部件(RaisedButton)

Widget button(String label, void func(),
    {BuildContext context = null,
    double borderRadius = 0,
    double paddingLeft = 20,
    double paddingRight: 20,
    double width = 460,
    double widthPercent = 0,
    double height = 50,
    double fontSize = 16,
    Color bacground = buttonPrimaryColor,
    FontWeight fontWeight = FontWeight.bold,
    Color fontColor = textPrimaryColor}) {
  if (context != null) {
    if (widthPercent > 0 && widthPercent <= 100) {
      width = widthPercent / 100 * MediaQuery.of(context).size.width;
    }
  }

  return Container(
    width: width,
    height: height,
    padding: EdgeInsets.only(left: paddingLeft, right: paddingRight),
    margin: EdgeInsets.only(left: 5),
    alignment: Alignment.topLeft,
    color: Colors.amber,
    child: RaisedButton(
      onPressed: () {
        func();
      },
      padding: EdgeInsets.only(left: paddingLeft, right: paddingRight),
      highlightColor: bacground,
      color: bacground,
      child: Text(label,
          style: TextStyle(
              fontWeight: fontWeight, color: fontColor, fontSize: fontSize)),
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(borderRadius)),
    ),
  );
}

【问题讨论】:

  • 用户 Flexible() 小部件作为它的父级。

标签: flutter dart flutter-web


【解决方案1】:

试试FractionallySizedBox,它会根据父尺寸调整小部件的大小,

FractionallySizedBox(
           heightFactor: 0.7,//70% height of your parent widget
           widthFactor: 0.6,//60% width of your parent widget
         )

【讨论】:

    【解决方案2】:

    移除多余的填充物或将填充物带到容器的一侧。删除以下代码。

    按钮:

      Widget button(
      String label,
      void func(), {
      BuildContext context = null,
      double borderRadius = 0,
      double paddingLeft = 20,
      double paddingRight: 20,
      double width = 460,
      double widthPercent = 0,
      double height = 50,
      double fontSize = 16,
      FontWeight fontWeight = FontWeight.bold,
    }) {
      if (context != null) {
        if (widthPercent > 0 && widthPercent <= 100) {
          width = widthPercent / 100 * MediaQuery.of(context).size.width;
        }
      }
    
      return Container(
        width: width,
        height: height,
        margin: EdgeInsets.only(left: 5),
        alignment: Alignment.topLeft,
        color: Colors.amber,
        child: RaisedButton(
          onPressed: () {
            func();
          },
          padding: EdgeInsets.only(left: paddingLeft, right: paddingRight),
          highlightColor: Colors.red,
          color: Colors.red,
          child: Text(label,
              style: TextStyle(
                  fontWeight: fontWeight, color: Colors.red, fontSize: fontSize)),
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(borderRadius)),
        ),
      );
    }
    

    输入:

    Widget input(
      Icon icon,
      String hint,
      TextEditingController controller,
      bool obscure, {
      double height = 50,
      double width = 460,
      double paddingLeft = 20,
      double paddingRight = 20,
      BuildContext context,
      double borderRadius = 0,
      double widthPercent = 0,
      double fontSize = 16,
    }) {
      if (context != null) {
        if (widthPercent > 0 && widthPercent <= 100) {
          width = widthPercent / 100 * MediaQuery.of(context).size.width;
        } else {
          width = MediaQuery.of(context).size.width;
        }
      }
    
      return Container(
        width: width,
        height: height,
        alignment: Alignment.topLeft,
        color: Colors.amber,
        child: TextField(
          controller: controller,
          obscureText: obscure,
          style: TextStyle(fontSize: fontSize),
          decoration: InputDecoration(
              border: OutlineInputBorder(),
              isDense: true, // Added this
              contentPadding: EdgeInsets.all(8),
              hintStyle: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: fontSize,
                  color: Colors.black54),
              hintText: hint,
              focusedBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(borderRadius),
                  borderSide: BorderSide(color: Colors.black87, width: 2)),
              enabledBorder: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(borderRadius),
                  borderSide: BorderSide(color: Colors.black54, width: 1)),
              prefixIcon: Padding(
                padding: EdgeInsets.only(left: 10, right: 10),
                child: IconTheme(
                  data: IconThemeData(color: Colors.black54),
                  child: icon,
                ),
              )),
        ),
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 2010-09-09
      • 2015-03-09
      • 2019-04-18
      • 2012-02-17
      相关资源
      最近更新 更多