flutter - Flutter - 容器是否有动态展开和折叠功能?
问题描述
我希望能够动态添加包含各种小部件的行。我目前这样做的方式是创建 3 个具有我需要的小部件的独特容器并根据级别缩进它们,但必须有一些东西已经这样做了?
我希望能够单击 + 或 - 按钮来展开或折叠底部的容器
以下是完整的 main.dart 代码:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Appbar for nested'),),
body: SafeArea(
child: NestedStuff(),
),
)
);
}
}
class NestedStuff extends StatefulWidget {
@override
_NestedStuffState createState() => _NestedStuffState();
}
class _NestedStuffState extends State<NestedStuff> {
@override
Widget build(BuildContext context) {
return Column(
children: [
RowWidgetOne(),
RowWidgetTwo(),
RowWidgetThree(),
RowWidgetTwo(),
RowWidgetThree(),
RowWidgetThree(),
],
);
}
}
class RowWidgetOne extends StatelessWidget {
// const RowWidget({
// Key key,
// }) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(25,8.0,8,25),
child: Container(
color: Colors.blue,
padding: EdgeInsets.all(10.0),
child: Row(
children: <Widget> [
CircleAvatar(
child: Icon(Icons.remove),
backgroundColor: Colors.lightBlue,
),
Text('sometext'),
],
),
),
);
}
}
class RowWidgetTwo extends StatelessWidget {
// const RowWidget({
// Key key,
// }) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(50,1.0,1.0,25),
child: Container(
color: Colors.green,
padding: EdgeInsets.all(10.0),
child: Row(
children: <Widget> [
CircleAvatar(
child: Icon(Icons.remove),
backgroundColor: Colors.purple,
),
Text('sometext'),
],
),
),
);
}
}
class RowWidgetThree extends StatelessWidget {
// const RowWidget({
// Key key,
// }) : super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(75,8.0,8,25),
child: Container(
color: Colors.grey,
padding: EdgeInsets.all(10.0),
child: Row(
children: <Widget> [
CircleAvatar(
child: Icon(Icons.add),
backgroundColor: Colors.lightBlue,
),
Text('sometext'),
],
),
),
);
}
}
解决方案
您可以使用Visibility
小部件
bool _visibility = false;
Column(
children: [
// first row including + and - button changing _visibility value,
Visibility(
visible: _visibility,
child: Column(
children: [
//other children
],
),
)
],
)
推荐阅读
- dart - 为什么 Dart 中的单元化类型变量不实现类型的接口?
- ocaml - 在 OCaml Menhir 中,如何为 C++/Rust/Java 风格的泛型编写解析器
- python - 使用 make_lupton_rgb 获取深色图像
- android - ConstraintLayout 中的 MoPubRecyclerAdapter 和 Facebook 原生广告崩溃
- python - Keras 错误:dense_3 层的输入 0 与层不兼容::预期 min_ndim=2,发现 ndim=1。收到的完整形状:[16]
- algorithm - 如何设计使用 A* 寻路算法找到 FASTEST 路线的成本函数和启发式函数?
- ios - 嵌套的 UICollectionView 为每个部分返回相同的值 - Swift
- python-3.x - 为每个列表列表造一个词
- algorithm - 最长回文子串。以下 DP + Recursive 方法的运行时间可能是多少。是 O(n^2) 还是 O(n^3)
- swift - 如何解决 WKWebView 较小的内容高度值问题?