Navigator.push и AutomaticKeepAliveClientMixin: сохранение состояния виджета с сохранением состояния после переключения экрана - PullRequest
0 голосов
/ 07 октября 2019

Проблема: 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;
}
...