Skip to content

Commit

Permalink
changed appearance of the category and file selection widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Zhdanov committed Dec 27, 2024
1 parent 87ee205 commit 646eb68
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 135 deletions.
128 changes: 63 additions & 65 deletions lib/category/widget/category_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import 'package:chrono_sheet/category/model/category.dart';
import 'package:chrono_sheet/category/state/categories_state.dart';
import 'package:chrono_sheet/file/state/files_state.dart';
import 'package:chrono_sheet/generated/app_localizations.dart';
import 'package:chrono_sheet/ui/dimension.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

Expand Down Expand Up @@ -60,73 +58,73 @@ class CategoryState extends ConsumerState<CategoryWidget> {
final asyncInfo = ref.watch(fileCategoriesProvider);
final theme = Theme.of(context);
final localization = AppLocalizations.of(context);
return Padding(
padding: EdgeInsets.only(
left: Dimension.historyIconButtonSize,
right: Dimension.historyIconButtonSize,
),
child: Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide()
),
return Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide()
),
child: Row(
children: [
Expanded(
child: GestureDetector(
onTap: _startEditing,
child: _editing
? TextField(
controller: _controller,
focusNode: _focusNode,
decoration: InputDecoration(
border: InputBorder.none,
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
),
)
: asyncInfo.when(
data: (data) => Text(
data.selected?.name
),
child: Row(
children: [
Expanded(
child: _editing
? TextField(
controller: _controller,
focusNode: _focusNode,
decoration: InputDecoration(
border: InputBorder.none,
enabledBorder: InputBorder.none,
focusedBorder: InputBorder.none,
),
textAlign: TextAlign.center,
)
: GestureDetector(
onTap: _startEditing,
child: Container(
color: Colors.transparent,
child: Center(
child: asyncInfo.when(
data: (data) => Text(
data.selected?.name
?? localization.hintTapToCreateCategory,
style: data.selected == null
? TextStyle(color: theme.disabledColor)
: null,
),
error: (_, __) => Text(
localization.errorCanNotParseCategories,
style: TextStyle(color: theme.disabledColor),
),
loading: () => Text(
localization.progressParsingCategories,
style: TextStyle(color: theme.disabledColor),
),
style: data.selected == null
? TextStyle(color: theme.disabledColor)
: null,
),
error: (_, __) => Text(
localization.errorCanNotParseCategories,
style: TextStyle(color: theme.disabledColor),
),
loading: () => Text(
localization.progressParsingCategories,
style: TextStyle(color: theme.disabledColor),
),
)
),
),
)
),
),
asyncInfo.when(
data: (data) => PopupMenuButton<Category>(
icon: Icon(Icons.arrow_drop_down),
onSelected: data.categories.isEmpty ? null : _onCategorySelected,
itemBuilder: (context) => data.categories.map((c) {
return PopupMenuItem(
value: c,
child: Text(c.name),
);
}).toList(),
),
asyncInfo.when(
data: (data) => PopupMenuButton<Category>(
icon: Icon(Icons.arrow_drop_down),
onSelected: data.categories.isEmpty ? null : _onCategorySelected,
itemBuilder: (context) => data.categories.map((c) {
return PopupMenuItem(
value: c,
child: Text(c.name),
);
}).toList(),
),
error: (_, __) => PopupMenuButton(
icon: Icon(Icons.arrow_drop_down),
itemBuilder: (context) => [],
),
loading: () => PopupMenuButton(
icon: Icon(Icons.arrow_drop_down),
itemBuilder: (context) => [],
),
)
],
),
error: (_, __) => PopupMenuButton(
icon: Icon(Icons.arrow_drop_down),
itemBuilder: (context) => [],
),
loading: () => PopupMenuButton(
icon: Icon(Icons.arrow_drop_down),
itemBuilder: (context) => [],
),
)
],
),
);
}
Expand Down
134 changes: 64 additions & 70 deletions lib/file/widget/selected_file_widget.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:chrono_sheet/file/model/google_file.dart';
import 'package:chrono_sheet/router/router.dart';
import 'package:chrono_sheet/ui/dimension.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
Expand All @@ -10,86 +9,81 @@ import '../state/files_state.dart';

class SelectedFileWidget extends ConsumerWidget {

final GlobalKey _historyButtonKey = GlobalKey();

SelectedFileWidget({super.key});
const SelectedFileWidget({super.key});

void _selectFile(BuildContext context) async {
context.push(AppRoute.chooseSheet);
}

void _onFileSelected(GoogleFile file, WidgetRef ref) {
ref.read(filesInfoHolderProvider.notifier).select(file);
}

@override
Widget build(BuildContext context, WidgetRef ref) {
final asyncFiles = ref.watch(filesInfoHolderProvider);
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SizedBox(width: Dimension.historyIconButtonSize),
Expanded(
child: GestureDetector(
onTap: () => _selectFile(context),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8.0),
final asyncData = ref.watch(filesInfoHolderProvider);
final localization = AppLocalizations.of(context);
final theme = Theme.of(context);
return Container(
decoration: BoxDecoration(
border: Border(
bottom: BorderSide()
),
),
child: Row(
// mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: GestureDetector(
onTap: () => _selectFile(context),
// we use Container here in order for it to fill all the
// available space occupied by Expanded. Otherwise
// GestureDetector reacts only on taps on the nested Text.
child: Container(
color: Colors.transparent,
child: Center(
child: asyncData.when(
data: (data) => Text(
data.selected?.name ?? localization.hintSelectFile,
style: data.selected == null
? TextStyle(color: theme.disabledColor)
: null,
),
error: (_, __) => Text(
localization.hintSelectFile,
style: TextStyle(color: theme.disabledColor),
),
loading: () => Text(
localization.hintSelectFile,
style: TextStyle(color: theme.disabledColor),
),
),
),
child: Padding(
padding: EdgeInsets.all(16.0),
child: Center(
child: asyncFiles.when(
data: (data) =>
_fileWidget(data.selected?.name, context),
error: (_, __) => _fileWidget(null, context),
loading: () => _fileWidget(null, context)),
)
)
),
),
),
),
IconButton(
key: _historyButtonKey,
onPressed: asyncFiles.when(
data: (data) => data.recent.isNotEmpty
? () => _showRecent(data.recent, ref)
: null,
error: (_, __) => null,
loading: () => null,
asyncData.when(
data: (data) => PopupMenuButton<GoogleFile>(
icon: Icon(Icons.arrow_drop_down),
onSelected: (file) => _onFileSelected(file, ref),
itemBuilder: (context) => data.recent.map((file) {
return PopupMenuItem(
value: file,
child: Text(file.name),
);
}).toList(),
),
error: (_, __) => PopupMenuButton(
icon: Icon(Icons.arrow_drop_down),
itemBuilder: (context) => [],
),
loading: () => PopupMenuButton(
icon: Icon(Icons.arrow_drop_down),
itemBuilder: (context) => [],
),
),
icon: Icon(Icons.history),
iconSize: Dimension.historyIconButtonSize,
)
],
);
}

void _showRecent(List<GoogleFile> recent, WidgetRef ref) async {
final BuildContext context = _historyButtonKey.currentContext!;
final RenderBox renderBox = context.findRenderObject() as RenderBox;
final Offset position = renderBox.localToGlobal(Offset.zero);
final Size size = renderBox.size;
final x = position.dx + size.width;
final y = position.dy + size.height;

final selected = await showMenu(
context: context,
position: RelativeRect.fromLTRB(x, y, x, y),
items: recent.map((file) => PopupMenuItem(
value: file,
child: Text(file.name),
)).toList(),
);
if (selected != null) {
ref.read(filesInfoHolderProvider.notifier).select(selected);
}
}

Widget _fileWidget(String? text, BuildContext context) {
return Text(
text ?? AppLocalizations.of(context).hintSelectFile,
style: TextStyle(
// TODO implement use theme
color:text == null ? Colors.grey : Colors.black
),
],
)
);
}
}

0 comments on commit 646eb68

Please sign in to comment.