【发布时间】:2026-01-23 12:45:01
【问题描述】:
我正在尝试执行以下操作: 我想在更改 appBar 本身的大小时动态更改操作 [...] 中的 appBar 标题和小部件的大小。 我想将标题的宽度设置为 75%,将操作中小部件(凸起的按钮)的宽度设置为 appBar 本身宽度的 30%,并且当 appBar 的大小发生更改时(例如,当我制作它时)用鼠标拉动应用程序窗口时更大或更小),我希望标题的大小(在这种情况下为宽度)和提到的小部件(凸起的按钮)相应地改变。
我尝试使用:
- 孩子:按钮主题( minWidth: MediaQuery.of(context).size.width, 高度:MediaQuery.of(context).size.height, ),
- minWidth: AppBar().preferredSize.width
- 灵活的小部件
- 扩展的小部件
- FractionallySizedBox。 ...等c
但我无法实现上述行为。
考虑最简单的 appBar 示例:
/// Flutter code sample for AppBar
import 'package:flutter/material.dart';
void main() => runApp(const AlbertsApp());
/// This is the main application widget.
class AlbertsApp extends StatelessWidget {
const AlbertsApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatelessWidget(),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
const MyStatelessWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Alberts trial AppBar'),
actions : <Widget> [
RaisedButton (
child: Text('Hi'),
color: Colors.red,
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('This is a TonysChoocolonely')));
},
),
RaisedButton (
child: Text('Bye'),
color: Colors.green,
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('This is a KitKat')));
},
),
RaisedButton (
child: Text('Third'),
color: Colors.yellow,
onPressed: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('This is a PeanutButter')));
},
),
],
),
body: const Center(
child: Text(
'This Is The Home Page Itself',
style: TextStyle(fontSize: 24),
),
),
);
}
}
在任何时候,当 appBar 的大小发生变化时,我希望标题和凸起按钮的大小相应地发生变化。 例如,在任何时候,当 appBar 宽度变为“x”时,我希望标题宽度变为“x * 70 / 100”,并且每个凸起按钮的宽度变为“x * 30 / 100”。
【问题讨论】:
-
你能分享你的代码吗?
-
@Diwyansh 感谢您的回复。我无法添加代码本身,但您可以考虑最简单的 appBar 示例。我已经用一个更新了这个问题。
-
你能澄清一下你需要title(75%)+action(30%+30%+30%) 你的示例代码中有这些按钮吗
标签: flutter