Solian/lib/screens/realms/realm.dart

132 lines
3.2 KiB
Dart
Raw Normal View History

2024-05-05 15:01:08 +00:00
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
2024-05-05 15:51:46 +00:00
import 'package:solian/models/realm.dart';
2024-05-05 15:01:08 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/realm.dart';
2024-05-05 15:51:46 +00:00
import 'package:solian/router.dart';
2024-05-05 15:01:08 +00:00
import 'package:solian/screens/chat/chat_list.dart';
import 'package:solian/screens/explore.dart';
import 'package:solian/utils/theme.dart';
import 'package:solian/widgets/scaffold.dart';
class RealmScreen extends StatelessWidget {
final String alias;
const RealmScreen({super.key, required this.alias});
@override
Widget build(BuildContext context) {
2024-05-05 15:51:46 +00:00
final auth = context.read<AuthProvider>();
2024-05-05 15:01:08 +00:00
final realm = context.watch<RealmProvider>();
return IndentScaffold(
title: realm.focusRealm?.name ?? 'Loading...',
2024-05-06 15:36:54 +00:00
hideDrawer: true,
2024-05-05 15:01:08 +00:00
fixedAppBarColor: SolianTheme.isLargeScreen(context),
2024-05-05 15:51:46 +00:00
appBarActions: realm.focusRealm != null
? [
RealmManageAction(
realm: realm.focusRealm!,
onUpdate: () => realm.fetchSingle(auth, alias),
),
]
: [],
2024-05-08 14:01:06 +00:00
appBarLeading: SolianTheme.isLargeScreen(context)
? IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
realm.clearFocus();
},
)
: null,
body: RealmWidget(
2024-05-05 15:01:08 +00:00
alias: alias,
),
);
}
}
class RealmWidget extends StatefulWidget {
final String alias;
const RealmWidget({super.key, required this.alias});
@override
State<RealmWidget> createState() => _RealmWidgetState();
}
class _RealmWidgetState extends State<RealmWidget> {
bool _isReady = false;
late RealmProvider _realm;
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () {
final auth = context.read<AuthProvider>();
if (_realm.focusRealm?.alias != widget.alias) {
_realm.fetchSingle(auth, widget.alias);
}
});
}
@override
Widget build(BuildContext context) {
if (!_isReady) {
_realm = context.watch<RealmProvider>();
_isReady = true;
}
return DefaultTabController(
2024-05-06 12:57:52 +00:00
length: 2,
2024-05-05 15:01:08 +00:00
child: Column(
children: [
TabBar(
isScrollable: !SolianTheme.isLargeScreen(context),
tabs: const [
Tab(icon: Icon(Icons.newspaper)),
Tab(icon: Icon(Icons.message)),
],
),
Expanded(
child: TabBarView(
children: [
ExplorePostWidget(realm: widget.alias),
ChatListWidget(realm: widget.alias),
],
),
)
],
),
);
}
}
2024-05-05 15:51:46 +00:00
class RealmManageAction extends StatelessWidget {
final Realm realm;
final Function onUpdate;
const RealmManageAction({
super.key,
required this.realm,
required this.onUpdate,
});
@override
Widget build(BuildContext context) {
return IconButton(
onPressed: () async {
final did = await SolianRouter.router.pushNamed(
2024-05-06 12:57:52 +00:00
'realms.manage',
2024-05-05 15:51:46 +00:00
extra: realm,
2024-05-06 12:57:52 +00:00
pathParameters: {'realm': realm.alias},
2024-05-05 15:51:46 +00:00
);
if (did == true) onUpdate();
},
icon: const Icon(Icons.settings),
);
}
}