首页 新闻 会员 周边 捐助

在flutter里面, 我用相同的setState方法来刷新数据,一段代码可以刷新数据,一段代码不可以刷新数据,请高手帮忙看一下,这是为何呢?非常感谢。

0
悬赏园豆:20 [已关闭问题] 关闭于 2021-12-02 16:07

在flutter里面,
我用相同的setState方法来刷新数据,一段代码可以刷新数据,一段代码不可以刷新数据,请高手帮忙看一下,这是为何呢?非常感谢。
通过print listindex确认菜单按钮已经传值给了数据列表函数:

setIndex(var listinput) {
if (listindex.isEmpty) {
listindex = listDataHead;
} else {
listindex = listinput;
}
print(listindex);
return (listinput);
}

这段代码不能够刷新数据:

================================================
import 'package:flutter/material.dart';
import 'package:chinamedicine2/main.dart';
import 'package:flutter/rendering.dart';
import 'tabs.dart';
import '../res/alldartOnefile.dart';
import '../res/Arthrosis.dart';
import 'dart:async';
import 'routes.dart';
import '../res/Head.dart';
import '../res/Blood.dart';
import '../res/Cold.dart';
import '../res/Cancer.dart';

bool isActive = false;
List listindex = listDataHead;

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

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

class _sort_pageXState extends State<sort_pageX> {
//final GlobalKey<_sort_pageState> key = GlobalKey();
@override
Widget build(BuildContext context) {
debugShowCheckedModeBanner:
false;

return Stack(
  children: <Widget>[
    // Container(
    //   child: sort_toppage(),
    // ),
    Container(
      child: sort_page(),
    ),
    Container(
        alignment: AlignmentDirectional.bottomEnd,
        child: showPopupMenuButton()),
  ],
);

}
}

class sort_page extends StatefulWidget {
@override
_sort_pageState createState() => _sort_pageState();
setIndex(var listinput) {
if (listindex.isEmpty) {
listindex = listDataHead;
} else {
listindex = listinput;
}
//print(listindex);
return (listinput);
}
}

class _sort_pageState extends State<sort_page> {
final GlobalKey<_sort_pageState> key = GlobalKey();

List<Widget> _getListData() {
var tempList = listindex.map((value) {
return Container(
// padding: EdgeInsets.all(10),
// height: 120,

    child: ListTile(
      isThreeLine: true,
      leading: const Icon(
        Icons.medication_outlined,
        color: Colors.blue,
        size: 50,
      ),
      title: Text('疾病名称:' + value["illsort"]),
      subtitle: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text('疾病细分类别:' + value["illsortdetails"]),
          Text('药名:' + value["title"],
              style: TextStyle(fontWeight: FontWeight.bold)),
          Text('组成:' + value["material"]),
          Text('类型:' + value["model"]),
          Text('功能:' + value["function"]),
          Text('用量:' + value["qty"]),
          Text('注意:' + value["attention"],
              style: TextStyle(fontWeight: FontWeight.bold)),
          Text('规格:' + value["spec"]),
          Text('保存:' + value["store"]),
        ],
      ),
    ),
    //),
    decoration: BoxDecoration(
        boxShadow: [
          BoxShadow(
            color: Colors.grey,
            offset: Offset(0.0, 4.0),
            blurRadius: 0.2,
            spreadRadius: 0.3,
          ),
        ],
        borderRadius: BorderRadius.circular(20.0),
        border: Border.all(
          color: Color.fromRGBO(23, 23, 23, 20),
          width: 1.0,
        ),
        color: Colors.grey,
        gradient: LinearGradient(
            colors: [Colors.grey, Colors.white],
            begin: Alignment.centerRight,
            end: Alignment(0.8, 0.0),
            tileMode: TileMode.clamp)),
  );
});
return tempList.toList();

}

@override
Widget build(BuildContext context) {
return GridView.count(
crossAxisSpacing: 20.0,
mainAxisSpacing: 10.0,
padding: EdgeInsets.all(10),
crossAxisCount: 1,
//childAspectRatio: 0.7,
children: _getListData(),
);
}
}

//====================

class showPopupMenuButton extends StatefulWidget {
showPopupMenuButton({Key? key}) : super(key: key);
final GlobalKey<_sort_pageState> key = GlobalKey();

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

class _showPopupMenuButtonState extends State<showPopupMenuButton> {
@override
Widget build(BuildContext context) {
return Container(
child: PopupMenuButton<String>(
icon: Icon(
Icons.settings,
color: Colors.black54,
),
onSelected: (String string) {},
itemBuilder: (BuildContext context) =>
// taskPopMenu()
<PopupMenuItem<String>>[
PopupMenuItem(
child: Row(
children: [
Icon(
Icons.label_important,
color: Colors.teal[100],
),
SizedBox(
width: 10,
),
Text("头部疾病"),
],
),
value: 'Head',
onTap: () { //不能刷新列表数据
sort_page sp2 = new sort_page();
sp2.setIndex(listDataHead);
//setIndex(listDataCold);
// key.currentState!.changeState();
setState(() {
isActive = !isActive;
print(isActive);
_sort_pageState();
});
}),
PopupMenuItem(
child: Text("感冒中暑瘟疫疾病"),
value: 'Cold',
onTap: () {
sort_page sp2 = new sort_page();
sp2.setIndex(listDataCold);
//setIndex(listDataCold);
// key.currentState!.changeState();
setState(() {
isActive = !isActive;
_sort_pageState();
});
}),
PopupMenuItem(
child: Text("癌症肿瘤疾病"),
value: "Cancer",
onTap: () {
sort_page sp2 = new sort_page();
sp2.setIndex(listDataCancer);
//setIndex(listDataCold);
// key.currentState!.changeState();
setState(() {
isActive = !isActive;
_sort_pageXState();
_sort_pageState();
});
}),
PopupMenuItem(
child: Text("血液系统疾病"),
value: "Blood",
onTap: () {
sort_page sp2 = new sort_page();
sp2.setIndex(listDataBlood);
//setIndex(listDataCold);
// key.currentState!.changeState();
setState(() {
isActive = !isActive;
_sort_pageState();
});
}),
]),
);
}
}

================================================

这段代码可以刷新数据:

import 'popmenu.dart';
import 'package:flutter/material.dart';
import 'Cancer.dart';
import 'Blood.dart';
import 'Cold.dart';
import 'Head.dart';

void main() {
runApp(sort_pageX());
}

class sort_pageX extends StatefulWidget {
sort_pageX({Key? key}) : super(key: key);
@override
_sort_pageXState createState() => _sort_pageXState();
}

class _sort_pageXState extends State<sort_pageX> {
final GlobalKey<_sort_pageState> key = GlobalKey();

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: sort_page(),
appBar: new AppBar(
title: new Text('首页'),
leading: new Icon(Icons.home),
backgroundColor: Colors.blue,
centerTitle: true,
actions: <Widget>[
// 非隐藏的菜单
new IconButton(
icon: new Icon(Icons.add_alarm),
tooltip: 'Add Alarm',
onPressed: () { //可以刷新数据列表
sort_page sp2 = new sort_page();
sp2.setIndex(listDataBlood);
//setIndex(listDataCold);
// key.currentState!.changeState();
setState(() {
isActive = !isActive;

              _sort_pageState();
            });
          }),
      // // 隐藏的菜单
      // new PopupMenuButton<String>(
      //     itemBuilder: (BuildContext context) => <PopupMenuItem<String>>[
      //         this.SelectView(Icons.message, '发起群聊', 'A'),
      //         this.SelectView(Icons.group_add, '添加服务', 'B'),
      //         this.SelectView(Icons.cast_connected, '扫一扫码', 'C'),
      //     ],
      //     onSelected: (String action) {
      //         // 点击选项的时候
      //         switch (action) {
      //             case 'A': break;
      //             case 'B': break;
      //             case 'C': break;
      //         }
      //     },
      // ),
    ],
  ),
));

}
}

bool isActive = false;
List listindex = listDataHead;

class sort_page extends StatefulWidget {
@override
_sort_pageState createState() => _sort_pageState();
setIndex(var listinput) {
if (listindex.isEmpty) {
listindex = listDataHead;
} else {
listindex = listinput;
}
//print(listindex);
return (listinput);
}
}

class _sort_pageState extends State<sort_page> {
List<Widget> _getListData() {
var tempList = listindex.map((value) {
return ConstrainedBox(
constraints: BoxConstraints(maxHeight: double.infinity),
child: Container(
padding: EdgeInsets.all(10),
// height: 120,
child: ListTile(
isThreeLine: true,
leading: const Icon(
Icons.medication_outlined,
color: Colors.blue,
size: 50,
),
title: Text('疾病名称:' + value["illsort"]),
subtitle: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text('疾病细分类别:' + value["illsortdetails"]),
Text('药名:' + value["title"],
style: TextStyle(fontWeight: FontWeight.bold)),
Text('组成:' + value["material"]),
Text('类型:' + value["model"]),
Text('功能:' + value["function"]),
Text('用量:' + value["qty"]),
Text('注意:' + value["attention"],
style: TextStyle(fontWeight: FontWeight.bold)),
Text('规格:' + value["spec"]),
Text('保存:' + value["store"]),
],
),
),
//),
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(0.0, 4.0),
blurRadius: 0.2,
spreadRadius: 0.3,
),
],
borderRadius: BorderRadius.circular(20.0),
border: Border.all(
color: Color.fromRGBO(23, 23, 23, 20),
width: 1.0,
),
color: Colors.grey,
gradient: LinearGradient(
colors: [Colors.grey, Colors.white],
begin: Alignment.centerRight,
end: Alignment(0.8, 0.0),
tileMode: TileMode.clamp)),
));
});
return tempList.toList();
}

@override
Widget build(BuildContext context) {
return GridView.count(
crossAxisSpacing: 20.0,
mainAxisSpacing: 10.0,
padding: EdgeInsets.all(10),
crossAxisCount: 1,
//childAspectRatio: 0.7,

  children: _getListData(),
);

}
}

================================================

商君治国安邦之张莽的主页 商君治国安邦之张莽 | 初学一级 | 园豆:137
提问于:2021-12-01 19:35
< >
分享
所有回答(1)
0

用provider解决了问题。谢谢

商君治国安邦之张莽 | 园豆:137 (初学一级) | 2021-12-02 16:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册