mirror of
https://github.com/nostrlabs-io/zap-stream-flutter.git
synced 2025-06-15 19:48:23 +00:00
init
This commit is contained in:
40
lib/widgets/avatar.dart
Normal file
40
lib/widgets/avatar.dart
Normal file
@ -0,0 +1,40 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:ndk/ndk.dart';
|
||||
import 'package:zap_stream_flutter/imgproxy.dart';
|
||||
import 'package:zap_stream_flutter/widgets/profile.dart';
|
||||
|
||||
class AvatarWidget extends StatelessWidget {
|
||||
final double? size;
|
||||
final Metadata profile;
|
||||
|
||||
const AvatarWidget({super.key, required this.profile, this.size});
|
||||
|
||||
static Widget pubkey(String pubkey, {double? size}) {
|
||||
return ProfileLoaderWidget(pubkey, (ctx, data) {
|
||||
return AvatarWidget(
|
||||
profile: data.data ?? Metadata(pubKey: pubkey),
|
||||
size: size,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final thisSize = size ?? 40;
|
||||
return ClipOval(
|
||||
child: CachedNetworkImage(
|
||||
fit: BoxFit.cover,
|
||||
imageUrl: proxyImg(
|
||||
context,
|
||||
profile.picture ??
|
||||
"https://nostr.api.v0l.io/api/v1/avatar/cyberpunks/${profile.pubKey}",
|
||||
resize: thisSize.ceil(),
|
||||
),
|
||||
height: thisSize,
|
||||
width: thisSize,
|
||||
errorWidget: (context, url, error) => Icon(Icons.error),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
61
lib/widgets/button.dart
Normal file
61
lib/widgets/button.dart
Normal file
@ -0,0 +1,61 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
|
||||
class BasicButton extends StatelessWidget {
|
||||
final Widget? child;
|
||||
final BoxDecoration? decoration;
|
||||
final EdgeInsetsGeometry? padding;
|
||||
final EdgeInsetsGeometry? margin;
|
||||
final void Function()? onTap;
|
||||
|
||||
const BasicButton(
|
||||
this.child, {
|
||||
super.key,
|
||||
this.decoration,
|
||||
this.padding,
|
||||
this.margin,
|
||||
this.onTap,
|
||||
});
|
||||
|
||||
static text(
|
||||
String text, {
|
||||
BoxDecoration? decoration,
|
||||
EdgeInsetsGeometry? padding,
|
||||
EdgeInsetsGeometry? margin,
|
||||
void Function()? onTap,
|
||||
double? fontSize,
|
||||
}) {
|
||||
return BasicButton(
|
||||
Text(
|
||||
text,
|
||||
style: TextStyle(
|
||||
color: Color.fromARGB(255, 255, 255, 255),
|
||||
fontSize: fontSize,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
decoration: decoration,
|
||||
padding: padding ?? EdgeInsets.symmetric(vertical: 10),
|
||||
margin: margin,
|
||||
onTap: onTap,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: padding,
|
||||
margin: margin,
|
||||
decoration:
|
||||
decoration ??
|
||||
BoxDecoration(
|
||||
color: LAYER_2,
|
||||
borderRadius: BorderRadius.all(Radius.circular(100)),
|
||||
),
|
||||
child: Center(child: child),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
146
lib/widgets/chat.dart
Normal file
146
lib/widgets/chat.dart
Normal file
@ -0,0 +1,146 @@
|
||||
import 'dart:developer' as developer;
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:ndk/ndk.dart';
|
||||
import 'package:zap_stream_flutter/main.dart';
|
||||
import 'package:zap_stream_flutter/rx_filter.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
import 'package:zap_stream_flutter/utils.dart';
|
||||
import 'package:zap_stream_flutter/widgets/avatar.dart';
|
||||
import 'package:zap_stream_flutter/widgets/profile.dart';
|
||||
|
||||
class ChatWidget extends StatelessWidget {
|
||||
final StreamEvent stream;
|
||||
|
||||
const ChatWidget({super.key, required this.stream});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: SingleChildScrollView(
|
||||
reverse: true,
|
||||
child: RxFilter<Nip01Event>(
|
||||
filter: Filter(kinds: [1311], limit: 200, aTags: [stream.aTag]),
|
||||
builder: (ctx, state) {
|
||||
return Column(
|
||||
spacing: 8,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children:
|
||||
(state ?? [])
|
||||
.sortedBy((c) => c.createdAt)
|
||||
.map((c) => ChatMessageWidget(stream: stream, msg: c))
|
||||
.toList(),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
WriteMessageWidget(stream: stream),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatMessageWidget extends StatelessWidget {
|
||||
final StreamEvent stream;
|
||||
final Nip01Event msg;
|
||||
|
||||
const ChatMessageWidget({super.key, required this.stream, required this.msg});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ProfileLoaderWidget(msg.pubKey, (ctx, state) {
|
||||
final profile = state.data ?? Metadata(pubKey: msg.pubKey);
|
||||
return RichText(
|
||||
text: TextSpan(
|
||||
children: [
|
||||
WidgetSpan(child: AvatarWidget(profile: profile, size: 20)),
|
||||
TextSpan(text: " "),
|
||||
TextSpan(
|
||||
text: ProfileNameWidget.nameFromProfile(profile),
|
||||
style: TextStyle(
|
||||
color: msg.pubKey == stream.info.host ? PRIMARY_1 : SECONDARY_1,
|
||||
),
|
||||
),
|
||||
TextSpan(text: " "),
|
||||
TextSpan(text: msg.content, style: TextStyle(color: FONT_COLOR)),
|
||||
],
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class WriteMessageWidget extends StatefulWidget {
|
||||
final StreamEvent stream;
|
||||
|
||||
const WriteMessageWidget({super.key, required this.stream});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _WriteMessageWidget();
|
||||
}
|
||||
|
||||
class _WriteMessageWidget extends State<WriteMessageWidget> {
|
||||
final TextEditingController _controller = TextEditingController();
|
||||
|
||||
Future<void> _sendMessage() async {
|
||||
final login = ndk.accounts.getLoggedAccount();
|
||||
if (login == null) return;
|
||||
|
||||
final chatMsg = Nip01Event(
|
||||
pubKey: login.pubkey,
|
||||
kind: 1311,
|
||||
content: _controller.text,
|
||||
tags: [
|
||||
["a", widget.stream.aTag],
|
||||
],
|
||||
);
|
||||
developer.log(chatMsg.toString());
|
||||
final res = ndk.broadcast.broadcast(nostrEvent: chatMsg);
|
||||
await res.broadcastDoneFuture;
|
||||
_controller.text = "";
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isLogin = ndk.accounts.isLoggedIn;
|
||||
|
||||
return Container(
|
||||
margin: EdgeInsets.fromLTRB(4, 8, 4, 0),
|
||||
padding: EdgeInsets.symmetric(horizontal: 8),
|
||||
decoration: BoxDecoration(color: LAYER_2, borderRadius: DEFAULT_BR),
|
||||
child:
|
||||
isLogin
|
||||
? Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _controller,
|
||||
decoration: InputDecoration(
|
||||
labelText: "Write message",
|
||||
contentPadding: EdgeInsets.symmetric(vertical: 4),
|
||||
labelStyle: TextStyle(color: LAYER_4, fontSize: 14),
|
||||
border: InputBorder.none,
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(onPressed: () {}, icon: Icon(Icons.mood)),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
_sendMessage();
|
||||
},
|
||||
icon: Icon(Icons.send),
|
||||
),
|
||||
],
|
||||
)
|
||||
: Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 12),
|
||||
child: Row(children: [Text("Please login to send messages")]),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
55
lib/widgets/header.dart
Normal file
55
lib/widgets/header.dart
Normal file
@ -0,0 +1,55 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/svg.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:zap_stream_flutter/main.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
import 'package:zap_stream_flutter/widgets/avatar.dart';
|
||||
|
||||
class HeaderWidget extends StatefulWidget {
|
||||
const HeaderWidget({super.key});
|
||||
|
||||
@override
|
||||
State<StatefulWidget> createState() => _HeaderWidget();
|
||||
}
|
||||
|
||||
class _HeaderWidget extends State<HeaderWidget> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: EdgeInsets.symmetric(vertical: 12),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
SvgPicture.asset("assets/svg/logo.svg", height: 23),
|
||||
LoginButtonWidget(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class LoginButtonWidget extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (ndk.accounts.isLoggedIn) {
|
||||
return AvatarWidget.pubkey(ndk.accounts.getPublicKey()!);
|
||||
} else {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
context.push("/login");
|
||||
},
|
||||
child: Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: FONT_COLOR),
|
||||
borderRadius: BorderRadius.all(Radius.circular(50)),
|
||||
),
|
||||
child: Row(
|
||||
spacing: 8,
|
||||
children: [Text("Login"), Icon(Icons.login, size: 16)],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
20
lib/widgets/pill.dart
Normal file
20
lib/widgets/pill.dart
Normal file
@ -0,0 +1,20 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class PillWidget extends StatelessWidget {
|
||||
final Widget child;
|
||||
final Color? color;
|
||||
|
||||
const PillWidget({super.key, required this.child, required this.color});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
padding: EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.all(Radius.circular(8)),
|
||||
color: color,
|
||||
),
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
}
|
83
lib/widgets/profile.dart
Normal file
83
lib/widgets/profile.dart
Normal file
@ -0,0 +1,83 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:ndk/ndk.dart';
|
||||
import 'package:ndk/shared/nips/nip19/nip19.dart';
|
||||
import 'package:zap_stream_flutter/main.dart';
|
||||
import 'package:zap_stream_flutter/widgets/avatar.dart';
|
||||
|
||||
class ProfileLoaderWidget extends StatelessWidget {
|
||||
final String pubkey;
|
||||
final AsyncWidgetBuilder<Metadata?> builder;
|
||||
|
||||
const ProfileLoaderWidget(this.pubkey, this.builder, {super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder(
|
||||
future: ndk.metadata.loadMetadata(pubkey),
|
||||
builder: builder,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ProfileNameWidget extends StatelessWidget {
|
||||
final Metadata profile;
|
||||
final TextStyle? style;
|
||||
|
||||
const ProfileNameWidget({super.key, required this.profile, this.style});
|
||||
|
||||
static Widget pubkey(String pubkey, {TextStyle? style}) {
|
||||
return FutureBuilder(
|
||||
future: ndk.metadata.loadMetadata(pubkey),
|
||||
builder:
|
||||
(ctx, data) => ProfileNameWidget(
|
||||
profile: data.data ?? Metadata(pubKey: pubkey),
|
||||
style: style,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static nameFromProfile(Metadata profile) {
|
||||
if ((profile.displayName?.length ?? 0) > 0) {
|
||||
return profile.displayName!;
|
||||
}
|
||||
if ((profile.name?.length ?? 0) > 0) {
|
||||
return profile.name!;
|
||||
}
|
||||
return Nip19.encodeSimplePubKey(profile.pubKey);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Text(ProfileNameWidget.nameFromProfile(profile), style: style);
|
||||
}
|
||||
}
|
||||
|
||||
class ProfileWidget extends StatelessWidget {
|
||||
final Metadata profile;
|
||||
final TextStyle? style;
|
||||
final double? size;
|
||||
|
||||
const ProfileWidget({
|
||||
super.key,
|
||||
required this.profile,
|
||||
this.style,
|
||||
this.size,
|
||||
});
|
||||
|
||||
static Widget pubkey(String pubkey) {
|
||||
return ProfileLoaderWidget(pubkey, (ctx, state) {
|
||||
return ProfileWidget(profile: state.data ?? Metadata(pubKey: pubkey));
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
spacing: 8,
|
||||
children: [
|
||||
AvatarWidget(profile: profile, size: size),
|
||||
ProfileNameWidget(profile: profile),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
43
lib/widgets/stream_grid.dart
Normal file
43
lib/widgets/stream_grid.dart
Normal file
@ -0,0 +1,43 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:ndk/ndk.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
import 'package:zap_stream_flutter/utils.dart';
|
||||
import 'package:zap_stream_flutter/widgets/stream_tile.dart';
|
||||
|
||||
class StreamGrid extends StatelessWidget {
|
||||
final List<Nip01Event> events;
|
||||
const StreamGrid({super.key, required this.events});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final streams = events.map((e) => StreamEvent(e));
|
||||
final live = streams.where((s) => s.info.status == StreamStatus.live);
|
||||
return Column(children: [_streamGroup("Live", live)]);
|
||||
}
|
||||
|
||||
Widget _streamGroup(String title, Iterable<StreamEvent> events) {
|
||||
return Column(
|
||||
spacing: 16,
|
||||
children: [
|
||||
Row(
|
||||
spacing: 16,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
style: TextStyle(fontSize: 21, fontWeight: FontWeight.w500),
|
||||
),
|
||||
Expanded(
|
||||
child: Container(
|
||||
height: 1,
|
||||
decoration: BoxDecoration(
|
||||
border: Border(bottom: BorderSide(color: LAYER_1)),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
...events.map((e) => StreamTileWidget(e)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
102
lib/widgets/stream_tile.dart
Normal file
102
lib/widgets/stream_tile.dart
Normal file
@ -0,0 +1,102 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:ndk/shared/nips/nip19/nip19.dart';
|
||||
import 'package:zap_stream_flutter/imgproxy.dart';
|
||||
import 'package:zap_stream_flutter/theme.dart';
|
||||
import 'package:zap_stream_flutter/utils.dart';
|
||||
import 'package:zap_stream_flutter/widgets/avatar.dart';
|
||||
import 'package:zap_stream_flutter/widgets/pill.dart';
|
||||
import 'package:zap_stream_flutter/widgets/profile.dart';
|
||||
|
||||
class StreamTileWidget extends StatelessWidget {
|
||||
final StreamEvent stream;
|
||||
|
||||
const StreamTileWidget(this.stream, {super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
context.push(
|
||||
"/e/${Nip19.encodeNoteId(stream.event.id)}",
|
||||
extra: stream,
|
||||
);
|
||||
},
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
spacing: 8,
|
||||
children: [
|
||||
Container(
|
||||
decoration: BoxDecoration(borderRadius: DEFAULT_BR, color: LAYER_1),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: AspectRatio(
|
||||
aspectRatio: 16 / 9,
|
||||
child: Stack(
|
||||
children: [
|
||||
CachedNetworkImage(
|
||||
imageUrl: proxyImg(context, stream.info.image ?? ""),
|
||||
),
|
||||
if (stream.info.status != null)
|
||||
Positioned(
|
||||
right: 8,
|
||||
top: 8,
|
||||
child: PillWidget(
|
||||
color: Theme.of(context).highlightColor,
|
||||
child: Text(
|
||||
stream.info.status!.name.toUpperCase(),
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (stream.info.participants != null)
|
||||
Positioned(
|
||||
right: 8,
|
||||
bottom: 8,
|
||||
child: PillWidget(
|
||||
color: LAYER_1.withAlpha(200),
|
||||
child: Text(
|
||||
"${stream.info.participants} viewers",
|
||||
style: TextStyle(
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 14,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Row(
|
||||
spacing: 8,
|
||||
children: [
|
||||
AvatarWidget.pubkey(stream.info.host),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
stream.info.title ?? "",
|
||||
overflow: TextOverflow.clip,
|
||||
style: TextStyle(fontWeight: FontWeight.w500),
|
||||
),
|
||||
ProfileNameWidget.pubkey(
|
||||
stream.info.host,
|
||||
style: TextStyle(color: LAYER_4),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user