Flutter学习第四课:SharedPreferences本地存储的简单使用

Flutter学习第四课:SharedPreferences本地存储的简单使用_第1张图片
一:第一步:Main.dart界面

import 'package:flutter/material.dart';
import 'package:flutter_first/storage/storage_sp.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "SharedPreferences示例",
      theme: ThemeData(primarySwatch: Colors.blue),
      home: StorageSp(),
    );
  }
}

第二步:SharePrefences 数据存储界面

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

  @override
  State createState() => _StorageSpState();
}

class _StorageSpState extends State {
  //第一步:实例化本地存储对象
  Future _prefs = SharedPreferences.getInstance();

  //昵称及选择语言的值 文本输入的控制器
  var controller = TextEditingController();

  //定义变量 value_dart,value_js,value_java
  bool value_dart = false;
  bool value_js = false;
  bool value_java = false;

  @override
  void initState() {
    super.initState();
    initFromCache();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("SharedPreferences示例"),
      ),
      body: Container(
        padding: EdgeInsets.all(15),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            TextField(
              controller: controller, //一个自定义控制器,可实现类似TextEditingController
              decoration: InputDecoration(
                  //一个输入框
                  labelText: "昵称:",
                  hintText: "请输入名称"),
            ),
            Text("你喜欢的编程语言"),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("Dart"),
                //Switch 表示开关组件  value选中的值,onChanged 改变时触发的事件
                Switch(
                    value: value_dart,
                    onChanged: (isChanged) {
                      //设置状态改变要存储的值
                      setState(() {
                        this.value_dart = isChanged;
                      });
                    })
              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("JavaScript"),
                Switch(
                    value: value_js,
                    onChanged: (isChanged){
                      //设置状态改变要存储的值
                      setState(() {
                        this.value_js=isChanged;
                      });
                    })

              ],
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("Java"),
                Switch(
                    value: value_java,
                    onChanged: (isChanged){
                      //设置状态改变要存储的值
                      setState(() {
                        this.value_java=isChanged;
                      });
                    })

              ],
            ),

            MaterialButton(
              child: Text("保存"),
                onPressed: (){
                saveInfo(controller.text);
                })


          ],
        ),
      ),
    );
  }

  //从缓存中获取信息填充
  void initFromCache() async {
    //获取SharedPreferences对象
    final SharedPreferences prefs = await _prefs;
    //根据键(key)获取本地存储的值(value)
    final value_nickname = prefs.getString("key_nickname");
    final value_dart = prefs.getBool("key_dart");
    final value_js = prefs.getBool("key_js");
    final value_java = prefs.getBool("key_java");
    //获取到缓存中的值后,使用setState更新界面信息
    setState(() {
      controller.text = (value_nickname == null ? "" : value_nickname);
      this.value_dart = (value_dart == null ? false : value_dart);
      this.value_js = (value_js == null ? false : value_js);
      this.value_java = (value_java == null ? false : value_java);
    });


  }
  //保存界面的输入选择信息
  void saveInfo(String value_nickname)  async{
    final SharedPreferences prefs=await _prefs;
    prefs.setString("key_nickname", value_nickname);
    prefs.setBool("key_dart", value_dart);
    prefs.setBool("key_js", value_js);
    prefs.setBool("key_java", value_java);


  }


}

在Android下的数据data/data/包名/shared_prefs
Flutter学习第四课:SharedPreferences本地存储的简单使用_第2张图片
Flutter学习第四课:SharedPreferences本地存储的简单使用_第3张图片



    Rocky
    
    
    

你可能感兴趣的:(flutter)