flutter 改变list中的值,setstate无效

新手上路,请多包涵
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(
            ),
          ),
        )
    );
  }
}
阅读 3.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题