利用Flutter寫一個跨平臺的果核APP(4)——數(shù)據(jù)存儲

前言

目前我們已經(jīng)實現(xiàn)了幾個界面,今天這篇文章開始著手進行登錄頁的制作,主要流程就是獲取輸入框中的內(nèi)容,發(fā)送給后臺進行驗證,如果成功將返回信息保存在本地并跳轉(zhuǎn)至首頁,如果失敗就提示用戶重新輸入。
在這里面需要掌握3塊知識,第一是flutter中的表單組件的使用,然后則是flutter中的數(shù)據(jù)存儲,最后是網(wǎng)絡(luò)請求。

效果

代碼

上述三個部分我已經(jīng)在其他文章中分別介紹了,詳情點擊相關(guān)文章的鏈接

  1. 《flutter表單組件》
  2. 《Flutter數(shù)據(jù)存儲之shared_preferences》
  3. 《利用Flutter寫一個跨平臺的果核APP(3)——網(wǎng)絡(luò)請求》

剩下的我就直接放出首頁代碼了:

import 'package:flutter/material.dart';
import 'package:flutter_guohe/constant//UrlConstant.dart';
import 'package:dio/dio.dart';
import 'package:flutter_guohe/views/customview.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_guohe/constant/SpConstant.dart';
import 'package:flutter_guohe/pages/app.dart';

class LoginPage extends StatefulWidget {
  static String tag = 'login-page';

  @override
  _LoginPageState createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  GlobalKey<FormState> _formKey = new GlobalKey<FormState>();

  String _account;

  String _password;

  //表單驗證方法
  void _forSubmitted(BuildContext context) {
    var _form = _formKey.currentState;

    if (_form.validate()) {
      _form.save();
      login(context, _account.trim(), _password.trim());
    }
  }

  //登錄
  void login(BuildContext context, String account, String password) {
    showDialog(
        context: context,
        builder: (context) {
          return new LoadingDialog(content: "登錄中,請稍后......");
        });
    FormData formData =
        new FormData.from({"username": account, "password": password});
    Dio().post(Constant.STU_INFO, data: formData).then((res) {
      print(account + " " + password);
      if (res.statusCode == 200) {
        Navigator.pop(context);
        if (res.data['code'] == 200) {
          print(res.data);
          String name = res.data['info']['name'];
          String academy = res.data['info']['academy'];
          String major = res.data['info']['major'];
          String stu_id = res.data['info']['name'];
          String stu_pass = res.data['info']['password'];
          List<String> list = new List();
          list.add(name);
          list.add(academy);
          list.add(major);
          list.add(stu_id);
          list.add(stu_pass);
          store(list);
          Navigator.push(
            context,
            new MaterialPageRoute(builder: (context) => new Guohe()),
          );
        }
      }
    });
  }

  //將學(xué)生的相關(guān)信息保存至本地
  void store(List<String> list) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setBool(SpConstant.IS_LOGIN, true);
    sharedPreferences.setStringList(SpConstant.STU_INFO, list);
  }

  @override
  Widget build(BuildContext context) {
    final account = new TextFormField(
      autofocus: true,
      initialValue: '',
      decoration: new InputDecoration(
        labelText: '學(xué)號',
      ),
      onSaved: (val) {
        _account = val;
      },
    );

    final password = new TextFormField(
      initialValue: '',
      obscureText: true,
      decoration: new InputDecoration(
        labelText: '密碼',
      ),
      onSaved: (val) {
        _password = val;
      },
    );

    final loginButton = new FloatingActionButton(
      backgroundColor: Colors.white,
      foregroundColor: Colors.black26,
      child: const Icon(Icons.arrow_forward),
      elevation: 18.0,
      onPressed: () => _forSubmitted(context),
    );

    final loginBox = new Container(
      width: 320.0,
      height: 250.0,
      margin: new EdgeInsets.only(top: 300.0, right: 30.0),
      child: new Stack(
        children: <Widget>[
          new Container(
              color: Colors.white,
              width: 280.0,
              height: 220.0,
              child: new Form(
                key: _formKey,
                child: new ListView(
                  shrinkWrap: true,
                  padding: new EdgeInsets.only(left: 24.0, right: 24.0),
                  children: <Widget>[
                    SizedBox(height: 48.0),
                    account,
                    new SizedBox(
                      height: 15.0,
                    ),
                    password,
                  ],
                ),
              )),
          new Positioned(
              //方法二
              right: 15.0,
              top: 180.0,
              child: loginButton),
        ],
      ),
    );

    return new Scaffold(
      backgroundColor: Colors.white,
      body: new Container(
        decoration: new BoxDecoration(
            image: new DecorationImage(
                image: new AssetImage('assets/imgs/bg_login.webp'),
                fit: BoxFit.cover)),
        child: new Center(child: loginBox),
      ),
    );
  }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,082評論 25 709
  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 14,014評論 2 59
  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,210評論 3 119
  • 半生寂苦空蹉跎,人生幾何?對酒當歌,閑看秋枝勾殘月。 那時少年荒唐事,情思難測。好天良夜,不執(zhí)金戈采青荷。
    狄克先生閱讀 887評論 1 8
  • 1.你可以使用我們定制的 cnpm (gzip 壓縮支持) 命令行工具代替默認的 npm: 2.或者你直接通過添加...
    darebeat閱讀 1,929評論 0 0

友情鏈接更多精彩內(nèi)容