Skip to content

Commit

Permalink
fix: button
Browse files Browse the repository at this point in the history
  • Loading branch information
guchengxi1994 committed Jun 11, 2024
1 parent 67a4074 commit c1629c9
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 69 deletions.
Binary file added dlls/msvcp140.dll
Binary file not shown.
Binary file added dlls/vcruntime140.dll
Binary file not shown.
Binary file added dlls/vcruntime140_1.dll
Binary file not shown.
131 changes: 68 additions & 63 deletions lib/opening_page/components/buttons.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:bonfire/parallax/camera_parallax.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:make_a_dream/common/logger_utils.dart';
import 'package:make_a_dream/opening_page/notifiers/buttons_notifier.dart';
Expand All @@ -13,19 +13,10 @@ class OpeningScreenButtons extends ConsumerStatefulWidget {
}

class _OpeningScreenButtonsState extends ConsumerState<OpeningScreenButtons> {
final FocusNode _focusNode1 = FocusNode(debugLabel: "1");
final FocusNode _focusNode2 = FocusNode(debugLabel: "2");
final FocusNode _focusNode3 = FocusNode(debugLabel: "3");
final FocusNode _focusNode4 = FocusNode(debugLabel: "4");

late final nodeList = [_focusNode1, _focusNode2, _focusNode3, _focusNode4];
final FocusNode _focusNode = FocusNode();

@override
void dispose() {
_focusNode1.dispose();
_focusNode2.dispose();
_focusNode3.dispose();
_focusNode4.dispose();
super.dispose();
}

Expand All @@ -41,22 +32,42 @@ class _OpeningScreenButtonsState extends ConsumerState<OpeningScreenButtons> {
decoration: BoxDecoration(color: Colors.lightBlue.withOpacity(0.1)),
// padding: const EdgeInsets.only(bottom: 20),
alignment: Alignment.center,
child: FocusTraversalGroup(
policy: WidgetOrderTraversalPolicy(
requestFocusCallback: (node,
{alignment, alignmentPolicy, curve, duration}) {
// print(node.debugLabel);
logger.info("[flutter] switch to ${node.debugLabel}");
ref
.read(buttonProvider.notifier)
.changeStateWithDebugLabel(node.debugLabel!);
},
),
child: Focus(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (node, event) {
// print(event);
if (event is KeyDownEvent) {
return KeyEventResult.ignored;
}

if (event.logicalKey == LogicalKeyboardKey.enter) {
if (ref.read(buttonProvider).current?.onTap != null) {
logger.info(
"[flutter] current button id ${ref.read(buttonProvider).current?.debugLabel}");
ref.read(buttonProvider).current?.onTap!(context, ref);
return KeyEventResult.handled;
}
} else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
ref.read(buttonProvider.notifier).next();
logger.info(
"[flutter] next button id ${ref.read(buttonProvider).current?.debugLabel}");

return KeyEventResult.handled;
} else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
ref.read(buttonProvider.notifier).previous();
logger.info(
"[flutter] previous button id ${ref.read(buttonProvider).current?.debugLabel}");

return KeyEventResult.handled;
}

return KeyEventResult.ignored;
},
child: FittedBox(
child: Column(
children: ButtonsNotifier.defaultModels
.mapIndexed(
(i, v) => _Button(node: nodeList[i], model: v))
.map((v) => _Button(model: v))
.toList()),
)),
),
Expand All @@ -66,53 +77,47 @@ class _OpeningScreenButtonsState extends ConsumerState<OpeningScreenButtons> {
}

class _Button extends ConsumerWidget {
const _Button({required this.node, required this.model});
const _Button({required this.model});
final ButtonModel model;
final FocusNode node;

@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(buttonProvider);
bool focused = model.debugLabel == state.current?.debugLabel;

return Focus(
focusNode: node,
autofocus: focused,
child: MouseRegion(
onEnter: (event) {
ref
.read(buttonProvider.notifier)
.changeStateWithDebugLabel(model.debugLabel);
},
onExit: (event) {
ref.read(buttonProvider.notifier).changeState(null);
},
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: () {
if (model.onTap != null) {
model.onTap!(context, ref);
}
},
child: Container(
width: 200,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: focused
? Colors.lightBlue.withOpacity(0.5)
: Colors.transparent),
child: Center(
child: Text(
model.content,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 30),
),
),
return MouseRegion(
onEnter: (event) {
ref
.read(buttonProvider.notifier)
.changeStateWithDebugLabel(model.debugLabel);
},
onExit: (event) {
ref.read(buttonProvider.notifier).changeState(null);
},
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: () {
model.onTap!(context, ref);
},
child: Container(
width: 200,
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: focused && model.onTap != null
? Colors.lightBlue.withOpacity(0.5)
: Colors.transparent),
child: Center(
child: Text(
model.content,
style: TextStyle(
color: model.onTap != null ? Colors.white : Colors.grey,
fontWeight: FontWeight.bold,
fontSize: 30),
),
),
));
),
),
);
}
}
63 changes: 57 additions & 6 deletions lib/opening_page/notifiers/buttons_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:make_a_dream/opening_page/notifiers/player_notifier.dart';
typedef OnTap = void Function(BuildContext context, WidgetRef ref);

class ButtonModel {
final String debugLabel;
final int debugLabel;
final String content;
final OnTap? onTap;
ButtonModel(
Expand Down Expand Up @@ -42,7 +42,7 @@ class ButtonState {
class ButtonsNotifier extends AutoDisposeNotifier<ButtonState> {
static List<ButtonModel> defaultModels = [
ButtonModel(
debugLabel: "1",
debugLabel: 1,
content: "起",
onTap: (context, ref) async {
final String? name = await showGeneralDialog(
Expand All @@ -68,7 +68,7 @@ class ButtonsNotifier extends AutoDisposeNotifier<ButtonState> {
),
ButtonModel(
content: "承",
debugLabel: "2",
debugLabel: 2,
onTap: (context, ref) {
final playerState = ref.read(playerProvider);
final last = playerState.records.lastOrNull;
Expand All @@ -80,10 +80,10 @@ class ButtonsNotifier extends AutoDisposeNotifier<ButtonState> {
GameInitialRoute.open(context);
},
),
ButtonModel(content: "转", debugLabel: "3", onTap: null),
ButtonModel(content: "转", debugLabel: 3, onTap: null),
ButtonModel(
content: "合",
debugLabel: "4",
debugLabel: 4,
onTap: (_, __) {
exit(0);
}),
Expand All @@ -100,13 +100,64 @@ class ButtonsNotifier extends AutoDisposeNotifier<ButtonState> {
}
}

changeStateWithDebugLabel(String label) {
changeStateWithDebugLabel(int label) {
if (state.current == null || state.current!.debugLabel != label) {
final c = defaultModels.where((v) => v.debugLabel == label).first;

state = state.copyWith(current: c);
}
}

ButtonModel getById(int id) {
return defaultModels.where((v) => v.debugLabel == id).first;
}

void next() {
if (state.current == null) {
return;
}

int id = state.current!.debugLabel + 1;
if (id == 5) {
id = 1;
}
while (true) {
if (getById(id).onTap == null) {
id++;
} else {
break;
}
}
if (id == 5) {
id = 1;
}

changeStateWithDebugLabel(id);
}

void previous() {
if (state.current == null) {
return;
}

int id = state.current!.debugLabel - 1;
if (id == 0) {
id = 4;
}
while (true) {
if (getById(id).onTap == null) {
id--;
} else {
break;
}
}

if (id == 0) {
id = 4;
}

changeStateWithDebugLabel(id);
}
}

final buttonProvider =
Expand Down
Loading

0 comments on commit c1629c9

Please sign in to comment.