gpt4 book ai didi

flutter - AppBar flutter 中 IconButton 内的 ExpansionTile

转载 作者:行者123 更新时间:2023-12-05 06:57:42 26 4
gpt4 key购买 nike

我尝试在我的 AppBar 小部件上单击 IconButton 后构建折叠 View ,我使用了 ExpansionTile 但在我之后没有任何反应单击 IconButton

     appBar: AppBar(
actions: <Widget>[

IconButton(
onPressed: () {

ExpansionTile(
title: Text(
"Settings",
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold
),
),
);

},
icon: Icon(Icons.settings),
),

],
),

我的代码写对了吗,还是应该考虑重构它。提前致谢!

最佳答案

是的,您需要重构代码,因为您不能通过这种方式直接从函数中将小部件插入到小部件树中。我不确定您要做什么,所以我做了两个假设(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),
),
);
}
}

关于flutter - AppBar flutter 中 IconButton 内的 ExpansionTile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64860497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com