Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created February 16, 2020 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlid/793a9815f2d091f0e6299550c8bded00 to your computer and use it in GitHub Desktop.
Save ryanlid/793a9815f2d091f0e6299550c8bded00 to your computer and use it in GitHub Desktop.
Offstage 控制是否显示组件
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appTitle = "Offstage控制是否显示组件";
return MaterialApp(
title: appTitle,
home: MyHomePage(title: appTitle),
);
}
}
class MyHomePage extends StatefulWidget {
final String title;
MyHomePage({Key key, this.title}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
// 状态控制是否显示文本组件
bool offstage = true;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Offstage(
// 控制是否显示
offstage: offstage,
child: Text(
'我出来啦!',
style: TextStyle(
fontSize: 36.0,
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// 设置是否显示文本组件
setState(() {
offstage = !offstage;
});
},
tooltip: "显示隐藏",
child: Icon(Icons.flip),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment