Flutter 如何在颤振中动态切换(映射)设定值?

Flutter 如何在颤振中动态切换(映射)设定值?,flutter,dart,Flutter,Dart,创建动态列表(请求),我在其中放置了一个开关。 但是,开关拒绝显示其预期状态(单击时状态不会更改) import'包装:flift/cupertino.dart'; 进口“包装:颤振/材料.省道”; 根据请求导入“package:scat/request.dart”; 将“package:scat/util.dart”导入为util; 类_ConfigPushState扩展状态{ 未来配置列表; 映射_onOffMap={}; @凌驾 void initState(){ super.initSt

创建动态列表(请求),我在其中放置了一个开关。 但是,开关拒绝显示其预期状态(单击时状态不会更改)

import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
根据请求导入“package:scat/request.dart”;
将“package:scat/util.dart”导入为util;
类_ConfigPushState扩展状态{
未来配置列表;
映射_onOffMap={};
@凌驾
void initState(){
super.initState();
configList=request.configList();
}
@凌驾
无效处置(){
super.dispose();
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:首选大小(
preferredSize:Size.fromHeight(50.0),//这里是所需的高度
孩子:新的AppBar(
背景颜色:Colors.black,
领先:新图标按钮(
图标:新图标(图标。关闭),
onPressed:()=>Navigator.of(context.pop(),
),
标题:对,
标题:新文本('API설정', 样式:util.appTitleStyle),
),
),
正文:中(
孩子:未来建设者(
未来:配置列表,
生成器:(上下文,快照){
if(snapshot.connectionState!=connectionState.done){
返回循环ProgressIndicator();
}
if(snapshot.hasError){
返回循环ProgressIndicator();
}
_onOffMap={};
返回ListView.separated(
separatorBuilder:(上下文,索引)=>Divider(),
itemCount:snapshot.data.data.length,
itemBuilder:(上下文,索引){
var row=snapshot.data.data[index];
变量类型=行['type']作为字符串;
var值=行['value']作为布尔值;
var disabled=行['isDisabled']为布尔值;
var subtitle=已禁用?“아직 준비중' : 行['description'];
_onOffMap[type]=值;
//_arr.add(增值);
//_灯=值;
打印('item builder${type}');
返回容器(
高度:util.isEmpty(副标题)?50:70,
孩子:新的ListTile(
标题:新文本(行['name']),
副标题:新文本(副标题),
拖曳:CupertinoSwitch(
activeColor:Colors.deepPurple,
//值:_arr[索引],
//值:_灯,
值:_onOffMap[type],
一旦更改:(布尔值){
设置状态(){
如果(禁用){
Scaffold.of(上下文).showSnackBar(
SnackBar(内容:文本)개발중..')));
返回;
}
//_灯=值;
打印(“在$onOffMap$类型之前”);
_onOffMap[type]=值;
打印(“在$onOffMap之后”);
要求
.pushSet(类型、值.toString()、“”、“”)
.然后((a){
Scaffold.of(上下文).showSnackBar(
SnackBar(内容:文本)처리 되었습니다.')));
});
});
},
),
));
});
})),
);
}
}

试试这个我希望它对你有用:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:scat/request.dart' as request;
import 'package:scat/util.dart' as util;

class _ConfigPushState extends State<ConfigPush> {
  Future<request.ApiResult> configList;
  Map<String, bool> _onOffMap = {};
  @override
  void initState() {
    super.initState();
    configList = request.configList();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(50.0), // here the desired height
        child: new AppBar(
          backgroundColor: Colors.black,
          leading: new IconButton(
            icon: new Icon(Icons.close),
            onPressed: () => Navigator.of(context).pop(),
          ),
          centerTitle: true,
          title: new Text('API 설정', style: util.appTitleStyle),
        ),
      ),
      body: Center(
          child: FutureBuilder<request.ApiResult>(
              future: configList,
              builder: (context, snapshot) {
                if (snapshot.connectionState != ConnectionState.done) {
                  return CircularProgressIndicator();
                }
                if (snapshot.hasError) {
                  return CircularProgressIndicator();
                }
                _onOffMap = {
                   "type": true;
                };
                return ListView.separated(
                    separatorBuilder: (context, index) => Divider(),
                    itemCount: snapshot.data.data.length,
                    itemBuilder: (context, index) {
                      var row = snapshot.data.data[index];

                      var type = row['type'] as String;
                      var value = row['value'] as bool;
                      var disabled = row['isDisabled'] as bool;
                      var subtitle = disabled ? '아직 준비중' : row['description'];
                      _onOffMap["type"] = value;
                      // _arr.add(value);
                      // _lights = value;

                      print('item builder ${type}');
                      return Container(
                          height: util.isEmpty(subtitle) ? 50 : 70,
                          child: new ListTile(
                            title: new Text(row['name']),
                            subtitle: new Text(subtitle),
                            trailing: CupertinoSwitch(
                              activeColor: Colors.deepPurple,
                              // value: _arr[index],
                              // value: _lights,
                              value: _onOffMap["type"],
                              onChanged: (bool value) {
                                setState(() {
                                  if (disabled) {
                                    Scaffold.of(context).showSnackBar(
                                        SnackBar(content: Text('개발중..')));
                                    return;
                                  }

                                  // _lights = value;
                                  print("before $_onOffMap $type");
                                  _onOffMap["type"] = value;
                                  print("after $_onOffMap");

                                  request
                                      .pushSet(type, value.toString(), "", "")
                                      .then((a) {
                                    Scaffold.of(context).showSnackBar(
                                        SnackBar(content: Text('처리 되었습니다.')));
                                  });
                                });
                              },
                            ),
                          ));
                    });
              })),
    );
  }
}
import'包装:flift/cupertino.dart';
进口“包装:颤振/材料.省道”;
根据请求导入“package:scat/request.dart”;
将“package:scat/util.dart”导入为util;
类_ConfigPushState扩展状态{
未来配置列表;
映射_onOffMap={};
@凌驾
void initState(){
super.initState();
configList=request.configList();
}
@凌驾
无效处置(){
super.dispose();
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:首选大小(
preferredSize:Size.fromHeight(50.0),//这里是所需的高度
孩子:新的AppBar(
背景颜色:Colors.black,
领先:新图标按钮(
图标:新图标(图标。关闭),
onPressed:()=>Navigator.of(context.pop(),
),
标题:对,
标题:新文本('API설정', 样式:util.appTitleStyle),
),
),
正文:中(
孩子:未来建设者(
未来:配置列表,
生成器:(上下文,快照){
if(snapshot.connectionState!=connectionState.done){
返回循环ProgressIndicator();
}
if(snapshot.hasError){
返回循环ProgressIndicator();
}
_onOffMap={
“类型”:正确;
};
返回ListView.separated(
separatorBuilder:(上下文,索引)=>Divider(),
itemCount:snapshot.data.data.length,
itemBuilder:(上下文,索引){
var row=snapshot.data.data[index];
变量类型=行['type']作为字符串;
var值=行['value']作为布尔值;
var disabled=行['isDisabled']为布尔值;
var subtitle=已禁用?“아직 준비중' : 一行
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:scat/request.dart' as request;
import 'package:scat/util.dart' as util;

class _ConfigPushState extends State<ConfigPush> {
  Future<request.ApiResult> configList;
  Map<String, bool> _onOffMap = {};
  @override
  void initState() {
    super.initState();
    configList = request.configList();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(50.0), // here the desired height
        child: new AppBar(
          backgroundColor: Colors.black,
          leading: new IconButton(
            icon: new Icon(Icons.close),
            onPressed: () => Navigator.of(context).pop(),
          ),
          centerTitle: true,
          title: new Text('API 설정', style: util.appTitleStyle),
        ),
      ),
      body: Center(
          child: FutureBuilder<request.ApiResult>(
              future: configList,
              builder: (context, snapshot) {
                if (snapshot.connectionState != ConnectionState.done) {
                  return CircularProgressIndicator();
                }
                if (snapshot.hasError) {
                  return CircularProgressIndicator();
                }
                _onOffMap = {
                   "type": true;
                };
                return ListView.separated(
                    separatorBuilder: (context, index) => Divider(),
                    itemCount: snapshot.data.data.length,
                    itemBuilder: (context, index) {
                      var row = snapshot.data.data[index];

                      var type = row['type'] as String;
                      var value = row['value'] as bool;
                      var disabled = row['isDisabled'] as bool;
                      var subtitle = disabled ? '아직 준비중' : row['description'];
                      _onOffMap["type"] = value;
                      // _arr.add(value);
                      // _lights = value;

                      print('item builder ${type}');
                      return Container(
                          height: util.isEmpty(subtitle) ? 50 : 70,
                          child: new ListTile(
                            title: new Text(row['name']),
                            subtitle: new Text(subtitle),
                            trailing: CupertinoSwitch(
                              activeColor: Colors.deepPurple,
                              // value: _arr[index],
                              // value: _lights,
                              value: _onOffMap["type"],
                              onChanged: (bool value) {
                                setState(() {
                                  if (disabled) {
                                    Scaffold.of(context).showSnackBar(
                                        SnackBar(content: Text('개발중..')));
                                    return;
                                  }

                                  // _lights = value;
                                  print("before $_onOffMap $type");
                                  _onOffMap["type"] = value;
                                  print("after $_onOffMap");

                                  request
                                      .pushSet(type, value.toString(), "", "")
                                      .then((a) {
                                    Scaffold.of(context).showSnackBar(
                                        SnackBar(content: Text('처리 되었습니다.')));
                                  });
                                });
                              },
                            ),
                          ));
                    });
              })),
    );
  }
}