Navigator.push和AutomaticKeepAliveClientMixin:切换屏幕后保留有状态的小部件状态

时间:2019-10-07 01:33:26

标签: flutter navigation preserve

问题: AutomaticKeepAliveClientMixin无法正常工作,每次切换屏幕时都会重建有状态的wigdet。我希望有状态的小部件为每个函数保留其状态,但是我不确定自己做错了什么。 我试图找到另一种解决方案,但真的不知道我需要在代码中进行哪些更改。

main.dart

    class Screen extends StatelessWidget  {
      @override
      Widget build(BuildContext context) {
          ...
         Navigator.push(context, MaterialPageRoute<void>(builder: (context) => Status( FontAwesomeIcons.lightbulb, lightGrey,  600.0, [Colors.grey, Colors.purple], 'Room Lights'),));

        Navigator.push(context, MaterialPageRoute<void>(builder: (context) => Status( FontAwesomeIcons.lightbulb,lightGrey,  600.0, [Colors.grey, Colors.purple], 'Bedroom Light'),));
...

 }}  
    // must be two independent screens

Switch_button.dart

 class Status extends StatefulWidget {

  const Status (
      this.icon, this.color, this.progress, this.colors, this.lightBar);

  final dynamic  lightBar;
  final dynamic icon;
  final dynamic color;
  // final dynamic type;
  final dynamic progress;
  final dynamic colors;


  @override

  _State createState() =>  _State(icon, color, progress, colors, lightBar);
}

//State
 {


   _State (
      this.icon, this.color, this.progress, this.colors, this.lightBar);

   final dynamic  lightBar;
  final dynamic icon;
  final dynamic color;
  // final dynamic type;
  final dynamic progress;
  final dynamic colors;
  String set;
  bool _value1 = false;


  void _onChanged1(bool value) => setState(() => _value1 = value);

  String change(){

    if (_value1 == true) {
      set = 'On';
    } else {
      set = 'Off';
    }

    return set;
  }


  @override
  Widget build(BuildContext context) {

    super.build(context);

    return  Scaffold(

        resizeToAvoidBottomPadding: false,
        backgroundColor: Color(0xFF32323e),

        appBar: PreferredSize(
            preferredSize: Size.fromHeight(65.0),

            child: AppBar( brightness: Brightness.light,title: Text('$lightBar', style: TextStyle(
                fontSize: 24.0,fontWeight: FontWeight.w500, color: Colors.white)),
                backgroundColor: Color(0xFF414350))),

      body:  Container(

          child:  Column(
              crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[ SizedBox(height: 100),
                  Container(
                      padding: const EdgeInsets.all(2.0),
            height: 75.0,
            width: 900,
            color: color,

                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,

                  children: <Widget>[ SwitchListTile(
                     title: Text(change(), style: TextStyle( color: Colors.white,
                       fontSize: 22.0)),
                      activeColor: Colors.purple,
                      secondary: Icon(icon, size: 27.0,
                        color: Colors.white),
                      value: _value1,
                      onChanged: _onChanged1 ),

                    Spacer(),
                    Container(

                      height: 4.0,
                      width: progress,
                      decoration: BoxDecoration(
                          gradient: LinearGradient(
                              colors: colors,
                              begin: Alignment.centerLeft,
                              end: Alignment.centerRight)),
                    )
                  ],
                ))]),
        ),

    );
  }  @override
  bool get wantKeepAlive => true;
}

0 个答案:

没有答案