Uma*_*air 0 package code-snippets dart dropdown flutter
我怎样才能制作一个像图片中显示的下拉小部件。
按下下拉按钮时,我们必须仅显示与该按钮相关的详细信息。任何人都可以分享一些存储库/dart 包或代码片段来在 flutter 中实现这一目标吗
您可以复制粘贴运行下面的完整代码
您可以使用包https://pub.dev/packages/expandable ,您可以手动设置为
或这样
您就可以实现折叠其他controllerexpandedfalsetrue
代码片段
List<ExpandableController> controllerList = [
ExpandableController(),
ExpandableController(),
ExpandableController()
];
ExpandablePanel(
controller: controllerList[2],
for (int i = 0; i < controllerList.length; i++) {
if (i == currentIndex) {
controllerList[i].expanded = true;
} else {
controllerList[i].expanded = false;
}
}
ScrollOnExpand(
scrollOnExpand: true,
scrollOnCollapse: false,
child: ExpandablePanel(
theme: const ExpandableThemeData(
...
header: Container(
color: Colors.blue,
...
collapsed: Container(),
expanded: Container(
color: Colors.white,
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
Run Code Online (Sandbox Code Playgroud)
工作演示
完整代码
import 'package:expandable/expandable.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Expandable Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State createState() {
return MyHomePageState();
}
}
class MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Expandable Demo"),
),
body: ExpandableTheme(
data: const ExpandableThemeData(
iconColor: Colors.blue,
useInkWell: true,
),
child: ListView(
physics: const BouncingScrollPhysics(),
children: <Widget>[
Card1(),
Card2(),
Card3(),
],
),
),
);
}
}
const loremIpsum =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
List<ExpandableController> controllerList = [
ExpandableController(),
ExpandableController(),
ExpandableController()
];
int currentIndex = -1;
class Card1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ExpandableNotifier(
child: Padding(
padding: const EdgeInsets.all(10),
child: Card(
color: Colors.blue,
clipBehavior: Clip.antiAlias,
child: Column(
children: <Widget>[
ScrollOnExpand(
scrollOnExpand: true,
scrollOnCollapse: false,
child: ExpandablePanel(
controller: controllerList[0],
theme: const ExpandableThemeData(
iconColor: Colors.white,
headerAlignment: ExpandablePanelHeaderAlignment.center,
tapBodyToCollapse: true,
),
header: InkWell(
onTap: () {
currentIndex = 0;
for (int i = 0; i < controllerList.length; i++) {
if (i == currentIndex) {
controllerList[i].expanded = true;
} else {
controllerList[i].expanded = false;
}
}
},
child: Container(
color: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Icon(
Icons.calendar_today,
color: Colors.white,
)),
Expanded(flex: 1, child: Container()),
Expanded(
flex: 4,
child: Text(
"15/06/2020",
style: TextStyle(color: Colors.white),
),
),
],
)),
),
),
collapsed: Container(),
expanded: Container(
color: Colors.white,
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Bill Date",
style: TextStyle(color: Colors.blue)),
Text("15/05/2020",
style: TextStyle(color: Colors.blue)),
],
),
Divider(
color: Colors.blue,
thickness: 2.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Adjustment",
style: TextStyle(color: Colors.blue),
),
Text(".00", style: TextStyle(color: Colors.blue)),
],
),
Divider(
color: Colors.blue,
thickness: 2.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Total due",
style: TextStyle(color: Colors.blue)),
Text("413.27",
style: TextStyle(color: Colors.blue)),
],
),
],
),
),
),
builder: (_, collapsed, expanded) {
return Expandable(
collapsed: collapsed,
expanded: expanded,
theme: const ExpandableThemeData(crossFadePoint: 0),
);
},
),
),
],
),
),
));
}
}
class Card2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ExpandableNotifier(
child: Padding(
padding: const EdgeInsets.all(10),
child: Card(
color: Colors.blue,
clipBehavior: Clip.antiAlias,
child: Column(
children: <Widget>[
ScrollOnExpand(
scrollOnExpand: true,
scrollOnCollapse: false,
child: ExpandablePanel(
controller: controllerList[1],
theme: const ExpandableThemeData(
iconColor: Colors.white,
headerAlignment: ExpandablePanelHeaderAlignment.center,
tapBodyToCollapse: true,
),
header: InkWell(
onTap: () {
currentIndex = 1;
for (int i = 0; i < controllerList.length; i++) {
if (i == currentIndex) {
controllerList[i].expanded = true;
} else {
controllerList[i].expanded = false;
}
}
},
child: Container(
color: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Icon(
Icons.calendar_today,
color: Colors.white,
)),
Expanded(flex: 1, child: Container()),
Expanded(
flex: 4,
child: Text(
"15/05/2020",
style: TextStyle(color: Colors.white),
),
),
],
)),
),
),
collapsed: Container(),
expanded: Container(
color: Colors.white,
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Bill Date",
style: TextStyle(color: Colors.blue)),
Text("15/06/2020",
style: TextStyle(color: Colors.blue)),
],
),
Divider(
color: Colors.blue,
thickness: 2.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Adjustment",
style: TextStyle(color: Colors.blue),
),
Text(".00", style: TextStyle(color: Colors.blue)),
],
),
Divider(
color: Colors.blue,
thickness: 2.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Total due",
style: TextStyle(color: Colors.blue)),
Text("413.27",
style: TextStyle(color: Colors.blue)),
],
),
],
),
),
),
builder: (_, collapsed, expanded) {
return Expandable(
collapsed: collapsed,
expanded: expanded,
theme: const ExpandableThemeData(crossFadePoint: 0),
);
},
),
),
],
),
),
));
}
}
class Card3 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return ExpandableNotifier(
child: Padding(
padding: const EdgeInsets.all(10),
child: Card(
color: Colors.blue,
clipBehavior: Clip.antiAlias,
child: Column(
children: <Widget>[
ScrollOnExpand(
scrollOnExpand: true,
scrollOnCollapse: false,
child: ExpandablePanel(
controller: controllerList[2],
theme: const ExpandableThemeData(
iconColor: Colors.white,
headerAlignment: ExpandablePanelHeaderAlignment.center,
tapBodyToCollapse: true,
),
header: InkWell(
onTap: () {
currentIndex = 2;
for (int i = 0; i < controllerList.length; i++) {
if(i == currentIndex) {
controllerList[i].expanded = true;
} else {
controllerList[i].expanded = false;
}
}
},
child: Container(
color: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Expanded(
flex: 1,
child: Icon(
Icons.calendar_today,
color: Colors.white,
)),
Expanded(flex: 1, child: Container()),
Expanded(
flex: 4,
child: Text(
"14/04/2020",
style: TextStyle(color: Colors.white),
),
),
],
)),
),
),
collapsed: Container(),
expanded: Container(
color: Colors.white,
child: Padding(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Bill Date",
style: TextStyle(color: Colors.blue)),
Text("15/05/2020",
style: TextStyle(color: Colors.blue)),
],
),
Divider(
color: Colors.blue,
thickness: 2.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Adjustment",
style: TextStyle(color: Colors.blue),
),
Text(".00", style: TextStyle(color: Colors.blue)),
],
),
Divider(
color: Colors.blue,
thickness: 2.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Total due",
style: TextStyle(color: Colors.blue)),
Text("413.27",
style: TextStyle(color: Colors.blue)),
],
),
],
),
),
),
builder: (_, collapsed, expanded) {
return Expandable(
collapsed: collapsed,
expanded: expanded,
theme: const ExpandableThemeData(crossFadePoint: 0),
);
},
),
),
],
),
),
));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3034 次 |
| 最近记录: |