Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

错误的代码写法导致的不可预估的结果。 #333

Closed
moqi2011 opened this issue Sep 19, 2022 · 2 comments
Closed

错误的代码写法导致的不可预估的结果。 #333

moqi2011 opened this issue Sep 19, 2022 · 2 comments

Comments

@moqi2011
Copy link
Contributor

请看下面的例子

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';

class TestPage extends StatefulWidget {
  const TestPage({Key? key}) : super(key: key);

  @override
  State<TestPage> createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {
  bool isSelected = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            BrnCheckbox(
              radioIndex: 0,
              isSelected: isSelected,
              childOnRight: true,
              mainAxisSize: MainAxisSize.max,
              child: Text("test"),
              onValueChangedAtIndex: (index, value) {
                isSelected = value;
                setState(() {});
              },
            ),
            TestWidget(isSelected),
            TestWidget2(isSelected),
            TextButton(
                onPressed: () {
                  isSelected = !isSelected;
                  setState(() {});
                },
                child: Text("修改"))
          ],
        ),
      ),
    );
  }
}

class TestWidget extends StatefulWidget {
  final bool isSelected;

  const TestWidget(this.isSelected, {Key? key}) : super(key: key);

  @override
  State<TestWidget> createState() => _TestWidgetState();
}

class _TestWidgetState extends State<TestWidget> {
  @override
  Widget build(BuildContext context) {
    return Text("TestWidget:${widget.isSelected}");
  }
}

class TestWidget2 extends StatefulWidget {
  final bool isSelected;

  const TestWidget2(this.isSelected, {Key? key}) : super(key: key);

  @override
  State<TestWidget2> createState() => _TestWidget2State();
}

class _TestWidget2State extends State<TestWidget2> {
  late bool _isSelected;

  @override
  void initState() {
    super.initState();
    _isSelected = widget.isSelected;
    print("call TestWidget2 initState");
  }

  @override
  Widget build(BuildContext context) {
    return Text("TestWidget2:${_isSelected}");
  }
}

理论上讲,我点击【修改】按钮后BrnCheckbox状态应该发生同步变化,但实际上不行,_TestPageState中的isSelected变量无法和BrnCheckbox中的状态同步。原因是调用父亲控件的setState方法,并不会重新创建子控件的State。只会调用子控件的didUpdateWidget方法。类似的在_BrnRadioCoreState有处理didUpdateWidget方法貌似解决了这个问题。我觉得BrnCheckbox这种轻量级控件大可不必设置成StatefulWidget。

@moqi2011
Copy link
Contributor Author

@leftcoding
Copy link
Collaborator

问题已知晓,请查看提交代码的建议

violinday added a commit that referenced this issue Sep 30, 2022
* 3.x: (21 commits)
  Update build_test.yml (#348)
  fix: BrnIconButton 传入字体无效问题 (#346)
  add attribute emptyImage (#344)
  增加textInputAction (#337)
  fix BrnToast defaultoffset error (#343)
  fix docs (#342)
  feature: Add BrnSafeDialog (dismiss) instead of Navigator.pop to close the Dialog. (#321)
  BrnBrokenLine: xDial support selected style (#319)
  fix: BrnProgressChart 设置颜色,背景色,以及动画无效 (#326)
  feat: 增加focusNode prop,实现焦点可控 (#328)
  fix: BrnTextSelectFormItem 布局字偏上问题 (#331)
  [perf]: 优化代码:1、将 Container(),Container(width:0, height:0) 替换为 SizedBox.shrink(),2、将 InkWell 替换为 GestureDetector (#325)
  fix #333; (#335)
  Update brn_middle_input_diaolg.dart (#327)
  fix global style BrnCardTitleConfig don't work (#310) (#313)
  fix BrnMultiSelectTagsPicker style error (#316) (#320)
  Update build_test.yml (#323)
  optimize BrnPageLoading fix #295 (#298)
  Create i10n.md
  #fixbug: 修复issues275 (#301)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants