Skip to content

Instantly share code, notes, and snippets.

@xster
Last active March 24, 2021 19:46
Show Gist options
  • Save xster/0fdf6aac896bae0e011605010946ad92 to your computer and use it in GitHub Desktop.
Save xster/0fdf6aac896bae0e011605010946ad92 to your computer and use it in GitHub Desktop.
datastore + pigeon
@datastore
class Profile {
String name;
String address;
Image profile;
}
@HostApi
abstract class ProfileApi {
Profile getProfile();
}
@datastore
class FeedList {
String topic;
List<FeedItem> items;
}
@datastore
class FeedItem {
String timestmap;
String title;
String summary;
}
@FlutterApi
abstract class FeedApi {
void displayNewsFeed(FeedList feedList);
}
// Flutter gets an observable Profile
class ProfileWidget extends StatefulWidget {
ProfileWidget({Key key, this.title}) : super(key: key);
final String title;
@override
_ProfileWidgetState createState() => _ProfileWidgetState();
}
class _ProfileWidgetState extends State<ProfileWidget> implements ProfileApiHandler {
// This is observable.
ValueNotifier<Profile> profile;
@override
void initState() {
super.initState();
ProfileApi().getProfile().then((ValueNotifier<Profile> profile) {
setState(() {
this.profile = profile
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: ValueListenableBuilder(
builder: (BuildContext context, Profile profileSnapshot, Widget? child) {
return // Do stuff;
},
valueListenable: profile,
),
),
);
}
}
// Flutter gets pushed an observable feed list with observable feed items
class FeedWidget extends StatefulWidget {
FeedWidget({Key key, this.title}) : super(key: key);
final String title;
@override
_FeedWidgetState createState() => _FeedWidgetState();
}
class _FeedWidgetState extends State<FeedWidget> implements FeedApiHandler {
// This is observable.
ValueNotifier<FeedList> feedList;
@override
void initState() {
super.initState();
FeedApi.setup(this);
}
// Does this support our previous platform channel buffer work?
void displayNewsFeed(ValueNotifier<FeedList> feedList) {
setState(() => this.feedList = feedList);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: ValueListenableBuilder(
builder: (BuildContext context, FeedList feedList, Widget? child) {
return GridView.count(
crossAxisCount: 2,
children: List.generate(feedList.items.count, (index) {
return FeedItemWidget(feedList.items[index]);
}),
);
},
valueListenable: feedList,
),
),
);
}
}
class FeedItemWidget extends StatelessWidget {
final ValueNotifier<FeedItem> feedItem;
FeedItemWidget(this.feedItem);
@override
Widget build(BuildContext context) {
return ValueListenableBuilder(
builder: (BuildContext context, FeedItem item, Widget? child) {
return // Do things
},
valueListenable: feedItem,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment