flutter - 如何在 Flutter 中将 AlertDialog FlatButton 居中
问题描述
如何在无需创建自定义 AlertDialog 的情况下将 FlatButton 设置为居中对齐?
AlertDialog(
title: Text(
'Alert Dialog'.toUpperCase(),
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 28.0),
),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Scrollable AlertDialog' * 100),
],
),
),
actions: <Widget>[
FlatButton(
child: Text(
'Accept'.toUpperCase(),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
我尝试删除“操作”并将 FlatButton 添加到“内容”的一行中,但滚动停止工作并且内容溢出。
content: Column(
children: <Widget>[
SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text('Scrollable AlertDialog.' * 50),
],
),
),
Row(
children: <Widget>[
FlatButton(
child: Text(
'Accept'.toUpperCase(),
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
fontSize: 16.0
),
),
onPressed: () {
Navigator.of(context).pop();
},
),
],
),
],
),
解决方案
谷歌的材料设计指南不希望你把它放在中心,这就是为什么 Flutter 团队使用ButtonBar
并且actions
你无法控制属性ButtonBar
内部的对齐actions
,所以如果你真的想将你的单个按钮居中,有 2 个简单的解决方法:
在源代码中进行更改,在
dialog.dart
'AlertDialog
sbuild
方法中,您会看到一个ButtonBar
, 小部件,添加alignment
到它。ButtonBar( alignment: MainAxisAlignment.center, // add this line // ... )
您可以使用类似的虚拟小部件
SizedBox
并为其提供一些宽度。actions: [ SizedBox(width: space), FlatButton( onPressed: () {}, child: Text('Button'), ), SizedBox(width: space), ]
截屏:
推荐阅读
- python - Python Flask 生成文本文件
- azure - Azure/Microsoft.Automation AutomationAccounts/schedules 间隔属性示例
- r - 创建包含缺失值行位置的数据框
- reactjs - React-如何在另一个反应组件中使用 GeoLocation 类
- neural-network - 如何为神经网络提供输出,以使用高斯策略方法对连续动作进行采样?
- node.js - react-scripts 使用旧版本的 node 和带有 SSL 的 npm 开始崩溃
- r - 取消列出数据框列表并在循环中分配名称 - R
- amazon-web-services - 如何在 aws serverless 上安排按需任务
- sql - 如何修复单行子查询在选择查询中返回多行问题以便以所需的方式获得结果?
- excel - 从 VBA 中的非组织数据中创建一个计数不同的数据透视表