【问题标题】:Flutter can't preserve stateful state when exit showModalBottomSheet and show showModalBottomSheet again退出 showModalBottomSheet 并再次显示 showModalBottomSheet 时,Flutter 无法保留状态状态
【发布时间】:2022-01-16 13:02:21
【问题描述】:

所以我有以下代码来显示ModalBottomSheet,并在页面上显示过滤器/复选框。如果我退出模式并再次显示 showModalBottomSheet,那么我看不到复选框值得到保留。

似乎我第二次显示模态时,再次调用了 _CheckboxStatefulWidgetState 的 build(),然后将 checkValue 默认为 null。

这里是filter.dart

import 'package:flutter/material.dart';
import '../helper/checkbox.dart';

mixin ListFilter<T extends StatefulWidget> on State<T> {

  dynamic filters = [
    {
      "name": "name1",
      "choices": [
        {'label': 'label1', 'value': 'value1', 'check': false},
        {'label': 'label2', 'value': 'value2', 'check': false},
      ],
    },
    {
      "name": "nameA",
      "choices": [
        {'label': 'labelA', 'value': 'valueA', 'check': false},
        {'label': 'labelB', 'value': 'valueB', 'check': false},
      ],
    },
  ];

  dynamic filterWidgets;

  void showFilterModal(context, dynamic filters) {
    showModalBottomSheet<void>(
      context: context,
      //isScrollControlled: true,
      builder: (BuildContext context) {
        if (filterWidgets == null) {
          print("----> filterWidgets is null");
          filterWidgets = _getFilterWidgets(filters);
        }
        return Container(
          child: Center(
            child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: filterWidgets),
          ),
        );
      },
    );
  }

  List<Widget> _getFilterWidgets(dynamic filters) {
    List<Widget> children = [];

    for (var i = 0; i < filters.length; i++) {
      dynamic filter = filters[i];
      dynamic choices = filter['choices'];

      for (var j = 0; j < choices.length; j++) {
        dynamic choice = choices[i];
        children.add(CheckboxStatefulWidget(
            label: choice['label']));
      }

      Widget divider = Divider();
      children.add(divider);
    }

    return children;
  }
}

这里是checkbox.dart

import 'package:flutter/material.dart';


class CheckboxStatefulWidget extends StatefulWidget {
  final String label;

  CheckboxStatefulWidget({Key key, this.label}) : super(key: key);

  @override
  _CheckboxStatefulWidgetState createState() =>
      _CheckboxStatefulWidgetState();
}

class _CheckboxStatefulWidgetState extends State<CheckboxStatefulWidget> {

  bool checkValue;

  @override
  Widget build(BuildContext context) {

    print("checkValue: $checkValue");
    print(checkValue == true ?? false);

    return CheckboxListTile(
        title: Text(this.widget.label),
        value: checkValue == true ?? false,
        onChanged: (bool newValue) {
          setState(() {
            checkValue = newValue;
          });
        });
  }
}

【问题讨论】:

    标签: flutter dart stateful


    【解决方案1】:

    我想你可以试试这个解决方案:

    • 定义一个变量来存储当前的过滤器选项。
    • 当用户提交选项过滤器时 -> 弹出该过滤器。
    • 注意:当您在 2nd 显示时,Modal 将再次创建。因此,您需要将该选项弹出到父窗口小部件进行存储。
          FilterModel currentFilterOptions;
        
          Future<FilterModel> showFilterModal(context, FilterModel filters) {
            //Show modal 
          }
    
        ...
        showFilterModal(context, currentFilterOptions).then((value) {
          currentFilterOptions = value;
          //DO STH
        });
    

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 2020-12-28
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      相关资源
      最近更新 更多