Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created Feb 16, 2020
Embed
What would you like to do?
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