打开 ExpandablePanel 时如何更改 header
How can I change the header when the ExpandablePanel is opened
我想在 ExpandablePanel 打开时更改 header 部分。当我关闭它时,旧的 header 应该又来了。我如何在 Flutter 中执行此操作?
true false 值为 true 但文本未更新
return ExpandableNotifier(
controller: expandableController,
child: ExpandablePanel(
header: Padding(
padding: const EdgeInsets.only(left: 15.0, top: 15.0),
child: Text(expandableController.expanded
? "${itemCount} kalemi gizlemek için tıklayınız..."
: "${itemCount} kalem daha var görmek için tıklayınız..."),
),
collapsed: detayCard(detay, h),
expanded: detayCard(detay, maxheight),
),
);
final ExpandableController expandableController = ExpandableController();
void onExpandableControllerStateChanged() {
if (expandableController.expanded) {
print(expandableController.expanded);
} else {
print(expandableController.expanded);
}
}
@override
void initState() {
super.initState();
expandableController.addListener(onExpandableControllerStateChanged);
}
@override
void dispose() {
expandableController.removeListener(onExpandableControllerStateChanged);
super.dispose();
}
您可以尝试将 controller
添加到您的 ExpandablePanel
,每个文档的控制器都有此
/// Returns [true] if the state is expanded, [false] if collapsed.
bool get expanded => value;
这样您就可以通过检查面板的状态来更改 header
,例如
Text(controller.expanded ? 'Expanded' : 'Collapsed')
编辑:
例子
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ExpandableController _controller = ExpandableController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ExpandablePanel(
controller: _controller
..addListener(() {
setState(() {});
}),
header: Text(
_controller.expanded ? 'Header expanded' : 'Header collapsed',
key: ValueKey(_controller.expanded),
),
expanded: Container(
width: MediaQuery.of(context).size.width,
height: 250,
color: Colors.green,
),
collapsed: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.red,
),
),
),
);
}
}
以上结果:
我想在 ExpandablePanel 打开时更改 header 部分。当我关闭它时,旧的 header 应该又来了。我如何在 Flutter 中执行此操作?
true false 值为 true 但文本未更新
return ExpandableNotifier(
controller: expandableController,
child: ExpandablePanel(
header: Padding(
padding: const EdgeInsets.only(left: 15.0, top: 15.0),
child: Text(expandableController.expanded
? "${itemCount} kalemi gizlemek için tıklayınız..."
: "${itemCount} kalem daha var görmek için tıklayınız..."),
),
collapsed: detayCard(detay, h),
expanded: detayCard(detay, maxheight),
),
);
final ExpandableController expandableController = ExpandableController();
void onExpandableControllerStateChanged() {
if (expandableController.expanded) {
print(expandableController.expanded);
} else {
print(expandableController.expanded);
}
}
@override
void initState() {
super.initState();
expandableController.addListener(onExpandableControllerStateChanged);
}
@override
void dispose() {
expandableController.removeListener(onExpandableControllerStateChanged);
super.dispose();
}
您可以尝试将 controller
添加到您的 ExpandablePanel
,每个文档的控制器都有此
/// Returns [true] if the state is expanded, [false] if collapsed.
bool get expanded => value;
这样您就可以通过检查面板的状态来更改 header
,例如
Text(controller.expanded ? 'Expanded' : 'Collapsed')
编辑:
例子
class MyHomePage extends StatefulWidget {
const MyHomePage({
Key? key,
}) : super(key: key);
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final ExpandableController _controller = ExpandableController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: ExpandablePanel(
controller: _controller
..addListener(() {
setState(() {});
}),
header: Text(
_controller.expanded ? 'Header expanded' : 'Header collapsed',
key: ValueKey(_controller.expanded),
),
expanded: Container(
width: MediaQuery.of(context).size.width,
height: 250,
color: Colors.green,
),
collapsed: Container(
width: MediaQuery.of(context).size.width,
height: 50,
color: Colors.red,
),
),
),
);
}
}
以上结果: