Flutter-ElevatedButton宽高设置

前言

Flutter的ElevatedButton是一个很重要的Button组件,想要设置ElevatedButton的宽高有两种方式,
1.直接通过SizeBox进行设置
2.通过ElevatedButtonstyle属性进行设置

默认情况下ElevatedButton高度是固定的,宽度会跟随child内容的大小自动调整。

我们将新建Flutter项目时的默认模板稍微改一下来作为我们本篇文章的demo模板。

dart 复制代码
void main() {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(onPressed: () {}, child: const Text("默认情况下的宽高")),
            ],
          ),
        ),
      ),
    );
  }
}

设置固定宽高

使用SizeBox

直接使用SizeBox来设置宽高跟其他的控件方式是一致的,不赘述。

dart 复制代码
SizedBox(
  width: 330,
  height: 50,
  child: ElevatedButton(
    onPressed: () {},
    child: const Text("设置固定宽高(使用SizeBox)"),
  ),
),

使用ElevatedButtonstyle属性

直接使用ButtonStyle

ElevatedButton有一个style参数,可以设置Button相关的外观。style类型为ButtonStyle?,默认为null

style里设置了相应的参数之后,会有最高优先级。

设置Button大小的参数有3个,minimumSize(最小的Size),maximumSize(最大的Size),fixedSize(固定的Size)。

一般来说,我们设置fixedSize就可以了。

dart 复制代码
ElevatedButton(
  onPressed: () {},
  style: ButtonStyle(
    fixedSize: MaterialStateProperty.all(const Size(330, 50)),
  ),
  child: const Text("设置固定宽高(使用ButtonStyle)"),
),

使用ElevatedButton.styleFrom

上面使用ButtonStyle有一个比较坑爹的地方,就是ButtonStyle的属性大多是MaterialStateProperty类型,需要使用MaterialStateProperty.all()封装后才能使用。所以用起来比较麻烦。使用ElevatedButton.styleFrom()的方式来生成ButtonStyle会更加方便,因为Flutter已经为我们做了MaterialStateProperty封装了,我们直接设置就可以。

dart 复制代码
ElevatedButton(
  onPressed: () {},
  style: ElevatedButton.styleFrom(
    fixedSize: const Size(330, 50),
  ),
  child: const Text("设置固定宽高(使用ElevatedButton.styleFrom)"),
),

只设置宽度,高度默认

使用SizeBox

dart 复制代码
SizedBox(
  width: 330,
  child: ElevatedButton(
    onPressed: () {},
    child: const Text("只设置宽度,高度默认(SizeBox)"),
  ),
),

使用ElevatedButtonstyle属性

这个只需要设置Sizewidth,而height设置为double.infinity即可。

Size.fromWidth(330)Size(330, double.infinity)是一样的效果。

less 复制代码
ElevatedButton(
  onPressed: () {},
  style: ElevatedButton.styleFrom(
    fixedSize: const Size.fromWidth(330),
  ),
  child: const Text("只设置宽度,高度默认(style)"),
),

只设置高度,宽度默认

使用SizeBox

dart 复制代码
SizedBox(
  height: 50,
  child: ElevatedButton(
    onPressed: () {},
    child: const Text("只设置高度,宽度默认(SizeBox)"),
  ),
),

使用ElevatedButtonstyle属性

dart 复制代码
ElevatedButton(
  onPressed: () {},
  style: ElevatedButton.styleFrom(
    fixedSize: const Size.fromHeight(50),
  ),
  child: const Text("只设置高度,宽度默认(style)"),
),

宽度占满,高度默认

使用SizeBox

dart 复制代码
SizedBox(
  width: double.infinity,
  child: ElevatedButton(
    onPressed: () {},
    child: const Text("宽度占满,高度默认(SizeBox)"),
  ),
),

使用ElevatedButtonstyle属性

这个不能直接将width设置成double.infinity,因为设置之后是没有效果的。

正确的方式是设置一个较大的width,要大于占满时的宽度。

dart 复制代码
SizedBox(
  width: double.infinity,
  child: ElevatedButton(
    onPressed: () {},
    child: const Text("宽度占满,高度默认(SizeBox)"),
  ),
),

去掉Button边距

默认情况下ElevatedButton的内容是有边距的。如果不想要这个边距,可以设置下述的方式进行处理。

dart 复制代码
ElevatedButton(
  onPressed: () {},
  style: ElevatedButton.styleFrom(
    minimumSize: Size.zero,
    padding: EdgeInsets.zero,
    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
  ),
  child: const Text("去掉Button边距"),
),

Demo

dart 复制代码
import 'package:flutter/material.dart';
 
void main() {
  runApp(const MyApp());
}
 
class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
 
class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
 
  final String title;
 
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
 
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const SizedBox(height: 3),
              ElevatedButton(onPressed: () {}, child: const Text("默认情况下的宽高")),
              const SizedBox(height: 3),
              SizedBox(
                width: 330,
                height: 50,
                child: ElevatedButton(
                  onPressed: () {},
                  child: const Text("设置固定宽高(使用SizeBox)"),
                ),
              ),
              const SizedBox(height: 3),
              ElevatedButton(
                onPressed: () {},
                style: ButtonStyle(
                  fixedSize: MaterialStateProperty.all(const Size(330, 50)),
                ),
                child: const Text("设置固定宽高(使用ButtonStyle)"),
              ),
              const SizedBox(height: 3),
              ElevatedButton(
                onPressed: () {},
                style: ElevatedButton.styleFrom(
                  fixedSize: const Size(330, 50),
                ),
                child: const Text("设置固定宽高(使用ElevatedButton.styleFrom)"),
              ),
              const SizedBox(height: 3),
              SizedBox(
                width: 330,
                child: ElevatedButton(
                  onPressed: () {},
                  child: const Text("只设置宽度,高度默认(SizeBox)"),
                ),
              ),
              const SizedBox(height: 3),
              ElevatedButton(
                onPressed: () {},
                style: ElevatedButton.styleFrom(
                  fixedSize: const Size.fromWidth(330),
                ),
                child: const Text("只设置宽度,高度默认(style)"),
              ),
              const SizedBox(height: 3),
              SizedBox(
                height: 50,
                child: ElevatedButton(
                  onPressed: () {},
                  child: const Text("只设置高度,宽度默认(SizeBox)"),
                ),
              ),
              const SizedBox(height: 3),
              ElevatedButton(
                onPressed: () {},
                style: ElevatedButton.styleFrom(
                  fixedSize: const Size.fromHeight(50),
                ),
                child: const Text("只设置高度,宽度默认(style)"),
              ),
              const SizedBox(height: 3),
              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: () {},
                  child: const Text("宽度占满,高度默认(SizeBox)"),
                ),
              ),
              const SizedBox(height: 3),
              ElevatedButton(
                onPressed: () {},
                style: ElevatedButton.styleFrom(
                  fixedSize: const Size.fromWidth(10000),
                ),
                child: const Text("宽度占满,高度默认(style)"),
              ),
              const SizedBox(height: 3),
              ElevatedButton(
                onPressed: () {},
                style: ElevatedButton.styleFrom(
                  minimumSize: Size.zero,
                  padding: EdgeInsets.zero,
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                ),
                child: const Text("去掉Button边距"),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
相关推荐
事圆则缓18 分钟前
Java 常见数据结构与 Android 使用场景
android·java·数据结构
SoaringHeart21 分钟前
Flutter 进阶 | 组件封装:用 CustomPainter 实现光环动画组件AnimatedHalo
前端·flutter
Light Gao22 分钟前
企业级移动端 APP 架构设计:从原生双端到 Hybrid、React Native 与 Flutter
flutter·react native·react.js
RisunJan38 分钟前
Android 高频面试题与解答
android
MyBili44 分钟前
【安卓开发/搞机】快图浏览(QuickPic)技术解析:为何这款3MB应用仍是本地图库的性能天花板?
android·app·安卓·文件管理·相册·看图软件·手机相册
2501_915106321 小时前
第一次开发 iPhone App,可能碰到的问题,解决办法
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
事圆则缓1 小时前
Kotlin 高阶工程化与 Android 深入实践
android·开发语言·kotlin
zhangphil1 小时前
AI大模型生成maxTokens 与上下文context
android·llama
淡淡的香烟1 小时前
Androidiot开发之猫脸识别
android·物联网
2501_915106322 小时前
iOS数据采集技术详解:从性能监控到崩溃分析的全链路实践
android·ios·小程序·https·uni-app·iphone·webview