|
| 1 | +import 'package:flutter/material.dart' hide LinearGradient; |
| 2 | +import 'package:flutter/rendering.dart'; |
| 3 | + |
| 4 | +class FocusHistoryPane extends StatefulWidget { |
| 5 | + const FocusHistoryPane({super.key, this.width = 200}); |
| 6 | + |
| 7 | + final double width; |
| 8 | + |
| 9 | + @override |
| 10 | + State<FocusHistoryPane> createState() => _FocusHistoryPaneState(); |
| 11 | +} |
| 12 | + |
| 13 | +class _FocusHistoryPaneState extends State<FocusHistoryPane> { |
| 14 | + final _history = <String>[]; |
| 15 | + |
| 16 | + @override |
| 17 | + void initState() { |
| 18 | + super.initState(); |
| 19 | + |
| 20 | + FocusManager.instance.addListener(_onFocusChange); |
| 21 | + } |
| 22 | + |
| 23 | + @override |
| 24 | + void dispose() { |
| 25 | + FocusManager.instance.removeListener(_onFocusChange); |
| 26 | + |
| 27 | + super.dispose(); |
| 28 | + } |
| 29 | + |
| 30 | + void _onFocusChange() { |
| 31 | + final focusedNode = FocusManager.instance.primaryFocus; |
| 32 | + final name = _getFocusName(focusedNode); |
| 33 | + if (_history.lastOrNull == name) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + setState(() { |
| 38 | + _history.add(name); |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + String _getFocusName(FocusNode? focusNode) { |
| 43 | + if (focusNode == null) { |
| 44 | + return "None"; |
| 45 | + } |
| 46 | + |
| 47 | + return focusNode.debugLabel ?? "${focusNode.hashCode}"; |
| 48 | + } |
| 49 | + |
| 50 | + @override |
| 51 | + Widget build(BuildContext context) { |
| 52 | + return SizedBox( |
| 53 | + width: widget.width, |
| 54 | + child: ShaderMask( |
| 55 | + shaderCallback: _fadeOutTopToBottom, |
| 56 | + child: ColoredBox( |
| 57 | + color: Colors.black.withValues(alpha: 0.8), |
| 58 | + child: Column( |
| 59 | + crossAxisAlignment: CrossAxisAlignment.stretch, |
| 60 | + children: [ |
| 61 | + Padding( |
| 62 | + padding: const EdgeInsets.all(12), |
| 63 | + child: Text( |
| 64 | + "FOCUS HISTORY", |
| 65 | + textAlign: TextAlign.center, |
| 66 | + style: TextStyle(color: Colors.white, fontSize: 8, fontWeight: FontWeight.bold), |
| 67 | + ), |
| 68 | + ), |
| 69 | + Expanded( |
| 70 | + child: ListView.builder( |
| 71 | + itemCount: _history.length, |
| 72 | + itemBuilder: (context, index) { |
| 73 | + final historyIndex = _history.length - index - 1; |
| 74 | + final name = _history[historyIndex]; |
| 75 | + |
| 76 | + return Padding( |
| 77 | + padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8), |
| 78 | + child: Text("$historyIndex: $name", style: TextStyle(color: Colors.purpleAccent)), |
| 79 | + ); |
| 80 | + }, |
| 81 | + ), |
| 82 | + ), |
| 83 | + ], |
| 84 | + ), |
| 85 | + ), |
| 86 | + ), |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + Shader _fadeOutTopToBottom(Rect bounds) { |
| 91 | + return LinearGradient( |
| 92 | + colors: [Colors.white, Colors.transparent], |
| 93 | + begin: Alignment.topCenter, |
| 94 | + end: Alignment.bottomCenter, |
| 95 | + stops: [0.3, 0.6], |
| 96 | + ).createShader(bounds); |
| 97 | + } |
| 98 | +} |
0 commit comments