1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
class ParentWidgetC extends StatefulWidget { @override _ParentWidgetCState createState() => _ParentWidgetCState(); }
class _ParentWidgetCState extends State<ParentWidgetC> { bool _active = false;
void _handleTapboxChanged(bool newValue) { setState(() { _active = newValue; }); }
@override Widget build(BuildContext context) { return Container( child: TapboxC( active: _active, onChanged: _handleTapboxChanged, ), ); } }
class TapboxC extends StatefulWidget { TapboxC({Key? key, this.active: false, required this.onChanged}) : super(key: key);
final bool active; final ValueChanged<bool> onChanged; @override _TapboxCState createState() => _TapboxCState(); }
class _TapboxCState extends State<TapboxC> { bool _highlight = false;
void _handleTapDown(TapDownDetails details) { setState(() { _highlight = true; }); }
void _handleTapUp(TapUpDetails details) { setState(() { _highlight = false; }); }
void _handleTapCancel() { setState(() { _highlight = false; }); }
void _handleTap() { widget.onChanged(!widget.active); }
@override Widget build(BuildContext context) { return GestureDetector( onTapDown: _handleTapDown, onTapUp: _handleTapUp, onTap: _handleTap, onTapCancel: _handleTapCancel, child: Container( child: Center( child: Text( widget.active ? 'Active' : 'Inactive', style: TextStyle(fontSize: 32.0, color: Colors.white), ), ), width: 200.0, height: 200.0, decoration: BoxDecoration( color: widget.active ? Colors.lightGreen[700] : Colors.grey[600], border: _highlight ? Border.all( color: Colors.teal[700], width: 10.0, ) : null, ), ), ); } }
|