在重新启动应用程序进入初始状态

时间:2021-05-30 07:37:40

标签: flutter flutter-layout flutter-dependencies flutter-animation flutter-test

我正在学习颤振,我是新手! 我在 flutter 中做了一个应用程序,在那个主体里面我放了一个容器。 应用程序也有动态页面,可以动态添加到容器中,所以我使用 list.view Builder, 并通过滚动查看此页面。 但是当应用程序关闭并重新启动时,这些页面消失了!! 即使在应用程序重新启动后,我也想将这些页面保留在 Container 中!!! 这是我的代码

import 'package:dynamic_widget/Contact.dart';
import 'package:dynamic_widget/DataBase%20Helper/DataBaseHelper.dart';
import 'package:dynamic_widget/DataBase.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

final DataBaseUnit dataBase = new DataBaseUnit();

void main() {
  runApp( MaterialApp(home: HomeScreen()));
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key key}) : super(key: key);

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

class _HomeScreenState extends State<HomeScreen>  {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('HOME'),
        actions: [
          IconButton(
              icon: Icon(Icons.add),
              onPressed: () {
                setState(() {
                  addWidget();
                });
              }),
        ],
      ),
      body: Container(
        // height: 200,
        padding: EdgeInsets.all(20),
        color: Colors.deepOrange,
        child: Column(
          children: <Widget>[
            Flexible(
              child: ListView.builder(
                scrollDirection: Axis.horizontal,
                itemCount: listWidget.length,
                itemBuilder: (_, index) => listWidget[index],
                padding: EdgeInsets.symmetric(),
              ),
            ),
            FloatingActionButton(
                child: Icon(Icons.add),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => ContentScreen()),
                  );
                })
          ],
        ),
      ),
    );
  }

/////////////////////////////////////////////////////////////////////




/////////////////////////////////////////////////////////////
List<Widget> listWidget = [];

// List<DynamicWidget2> listWidget2 = [];

addWidget() async {
  listWidget.add(new DynamicWidget());
}

addWidget2() {
  listWidget.add(new DynamicWidget2());
}

////////////////////////////////////////////////////////
class DynamicWidget extends StatefulWidget {
  @override
  _DynamicWidgetState createState() => _DynamicWidgetState();
}

class _DynamicWidgetState extends State<DynamicWidget> {
  bool showDialog = false;
  TextEditingController RoomName = new TextEditingController();
  TextEditingController Code = new TextEditingController();
  TextEditingController B1 = new TextEditingController();
  TextEditingController B2 = new TextEditingController();
  TextEditingController B3 = new TextEditingController();
  TextEditingController B4 = new TextEditingController();
  String name = "";
  String code = "";
  String b1 = "";
  String b2 = "";
  String b3 = "";
  String b4 = "";
  @override
  Widget build(BuildContext context) {
    return Container(
        alignment: Alignment.center,
        width: 400,
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(20),
        ),
        child: Container(
          width: 300,
          height: 600,
          padding: EdgeInsets.all(10),
          decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(
                color: Colors.grey.withOpacity(0.3),
                spreadRadius: 15,
                blurRadius: 12,
                offset: Offset(3, 3), // changes position of shadow
              ),
              BoxShadow(
                color: Colors.deepOrange[900].withOpacity(1), spreadRadius: 3,
                blurRadius: 0,
                offset: Offset(-3, -3), // changes position of shadow
              ),
            ],
            borderRadius: BorderRadius.circular(20),
            color: Colors.deepOrange[700],
          ),
          child: SingleChildScrollView(
            dragStartBehavior: DragStartBehavior.down,
            child: Column(
              children: <Widget>[
                showDialog == true
                    ? AlertDialog(
                        content: Column(children: [
                          TextField(
                            decoration: InputDecoration(hintText: "Code"),
                            controller: Code,
                          ),
                          TextField(
                            decoration:
                                InputDecoration(hintText: "Provide Room name"),
                            controller: RoomName,
                          ),
                          TextField(
                            decoration: InputDecoration(
                                hintText: "Provide BUTTON1 name"),
                            controller: B1,
                          ),
                          TextField(
                            decoration: InputDecoration(
                                hintText: "Provide BUTTON2 name"),
                            controller: B2,
                          ),
                          TextField(
                            decoration: InputDecoration(
                                hintText: "Provide BUTTON3 name"),
                            controller: B3,
                          ),
                          TextField(
                            decoration: InputDecoration(
                                hintText: "Provide BUTTON4 name"),
                            controller: B4,
                          ),
                        ]),
                        actions: [
                          IconButton(
                              icon: Icon(Icons.add),
                              onPressed: () {
                                setState(() async {
                                  showDialog = false;
                                  name = RoomName.text;
                                  code = Code.text;
                                  b1 = B1.text;
                                  b2 = B2.text;
                                  b3 = B3.text;
                                  b4 = B4.text;

                                  var contact = new Contact(
                                    Code: code,
                                    RoomName: name,
                                    Button1: b1,
                                    Button2: b2,
                                    Button3: b3,
                                    Button4: b4,
                                  );
                                  var db = DataBaseHelper();
                                  await db.create(contact);
                                  print('id created ');
                                });
                              })
                        ],
                      )
                    : Text(''),
                FlatButton(
                  color: Colors.blue,
                  child: Text(name),
                  onLongPress: () {
                    setState(() {
                      showDialog = true;
                    });
                  },
                  onPressed: () {},
                ),
                SizedBox(
                  height: 40,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Flexible(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(10.0),
                        ),
                        onLongPress: () {
                          showDialog = true;
                        },
                        onPressed: () {},
                        color: Colors.deepOrangeAccent[200],
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Icon(
                              Icons.lightbulb,
                              color: Colors.yellow,
                              size: 90,
                            ),
                            Text(
                              b1,
                              textScaleFactor: 1,
                            )
                          ],
                        ),
                      ),
                    ),
                    Flexible(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(10.0),
                        ),
                        onLongPress: () {
                          showDialog = true;
                        },
                        onPressed: () {},
                        color: Colors.deepOrangeAccent[200],
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Icon(
                              Icons.lightbulb,
                              color: Colors.yellow,
                              size: 90,
                            ),
                            Text(
                              b2,
                              textScaleFactor: 1,
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 40,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Flexible(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(10.0),
                        ),
                        onLongPress: () {
                          showDialog = true;
                        },
                        onPressed: () {},
                        color: Colors.deepOrangeAccent[200],
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Icon(
                              Icons.lightbulb,
                              color: Colors.yellow,
                              size: 90,
                            ),
                            Text(
                              b3,
                              textScaleFactor: 1,
                            )
                          ],
                        ),
                      ),
                    ),
                    Flexible(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(10.0),
                        ),
                        onLongPress: () {
                          showDialog = true;
                        },
                        onPressed: () {},
                        color: Colors.deepOrangeAccent[200],
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Icon(
                              Icons.lightbulb,
                              color: Colors.yellow,
                              size: 90,
                            ),
                            Text(
                              b4,
                              textScaleFactor: 1,
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: 40,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    Flexible(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(10.0),
                        ),
                        onLongPress: () {
                          showDialog = true;
                        },
                        onPressed: () {},
                        color: Colors.deepOrangeAccent[200],
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Icon(
                              Icons.flip_camera_android_rounded,
                              color: Colors.yellow,
                              size: 30,
                            ),
                          ],
                        ),
                      ),
                    ),
                    Flexible(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(10.0),
                        ),
                        onLongPress: () {
                          showDialog = true;
                        },
                        onPressed: () {},
                        color: Colors.deepOrangeAccent[200],
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Icon(
                              Icons.flip_camera_android_rounded,
                              color: Colors.yellow,
                              size: 10,
                            ),
                          ],
                        ),
                      ),
                    ),
                    Flexible(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(10.0),
                        ),
                        onLongPress: () {
                          showDialog = true;
                        },
                        onPressed: () {},
                        color: Colors.deepOrangeAccent[200],
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Icon(
                              Icons.flip_camera_android_rounded,
                              color: Colors.yellow,
                              size: 10,
                            ),
                          ],
                        ),
                      ),
                    ),
                    Flexible(
                      flex: 1,
                      child: RaisedButton(
                        shape: RoundedRectangleBorder(
                          borderRadius: new BorderRadius.circular(10.0),
                        ),
                        onLongPress: () {
                          showDialog = true;
                        },
                        onPressed: () {},
                        color: Colors.deepOrangeAccent[200],
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Icon(
                              Icons.flip_camera_android_rounded,
                              color: Colors.yellow,
                              size: 10,
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ));
  }
}


////////////////////////////////////////////////////////////
class ContentScreen extends StatefulWidget {
  @override
  _ContentScreenState createState() => _ContentScreenState();
}

class _ContentScreenState extends State<ContentScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: ListView(
        children: [
          Container(
            color: Colors.blue,
            child: FlatButton(
                onPressed: () {
                  setState(() {
                    var Widget = addWidget();
                    addWidget();
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) => HomeScreen()));
                  });
                },
                child: Icon(Icons.ac_unit)),
          ),
          Container(
            color: Colors.red,
            child: FlatButton(
                onPressed: () {
                  setState(() {
                    addWidget2();
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) => HomeScreen()));
                  });
                },
                child: Icon(Icons.ac_unit)),
          ),
          Container(
            color: Colors.green,
          ),
          Container(
            height: 80,
            color: Colors.yellow,
          ),
          Container(
            height: 80,
            color: Colors.brown,
          ),
        ],
      ),
    );
  }
}

initial state of app widgit added dynamically

应用重启后又回到初始状态

1 个答案:

答案 0 :(得分:0)

默认情况下,Flutter 不会在应用重新启动时存储任何应用状态。解决您的问题的一种解决方案是使用 shared_preferences 颤振插件。使用它,您可以跨应用重新启动存储数据(在您的情况下应该显示哪些页面),在打开应用时再次获取它并相应地构建您的用户界面。