我正在使用https://pub.dev/packages/sliding_up_panel我想在现有面板上按下按钮后触发新面板的打开。
@override
Widget build(BuildContext context) {
BorderRadiusGeometry radius = BorderRadius.only(
topLeft: Radius.circular(24.0),
topRight: Radius.circular(24.0),
);
return Scaffold(
appBar: AppBar(
title: Text("SlidingUpPanelExample"),
),
body: SlidingUpPanel(
panel: Center(
child: RaisedButton(
child: Text('Show new panel'),
onPressed: () {
**///want to add code
here to trigger the callback of the new panel.**
},
),
),
collapsed: Container(
decoration: BoxDecoration(
color: Colors.blueGrey,
borderRadius: radius
),
child: Center(
child: Text(
"This is the collapsed Widget",
style: TextStyle(color: Colors.white),
),
),
),
body: Center(
child: Text("This is the Widget behind the sliding panel"),
),
borderRadius: radius,
),
);
}
此依赖项能够在 Controller 的帮助下调用 open() 方法,但我无法理解如何用它调用新的向上滑动面板(并且显然关闭现有面板)。 这是 open() 操作的代码:
PanelController _pc = new PanelController();
...
RaisedButton(
child: Text("Open"),
onPressed: () => _pc.open(),
),
最佳答案
您可以复制粘贴运行下面的完整代码
您可以使用 Column
并将这两个 SlidingUpPanel
与 Visibility
包装起来,并设置 maintainState
、maintainAnimation
> 为真
工作演示显示 SlidingUpPanel
1 和 2 之间的切换
代码片段
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Visibility(
maintainState: true,
maintainAnimation: true,
visible: _visible,
child: SlidingUpPanel(
controller: _pc1,
panel: Center(
child: RaisedButton(
child: Text('Show new panel 2'),
onPressed: () {
_pc1.close();
_visible = false;
setState(() {});
_pc2.open();
},
工作演示
完整代码
import 'package:flutter/material.dart';
import 'package:sliding_up_panel/sliding_up_panel.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
PanelController _pc1 = new PanelController();
PanelController _pc2 = new PanelController();
bool _visible = true;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
BorderRadiusGeometry radius = BorderRadius.only(
topLeft: Radius.circular(24.0),
topRight: Radius.circular(24.0),
);
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Visibility(
maintainState: true,
maintainAnimation: true,
visible: _visible,
child: SlidingUpPanel(
controller: _pc1,
panel: Center(
child: RaisedButton(
child: Text('Show new panel 2'),
onPressed: () {
_pc1.close();
_visible = false;
setState(() {});
_pc2.open();
},
),
),
collapsed: Container(
decoration:
BoxDecoration(color: Colors.blueGrey, borderRadius: radius),
child: Center(
child: Text(
"Panel 1",
style: TextStyle(color: Colors.orange),
),
),
),
body: Center(
child: Text(
"Panel 1 This is the Widget behind the sliding panel")),
borderRadius: radius,
),
),
Visibility(
maintainState: true,
maintainAnimation: true,
visible: !_visible,
child: SlidingUpPanel(
controller: _pc2,
panel: Center(
child: RaisedButton(
child: Text('Show new panel 1'),
onPressed: () {
_pc2.close();
_visible = true;
setState(() {});
_pc1.open();
},
),
),
collapsed: Container(
decoration:
BoxDecoration(color: Colors.blueGrey, borderRadius: radius),
child: Center(
child: Text(
"Panel 2 ",
style: TextStyle(color: Colors.red),
),
),
),
body: Center(
child:
Text("Panel 2 This is the Widget behind the sliding panel"),
),
borderRadius: radius,
),
),
],
),
);
}
}
关于user-interface - Flutter中如何调用上滑面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61216330/