list中改变值,setstate无法刷新小部件

龙上 发布于 2020/04/04 18:32
阅读 404
收藏 0

开源之夏第三届火热来袭,高校学生参与赢万元奖金!>>>

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  List<bool> tabsActive = [true,false,false,false,false];

  void changeTabs(int view) {
    print("view:$view");
    for(int i = 0;i < tabsActive.length;i++) {
      tabsActive[i] = false;
    }

    setState(() {
      tabsActive[view-1] = true;
    });
    print(tabsActive);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:Center(
        child: Row(
          children: <Widget>[
            GameTabs("tab1",1,tabsActive[0],changeTabs),
            GameTabs("tab2",2,tabsActive[1],changeTabs),
            GameTabs("tab3",3,tabsActive[2],changeTabs),
            GameTabs("tab4",4,tabsActive[3],changeTabs),
            GameTabs("tab5",5,tabsActive[4],changeTabs),
          ],
        ),
      )
    );
  }
}
typedef CallIntFun = void Function(int);
class GameTabs extends StatefulWidget {
  final String title;
  final int view;
  final bool active;
  CallIntFun changeTabs;
  GameTabs(this.title,this.view,this.active,this.changeTabs);
  @override
  _GameTabsState createState() => _GameTabsState(title,view,active,changeTabs);
}

class _GameTabsState extends State<GameTabs> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  final String title;
  final int view;
  final bool active;
  // 背景色
  Color bgColor;
  CallIntFun changeTabs;


  _GameTabsState(this.title,this.view,this.active,this.changeTabs);

  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
  }

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


  @override
  Widget build(BuildContext context) {
    if (active) {
      return ActiveGameTabs(title);
    }
    return Expanded(
        child: GestureDetector(
          onTap: (){
            changeTabs(view);
          },
          child: Container(
            height: 56,
            padding: EdgeInsets.fromLTRB(0, 6, 0, 0),
            decoration: BoxDecoration(
              color: Color.fromRGBO(235, 235, 235, 1),
              border: Border.all(
                  color: Colors.green,
                  width: 3.0,
                  style: BorderStyle.none
              ),
            ),
            child: Text(
              title,
              textAlign: TextAlign.center,
              style: TextStyle(
              ),
            ),
          ),
        )
    );
  }
}


class ActiveGameTabs extends StatelessWidget {
  final String title;
  ActiveGameTabs(this.title);
  @override
  Widget build(BuildContext context) {
    return Expanded(
        child: Container(
          height: 56,
          padding: EdgeInsets.fromLTRB(0, 6, 0, 0),
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border.all(
                color: Colors.green,
                width: 3.0,
                style: BorderStyle.none
            ),
          ),
          child: Text(
            title,
            textAlign: TextAlign.center,
            style: TextStyle(
            ),
          ),
        )
    );
  }
}

过点击tab中改变活动状态,现在改变list中的值,无法改变状态

 

 

加载中
0
巧克力的口袋
巧克力的口袋

大概是因为因为 setState  只是在调用该方法时,重新执行了当前build方法中的逻辑,而这个getWidget2是在initState中创建的。当前显示的是什么还是什么,getWidget中改变的并不会显示出来。 不确定哈,参考下~

0
巧克力的口袋
巧克力的口袋
if (active) {
  return ActiveGameTabs(title);
}

修改为

if (widget.active) {
  return ActiveGameTabs(title);
}
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部
返回顶部
顶部