简体   繁体   English

如何更改 Flutter TextButton 高度?

[英]How to change the Flutter TextButton height?

This is the output:这是 output:

在此处输入图像描述

  1. I try to make an app but when I use the TextButton , I get the space between two Buttons我尝试制作一个应用程序,但是当我使用TextButton时,我得到了两个 Buttons 之间的空间
  2. I need one by one without space我需要一个一个没有空间
  3. If I use the Expanded Widget, ScrollChildView doesn't work如果我使用Expanded小部件, ScrollChildView不起作用
  4. I try but I can't clear this stuff.我尝试但我无法清除这些东西。
  5. I try to make this type of TextButton .我尝试制作这种类型的TextButton

Anyone know or have any idea about this?有人知道或对此有任何想法吗?

    import "package:flutter/material.dart";
    import 'package:audioplayers/audio_cache.dart';

    class Account extends StatefulWidget {
    Account({Key key}) : super(key: key);

    @override
    _AccountState createState() => _AccountState();
    }

    class _AccountState extends State<Account> {
    @override
    Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Stack(
            children: [
              Container(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: [
                    TextButton(
                      child: Container(
                        child: Text(
                          'One',
                          style: TextStyle(color: Colors.white, fontSize: 10),
                        ),
                      ),
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all<Color>(Colors.red),
                      ),
                      onPressed: () {
                        final player = AudioCache();
                        player.play('note1.wav');
                      },
                    ),
                    SizedBox(
                      height: 1,
                    ),
                    TextButton(
                      child: Container(
                        child: Text(
                          'Two',
                          style: TextStyle(color: Colors.white, fontSize: 10),
                        ),
                      ),
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all<Color>(Colors.green),
                      ),
                      onPressed: () {
                        final player = AudioCache();
                        player.play('note2.wav');
                      },
                    ),
                    TextButton(
                      child: Container(
                        child: Text(
                          'Three',
                          style: TextStyle(color: Colors.white, fontSize: 10),
                        ),
                      ),
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all<Color>(Colors.blue),
                      ),
                      onPressed: () {
                        final player = AudioCache();
                        player.play('note3.wav');
                      },
                    ),
                    TextButton(
                      child: Container(
                        child: Text(
                          'Four',
                          style: TextStyle(color: Colors.white, fontSize: 10),
                        ),
                      ),
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all<Color>(Colors.grey),
                      ),
                      onPressed: () {
                        final player = AudioCache();
                        player.play('note4.wav');
                      },
                    ),
                    TextButton(
                      child: Container(
                        child: Text(
                          'Five',
                          style: TextStyle(color: Colors.white, fontSize: 10),
                        ),
                      ),
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all<Color>(Colors.purple),
                      ),
                      onPressed: () {
                        final player = AudioCache();
                        player.play('note5.wav');
                      },
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
    }

     }

You just wrap your text button with the SizedBox and set height and width as follows:您只需用SizedBox包裹您的文本按钮并设置高度和宽度,如下所示:

SizedBox(
  height: 30,
  width: 150,
  child: TextButton(...),
)
  • Full available height:全可用高度:

     SizedBox( height: double.infinity, // <-- match_parent child: TextButton(...) )
  • Specific height:具体高度:

     SizedBox( height: 100, // <-- Your height child: TextButton(...) )

In Flutter 2.0, you can set the height of the TextButton directly without depending on other widgets by changing the ButtonStyle.fizedSize :在 Flutter 2.0 中,您可以通过更改ButtonStyle.fizedSize直接设置TextButton的高度,而无需依赖其他小部件:

TextButton(
  child: Text('Text Button'),
  style: TextButton.styleFrom(fixedSize: Size.fromHeight(150)),
),

If you want to modify all TextButton s, put it in the ThemeData like below:如果要修改所有TextButton ,请将其放在ThemeData中,如下所示:

return MaterialApp(
  theme: ThemeData(
    textButtonTheme: TextButtonThemeData(
      style: TextButton.styleFrom(fixedSize: Size.fromHeight(150)),
    ),
  ),

Live Demo 现场演示

use the following to even add your preferred size as well使用以下甚至添加您喜欢的尺寸

N/B: child sized box is the main child widget inside Padding widget N/B:子大小的框是 Padding 小部件内的主要子小部件

Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(30.0),
                      child: SizedBox(
                        height: 60,
                        width: 200,
                        child: ElevatedButton.icon(
                          onPressed: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => RegistrationMenu()));
                          },
                          style: ButtonStyle(
                            backgroundColor:
                                MaterialStateProperty.all(Colors.red.shade800),
                          ),
                          icon: Icon(Icons.person_add_alt_1_rounded, size: 18),
                          label: Text("Register Users"),
                        ),
                      ),
                    ),
                  ],
                ),

Wrap the TextButton in an "Expanded" Widget.将 TextButton 包装在“扩展的”小部件中。

          Expanded(
            child: TextButton(
              style: TextButton.styleFrom(
                backgroundColor: Colors.red,
                padding: EdgeInsets.zero,
              ),
              child: const Text(''),
              onPressed: () {
                playSound(1);
              },
            ),
          ),

Another solution would be wrapping with ConstrainedBox and using minWidth & minHeight另一种解决方案是使用ConstrainedBox包装并使用minWidthminHeight

properties.特性。


ConstrainedBox(
  constraints:BoxConstraints(
    minHeight:80,
    minWidth:200
    ),
  child:TextButton(..)
)

it's simple这很简单

TextButton(
    style: ButtonStyle(
      fixedSize: MaterialStateProperty.all<Size>(
        // width : double.maxFinite
        // height: 56.0
        const Size(double.maxFinite, 56.0),
      
      ),
       // fill color
      backgroundColor: MaterialStateProperty.all<Color>(
      Theme.of(context).primaryColorDark,
      ),

    ),
    onPressed: (){
      // todo: action
    },
    child: Text(
      'button name',
    ),
  );

TextButton(文本按钮(
onPressed: _submitOrder, onPressed: _submitOrder,
child: Padding(孩子:填充(
padding: EdgeInsets.only(left: 20, right: 20),填充:EdgeInsets.only(左:20,右:20),
child: Text("Raise")),孩子:文本(“提高”)),
style: ButtonStyle(样式:按钮样式(
shape: MaterialStateProperty.all(形状:MaterialStateProperty.all(
const RoundedRectangleBorder(常量圆角矩形边框(
borderRadius: BorderRadius.all(Radius.circular(50)),边界半径:边界半径.all(半径.圆形(50)),
side: BorderSide(color: Colors.green)))),边:BorderSide(颜色:Colors.green)))),
) )

TextButton(
  style: ButtonStyle(
    tapTargetSize: MaterialTapTargetSize.shrinkWrap
  ),
  child: Text(''),
);

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

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