当显示在模式表中时,SwitchListTile不会更改值。 [扑]

时间:2019-12-04 04:35:13

标签: flutter flutter-layout

我有一个bottommodalsheet,其中我定义了一个SwitchListTile小部件-


  bool isSwitched = false;


  void _onSwitchChanged(bool value) {
//    setState(() {
    isSwitched = value;
//    });
  }

void _showModalSheet() {
    showModalBottomSheet(context: context,backgroundColor: Color(redcolor),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20.0),
        ),
        isScrollControlled: true,
//        useRootNavigator: true,
        builder: (builder) {
      return Container(
//        height: 500,
        height: MediaQuery.of(context).copyWith().size.height * 0.50,//        width: MediaQuery.of(context).size.width,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SwitchListTile(
              secondary: Icon(Icons.notifications, color: Colors.white,),
              title: whitefontstyleBebas(text: "Notification", size: 20,), // just a custom font, otherwise a regular Text widget
              value: isSwitched,
              onChanged: (bool value){
                setState(() {
                  _onSwitchChanged(value);

                });
              },
            ),
            ],
          ),
      );
      }
    );
  }

当我在模式表中定义此开关时,ontap属性似乎并未将开关切换为打开或关闭。但是,当我在模式表之外定义此switchListTile时,单击它会更改其值。有人可以建议我哪里出问题了吗?

1 个答案:

答案 0 :(得分:0)

您需要在StatefulWidget中包含BottomSheet

  void _settingModalBottomSheet(context) {
    showModalBottomSheet(
        context: context,
        builder: (BuildContext bc) {
          return bottom();// return a StatefulWidget widget 
        });
  }

然后像tis一样声明您的StatefulWidget小部件,

class bottom extends StatefulWidget {
  @override
  _bottomState createState() => _bottomState();
}

class _bottomState extends State<bottom> {
  bool isSwitched = false;


  void _onSwitchChanged(bool value) {
//    setState(() {
    isSwitched = value;
//    });
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Wrap(
        children: <Widget>[

          SwitchListTile(
            secondary: Icon(Icons.notifications, color: Colors.white,),
            title:Text('Notification'), // just a custom font, otherwise a regular Text widget
            value: isSwitched,
            onChanged: (bool value){
              setState(() {
                _onSwitchChanged(value);

              });
            },
          ),

        ],
      ),
    );
  }
}

输出:

enter image description here