flutter - 单击按钮时显示小部件(Dart,Flutter)
问题描述
单击按钮时如何显示小部件(例如更多按钮)。
FlatButton(
onPressed: () {
//render the new widgets
},
child: Icon(
Icons.close,
color: Colors.white,
),
));
这是父类
class StopStream extends StatelessWidget
解决方案
- 您可以借助变量有条件地显示/隐藏小部件。
- 您需要一个 StatefulWidget 来更改小部件的状态,即动态显示/隐藏(小部件)。
请看下面的代码,我使用 abool showWidget
来显示或隐藏更多FlatButton
的 a Row
:
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),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
bool showWidget = false;
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
showWidget
? Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
onPressed: () {},
child: const Icon(Icons.ac_unit),
),
FlatButton(
onPressed: () {},
child: const Icon(Icons.accessible),
),
FlatButton(
onPressed: () {},
child: const Icon(Icons.backpack),
),
FlatButton(
onPressed: () {},
child: const Icon(Icons.cached),
),
],
)
: Container(),
FlatButton(
onPressed: () {
setState(() {
showWidget = !showWidget;
});
},
child: const Icon(
Icons.close,
color: Colors.white,
),
),
],
);
}
}
推荐阅读
- ios - UICollectionview 在部分之间有不需要的空间
- sqliteexception - 某些设备中的 android.database.sqlite.SQLiteException
- r - 如何通过使用plot()函数R通过分类列的值增加颜色的阴影来为绘图着色
- javascript - 如何在发送到服务器之前将 mimetype (video/webm) 和编解码器 (vp9) 添加到 blob 对象
- php - 如何使用 laravel 注册验证器返回错误
- php - 在php中搜索带有条件的数组
- angular - 为什么 Chrome 在使用 Angular Service-Worker 时会显示重复的 HTTP 请求?
- elasticsearch - Elasticsearch - cologne 和 koelnerphonetik 编码器的区别
- java - 在 API Retrofit PUT 调用中将自定义对象数组作为参数传递
- angular - ngmodule 不是子类型 ngmoduletype