From 8719682b7eff7b4f64b338d2cc20c1917bc7f41e Mon Sep 17 00:00:00 2001 From: stonega Date: Sat, 13 Nov 2021 13:06:09 +0800 Subject: [PATCH 1/2] Support change children via set state --- example/lib/main.dart | 22 +++++++++++++++++++--- lib/src/resizable_widget.dart | 14 +++++++++++--- lib/src/resizable_widget_controller.dart | 4 ++++ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 3400ddb..2a2856c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -18,14 +18,30 @@ class MyApp extends StatelessWidget { } } -class MyPage extends StatelessWidget { +class MyPage extends StatefulWidget { const MyPage({Key? key}) : super(key: key); + @override + State createState() => _MyPageState(); +} + +class _MyPageState extends State { + bool _showFirst = true; + @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Resizable Widget Example'), + actions: [ + IconButton( + onPressed: () { + setState(() { + _showFirst = !_showFirst; + }); + }, + icon: const Icon(Icons.settings)), + ], ), body: ResizableWidget( isHorizontalSeparator: false, @@ -34,7 +50,7 @@ class MyPage extends StatelessWidget { separatorSize: 4, onResized: _printResizeInfo, children: [ - Container(color: Colors.greenAccent), + if(_showFirst) Container(color: Colors.greenAccent), ResizableWidget( isHorizontalSeparator: true, separatorColor: Colors.blue, @@ -47,7 +63,7 @@ class MyPage extends StatelessWidget { Container(color: Colors.yellowAccent), Container(color: Colors.redAccent), ], - percentages: const [0.2, 0.5, 0.3], + percentages: const [0.2, 0.5, 0.3] ), Container(color: Colors.redAccent), ], diff --git a/lib/src/resizable_widget.dart b/lib/src/resizable_widget.dart index c1ecc43..e8536a8 100644 --- a/lib/src/resizable_widget.dart +++ b/lib/src/resizable_widget.dart @@ -72,15 +72,23 @@ class ResizableWidget extends StatefulWidget { } class _ResizableWidgetState extends State { - late ResizableWidgetArgsInfo _info; late ResizableWidgetController _controller; @override void initState() { super.initState(); - - _info = ResizableWidgetArgsInfo(widget); _controller = ResizableWidgetController(_info); + } + + ResizableWidgetArgsInfo get _info => ResizableWidgetArgsInfo(widget) ; + + @override + void didUpdateWidget(covariant ResizableWidget oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.children != widget.children) { + _controller = ResizableWidgetController(_info); + _controller.update(); + } } @override diff --git a/lib/src/resizable_widget_controller.dart b/lib/src/resizable_widget_controller.dart index f0f86a3..86be84e 100644 --- a/lib/src/resizable_widget_controller.dart +++ b/lib/src/resizable_widget_controller.dart @@ -28,6 +28,10 @@ class ResizableWidgetController { _model.callOnResized(); } + void update() { + eventStream.add(this); + } + void tryHideOrShow(int separatorIndex) { final result = _model.tryHideOrShow(separatorIndex); From e0a2e66904d4d4600ca85bb33eaf1b4bb5c37e79 Mon Sep 17 00:00:00 2001 From: stonega Date: Sat, 13 Nov 2021 13:17:25 +0800 Subject: [PATCH 2/2] update button name --- example/lib/main.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 2a2856c..496019e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -34,13 +34,13 @@ class _MyPageState extends State { appBar: AppBar( title: const Text('Resizable Widget Example'), actions: [ - IconButton( + TextButton( onPressed: () { setState(() { _showFirst = !_showFirst; }); }, - icon: const Icon(Icons.settings)), + child: const Text('Change chidren')), ], ), body: ResizableWidget(