Skip to content

Commit

Permalink
Merge pull request #41 from koji-1009/go_router_builder
Browse files Browse the repository at this point in the history
Add go_router_builder and ShellRoute
  • Loading branch information
koji-1009 authored Apr 6, 2023
2 parents 1d1fec9 + 5ff9f74 commit bb65d10
Show file tree
Hide file tree
Showing 14 changed files with 573 additions and 323 deletions.
4 changes: 1 addition & 3 deletions lib/view/home/full/full_search_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ enum _Field {
}

class FullSearchWidget extends ConsumerWidget {
const FullSearchWidget({
super.key,
});
const FullSearchWidget({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand Down
16 changes: 5 additions & 11 deletions lib/view/home/history/history_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ import 'package:flutter/material.dart';
import 'package:flutter_kokkai_gijiroku/model/entity/memo_result.dart';
import 'package:flutter_kokkai_gijiroku/model/entity/search_params.dart';
import 'package:flutter_kokkai_gijiroku/model/hive/search_history.dart';
import 'package:flutter_kokkai_gijiroku/view/search/search_speech_screen.dart';
import 'package:flutter_kokkai_gijiroku/view/router.dart';
import 'package:flutter_kokkai_gijiroku/view/widget/memo_edit_dialog.dart';
import 'package:flutter_kokkai_gijiroku/view/widget/search_params_list.dart';
import 'package:go_router/go_router.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:intl/intl.dart';

class HistoryWidget extends ConsumerWidget {
const HistoryWidget({
super.key,
});
const HistoryWidget({super.key});

DateFormat get _formatter => DateFormat.yMd().add_Hm();

Expand Down Expand Up @@ -49,12 +46,9 @@ class HistoryWidget extends ConsumerWidget {
clipBehavior: Clip.hardEdge,
child: InkWell(
onTap: () {
context.pushNamed(
SearchSpeechScreen.screenName,
queryParams: {
'q': history.params.uriQuery,
},
);
SearchSpeechRoute(
q: history.params.uriQuery,
).push(context);
},
child: Padding(
padding: const EdgeInsets.all(16),
Expand Down
243 changes: 109 additions & 134 deletions lib/view/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,135 +3,100 @@ import 'package:flutter/material.dart';
import 'package:flutter_kokkai_gijiroku/model/entity/search_params.dart';
import 'package:flutter_kokkai_gijiroku/model/entity/search_state.dart';
import 'package:flutter_kokkai_gijiroku/presenter/search_state_manager.dart';
import 'package:flutter_kokkai_gijiroku/view/home/full/full_search_screen.dart';
import 'package:flutter_kokkai_gijiroku/view/home/history/history_widget.dart';
import 'package:flutter_kokkai_gijiroku/view/home/simple/simple_search_screen.dart';
import 'package:flutter_kokkai_gijiroku/view/search/search_meeting_detail_screen.dart';
import 'package:flutter_kokkai_gijiroku/view/search/search_meeting_summary_screen.dart';
import 'package:flutter_kokkai_gijiroku/view/search/search_speech_screen.dart';
import 'package:flutter_kokkai_gijiroku/view/router.dart';
import 'package:flutter_kokkai_gijiroku/view/search_mode.dart';
import 'package:flutter_kokkai_gijiroku/view/widget/home_app_bar_action.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

enum HomeMode {
simple('/', 'homeScreenSimple'),
full('/full', 'homeScreenFull'),
history('/history', 'homeScreenHistory');
simple('/'),
full('/full'),
history('/history');

final String path;
final String name;

const HomeMode(this.path, this.name);
const HomeMode(this.path);
}

class HomeScreen extends ConsumerWidget {
const HomeScreen({
super.key,
required this.mode,
required this.child,
});

final HomeMode mode;
final Widget child;

int get index => HomeMode.values.indexOf(mode);
/// get current HomeMode from path
HomeMode _getHomeMode(BuildContext context) {
final path = GoRouter.of(context).location;
return HomeMode.values.lastWhere(
(e) => path.startsWith(e.path),
orElse: () => HomeMode.simple,
);
}

@override
Widget build(BuildContext context, WidgetRef ref) {
final screenSize = MediaQuery.of(context).breakpointScreenSize;
final mode = _getHomeMode(context);
final index = HomeMode.values.indexOf(mode);

final Widget body;
final FloatingActionButton? actionButton;

switch (mode) {
case HomeMode.simple:
body = SimpleSearchWidget(
submitAction: () {
actionButton = null;
break;
case HomeMode.full:
actionButton = FloatingActionButton.extended(
label: const Text('検索'),
icon: const Icon(Icons.search_outlined),
tooltip: '検索',
onPressed: () {
final state = ref.read(searchStateManagerProvider);
final text = state.any;

if (text.isEmpty) {
if (state.from != null &&
state.until != null &&
state.from!.isAfter(state.until!)) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('日付の指定が不正です'),
),
);
return;
}
if (state.any.isEmpty &&
state.speaker.isEmpty &&
state.nameOfMeeting.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('検索語を入力してください'),
content: Text('検索語/会議名/発言者名のいずれかを入力してください'),
),
);
return;
}

context.pushNamed(
SearchSpeechScreen.screenName,
queryParams: {
'q': SearchParams(
any: text,
).uriQuery,
},
);
switch (state.mode) {
case SearchMode.meetingDetail:
SearchMeetingDetailRoute(
q: state.fullParams.uriQuery,
).push(context);
break;
case SearchMode.meetingSummary:
SearchMeetingSummaryRoute(
q: state.fullParams.uriQuery,
).push(context);
break;
case SearchMode.speech:
SearchSpeechRoute(
q: state.fullParams.uriQuery,
).push(context);
break;
}
},
);
actionButton = null;
break;
case HomeMode.full:
action() {
final state = ref.read(searchStateManagerProvider);

if (state.from != null &&
state.until != null &&
state.from!.isAfter(state.until!)) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('日付の指定が不正です'),
),
);
return;
}
if (state.any.isEmpty &&
state.speaker.isEmpty &&
state.nameOfMeeting.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('検索語/会議名/発言者名のいずれかを入力してください'),
),
);
return;
}

switch (state.mode) {
case SearchMode.meetingDetail:
context.pushNamed(
SearchMeetingDetailScreen.screenName,
queryParams: {
'q': state.fullParams.uriQuery,
},
);
break;
case SearchMode.meetingSummary:
context.pushNamed(
SearchMeetingSummaryScreen.screenName,
queryParams: {
'q': state.fullParams.uriQuery,
},
);
break;
case SearchMode.speech:
context.pushNamed(
SearchSpeechScreen.screenName,
queryParams: {
'q': state.fullParams.uriQuery,
},
);
break;
}
}
body = const FullSearchWidget();
actionButton = FloatingActionButton.extended(
label: const Text('検索'),
icon: const Icon(Icons.search_outlined),
tooltip: '検索',
onPressed: action,
);
break;
case HomeMode.history:
body = const HistoryWidget();
actionButton = null;
break;
}
Expand All @@ -145,7 +110,7 @@ class HomeScreen extends ConsumerWidget {
HomeAppBarAction(),
],
),
body: body,
body: child,
floatingActionButton: actionButton,
bottomNavigationBar: BottomNavigationBar(
items: const [
Expand Down Expand Up @@ -174,55 +139,65 @@ class HomeScreen extends ConsumerWidget {
},
),
);
} else {
return Scaffold(
appBar: AppBar(
title: const Text('議事録検索'),
actions: const [
HomeAppBarAction(),
],
),
body: Row(
children: [
NavigationRail(
destinations: const [
NavigationRailDestination(
icon: Icon(Icons.search),
label: Text('キーワード検索'),
),
NavigationRailDestination(
icon: Icon(Icons.manage_search),
label: Text('条件検索'),
),
NavigationRailDestination(
icon: Icon(Icons.history),
label: Text('検索履歴'),
),
],
selectedIndex: index,
onDestinationSelected: (index) {
_navigate(
context: context,
index: index,
);
},
),
Expanded(
child: body,
),
],
),
floatingActionButton: actionButton,
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
);
}

return Scaffold(
appBar: AppBar(
title: const Text('議事録検索'),
actions: const [
HomeAppBarAction(),
],
),
body: Row(
children: [
NavigationRail(
destinations: const [
NavigationRailDestination(
icon: Icon(Icons.search),
label: Text('キーワード検索'),
),
NavigationRailDestination(
icon: Icon(Icons.manage_search),
label: Text('条件検索'),
),
NavigationRailDestination(
icon: Icon(Icons.history),
label: Text('検索履歴'),
),
],
selectedIndex: index,
onDestinationSelected: (index) {
_navigate(
context: context,
index: index,
);
},
),
Expanded(
child: child,
),
],
),
floatingActionButton: actionButton,
floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
);
}

void _navigate({
required BuildContext context,
required int index,
}) {
final mode = HomeMode.values[index];
context.goNamed(mode.name);
switch (mode) {
case HomeMode.simple:
const SimpleRoute().go(context);
break;
case HomeMode.full:
const FullRoute().go(context);
break;
case HomeMode.history:
const HistoryRoute().go(context);
break;
}
}
}
Loading

0 comments on commit bb65d10

Please sign in to comment.