110 lines
3.2 KiB
Dart
Raw Normal View History

2021-09-07 17:12:03 +08:00
import 'package:app_flowy/startup/startup.dart';
import 'package:app_flowy/workspace/application/workspace/welcome_bloc.dart';
2021-12-07 23:01:23 +05:30
import 'package:easy_localization/easy_localization.dart';
2021-11-05 16:23:44 +08:00
import 'package:flowy_infra/theme.dart';
2021-09-06 16:18:34 +08:00
import 'package:flowy_infra_ui/style_widget/scrolling/styled_list.dart';
2021-10-13 23:11:45 +08:00
import 'package:flowy_infra_ui/style_widget/button.dart';
2021-09-06 16:18:34 +08:00
import 'package:flowy_infra_ui/widget/error_page.dart';
2022-07-04 15:00:54 +08:00
import 'package:flowy_sdk/protobuf/flowy-folder/workspace.pb.dart';
2022-07-04 15:07:11 +08:00
import 'package:flowy_sdk/protobuf/flowy-user/user_profile.pb.dart';
2021-06-19 23:41:19 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-12-07 23:01:23 +05:30
import 'package:app_flowy/generated/locale_keys.g.dart';
2021-06-19 23:41:19 +08:00
class WelcomeScreen extends StatelessWidget {
final UserProfile userProfile;
2021-09-06 16:18:34 +08:00
const WelcomeScreen({
Key? key,
required this.userProfile,
2021-09-06 16:18:34 +08:00
}) : super(key: key);
2021-06-19 23:41:19 +08:00
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => getIt<WelcomeBloc>(param1: userProfile)..add(const WelcomeEvent.initial()),
2021-09-07 17:12:03 +08:00
child: BlocBuilder<WelcomeBloc, WelcomeState>(
2021-09-06 16:18:34 +08:00
builder: (context, state) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(60.0),
child: Column(
children: [
_renderBody(state),
_renderCreateButton(context),
],
),
),
);
},
2021-06-19 23:41:19 +08:00
),
);
}
2021-09-07 17:12:03 +08:00
Widget _renderBody(WelcomeState state) {
2021-09-06 16:18:34 +08:00
final body = state.successOrFailure.fold(
(_) => _renderList(state.workspaces),
(error) => FlowyErrorPage(error.toString()),
);
return body;
2021-07-12 23:27:58 +08:00
}
2021-09-06 16:18:34 +08:00
Widget _renderCreateButton(BuildContext context) {
2021-11-05 16:23:44 +08:00
final theme = context.watch<AppTheme>();
2021-09-06 16:18:34 +08:00
return SizedBox(
width: 200,
height: 40,
child: FlowyTextButton(
2021-12-07 23:01:23 +05:30
LocaleKeys.workspace_create.tr(),
2021-09-06 16:18:34 +08:00
fontSize: 14,
2021-11-05 16:23:44 +08:00
hoverColor: theme.bg3,
2021-09-06 16:18:34 +08:00
onPressed: () {
2021-12-07 23:01:23 +05:30
context.read<WelcomeBloc>().add(WelcomeEvent.createWorkspace(LocaleKeys.workspace_hint.tr(), ""));
2021-09-06 16:18:34 +08:00
},
),
);
2021-07-12 23:27:58 +08:00
}
2021-09-06 16:18:34 +08:00
Widget _renderList(List<Workspace> workspaces) {
return Expanded(
child: StyledListView(
itemBuilder: (BuildContext context, int index) {
final workspace = workspaces[index];
return WorkspaceItem(
workspace: workspace,
onPressed: (workspace) => _handleOnPress(context, workspace),
);
},
itemCount: workspaces.length,
),
);
}
void _handleOnPress(BuildContext context, Workspace workspace) {
2021-09-07 17:12:03 +08:00
context.read<WelcomeBloc>().add(WelcomeEvent.openWorkspace(workspace));
2021-07-12 23:27:58 +08:00
2021-09-06 16:18:34 +08:00
Navigator.of(context).pop(workspace.id);
2021-07-12 23:27:58 +08:00
}
}
2021-09-06 16:18:34 +08:00
class WorkspaceItem extends StatelessWidget {
final Workspace workspace;
final void Function(Workspace workspace) onPressed;
2021-10-13 23:11:45 +08:00
const WorkspaceItem({Key? key, required this.workspace, required this.onPressed}) : super(key: key);
2021-09-06 16:18:34 +08:00
2021-07-12 23:27:58 +08:00
@override
Widget build(BuildContext context) {
2021-11-05 16:23:44 +08:00
final theme = context.watch<AppTheme>();
2021-09-06 16:18:34 +08:00
return SizedBox(
height: 46,
child: FlowyTextButton(
workspace.name,
2021-11-05 16:23:44 +08:00
hoverColor: theme.bg3,
2021-09-06 16:18:34 +08:00
fontSize: 14,
onPressed: () => onPressed(workspace),
2021-07-12 23:27:58 +08:00
),
);
2021-06-19 23:41:19 +08:00
}
}