Skip to content

Instantly share code, notes, and snippets.

@shihaohong
Created September 14, 2019 00:16
Show Gist options
  • Save shihaohong/f3093f3a244e4b49dfd74400f22f9e2f to your computer and use it in GitHub Desktop.
Save shihaohong/f3093f3a244e4b49dfd74400f22f9e2f to your computer and use it in GitHub Desktop.
import 'dart:async';
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
Stream<bool> get isFABVisible => _isFABVisibleController.stream;
final _isFABVisibleController = StreamController<bool>.broadcast();
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(widget.title)),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text("Push new page"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => NewPage()),
);
},
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
);
}
}
class NewPage extends StatelessWidget {
const NewPage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return StreamBuilder<bool>(
stream: isFABVisible,
initialData: true,
builder: (context, snapshot) {
return Scaffold(
appBar: AppBar(title: Text('Next page')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: () => _isFABVisibleController.add(false),
child: Text("Press to make FAB disappear"),
),
RaisedButton(
onPressed: () => _isFABVisibleController.add(true),
child: Text("Press to make FAB reappear"),
),
],
),
),
floatingActionButton: snapshot.data
? FloatingActionButton(
onPressed: () {},
child: Icon(Icons.remove_circle),
)
: null,
);
},
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment