Flutter AppBar颤振中图标按钮内的ExpansionFile
在我的Flutter AppBar颤振中图标按钮内的ExpansionFile,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,在我的AppBar小部件上单击IconButton后,我试图构建一个折叠视图,我使用了expansionfile,但在单击IconButton后什么也没有发生 appBar: AppBar( actions: <Widget>[ IconButton( onPressed: () { ExpansionTile( title: T
AppBar
小部件上单击IconButton
后,我试图构建一个折叠视图,我使用了expansionfile
,但在单击IconButton
后什么也没有发生
appBar: AppBar(
actions: <Widget>[
IconButton(
onPressed: () {
ExpansionTile(
title: Text(
"Settings",
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold
),
),
);
},
icon: Icon(Icons.settings),
),
],
),
appBar:appBar(
行动:[
图标按钮(
已按下:(){
扩展文件(
标题:正文(
“设置”,
样式:TextStyle(
字体大小:18.0,
fontWeight:fontWeight.bold
),
),
);
},
图标:图标(图标.设置),
),
],
),
我是否编写了代码,或者我应该考虑重构它。提前谢谢 是的,您需要重构代码,因为您不能通过这种方式直接从函数将小部件插入到小部件树中。我不确定你想做什么,所以我做了两个假设(1)你想在应用程序栏中显示扩展磁贴,或者(2)你想显示弹出菜单。请参阅下面的代码,了解如何实现这两个目标
import 'package:flutter/material.dart';
final Color darkBlue = const Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
title: 'Welcome to Flutter',
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Widget myWidget = Container(
child: const Text("Flutter"),
);
String myText = 'Hello, World!';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: myWidget, actions: [
IconButton(
onPressed: () async {
print(context);
showMenu(
context: context,
position: const RelativeRect.fromLTRB(110.0, 80.0, 0.0, 0.0),
items: ["One", "Two", "Three", "Four"]
.map(
(value) => PopupMenuItem<String>(
child: Text(value),
value: value,
),
)
.toList(),
elevation: 8.0,
);
},
icon: Icon(Icons.settings),
),
PopupMenuButton<int>(
onSelected: (selected) {
setState(
() {
myText = "You selected the menu number $selected";
},
);
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<int>>[
const PopupMenuItem<int>(
value: 1,
child: Text('First Item'),
),
const PopupMenuItem<int>(
value: 2,
child: Text('Second Item'),
),
const PopupMenuItem<int>(
value: 3,
child: Text('Thrid Item'),
),
const PopupMenuItem<int>(
value: 4,
child: Text('Fourth Item'),
),
],
)
]),
body: Center(
child: Text(myText, style: Theme.of(context).textTheme.headline4),
),
);
}
}
导入“包装:颤振/材料.省道”;
最终颜色暗蓝色=常量颜色。来自argb(255,18,32,47);
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主题:ThemeData.dark().copyWith(脚手架背景颜色:深蓝色),
标题:“欢迎来到弗利特”,
debugShowCheckedModeBanner:false,
主页:主页(),
);
}
}
类主页扩展了StatefulWidget{
@凌驾
_HomePageState createState()=>\u HomePageState();
}
类_HomePageState扩展状态{
Widget myWidget=容器(
子:常量文本(“颤振”),
);
String myText='Hello,World!';
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:myWidget,操作:[
图标按钮(
onPressed:()异步{
印刷(上下文);
展示菜单(
上下文:上下文,
位置:常数从LTRB(110.0,80.0,0.0,0.0),
项目:[“一”、“二”、“三”、“四”]
.地图(
(值)=>PopupMenuItem(
子项:文本(值),
价值:价值,
),
)
.toList(),
标高:8.0,
);
},
图标:图标(图标.设置),
),
弹出菜单按钮(
当选:(已选){
设定状态(
() {
myText=“您选择了菜单号$selected”;
},
);
},
itemBuilder:(构建上下文)=>[
常数PopupMenuItem(
价值:1,
子项:文本(“第一项”),
),
常数PopupMenuItem(
价值:2,
子项:文本(“第二项”),
),
常数PopupMenuItem(
价值:3,
子项:文本(“第三项”),
),
常数PopupMenuItem(
价值:4,
子项:文本(“第四项”),
),
],
)
]),
正文:中(
子:文本(myText,样式:Theme.of(context.textTheme.headline4),
),
);
}
}
先生,谢谢您。我需要这个弹出式按钮。只有一个问题,单击按钮后,是否有任何属性可以修改PopupMenuItem
列表的位置?我想把它放在AppBar
区域下面。我已经更新了代码。单击设置图标以显示菜单。这部分>>>相对论。从LTRB(110.0,80.0,0.0,0.0)如果答案解决了您的问题,请接受答案。谢谢