main.dart 6.04 KB
import 'package:flutter/material.dart';
import 'package:flutter_mes2/utils/Constant.dart';
import 'package:flutter_mes2/utils/ToastUtil.dart';
import 'package:flutter_mes2/widget/chooseWork.dart';
import 'package:flutter_mes2/widget/chooseWork.dart';
import 'package:flutter_mes2/widget/homePage.dart';
import 'package:oktoast/oktoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'bean/LoginrResult.dart';
import 'https/dio_utils.dart';
import 'https/http_utils.dart';
import 'setNetwork.dart';
import 'dart:convert';


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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return OKToast( // 这一步
      child: new MaterialApp(
        title: 'MES',
        debugShowCheckedModeBanner: false,
        theme: new ThemeData(
          primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: new MyHomePage(title: '登录页面'),
      ),
    );
  }
}


class MyHomePage extends StatefulWidget {

  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

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

class TextFieldAndCheckPageState extends State<MyHomePage> {

  FocusNode nameFocusNode = FocusNode();

  FocusNode passFocusNode = FocusNode();

  //手机号的控制器
  TextEditingController nameController = TextEditingController();

  //密码的控制器
  TextEditingController passController = TextEditingController();

  @override
  void setState(fn) {
    super.setState(fn);
  }

 @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future<String> mNetwork = getNetwork();
    mNetwork.then((value) => DioUtils.network = value);
  }

  Future<String> getNetwork() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String network = prefs.getString(Constant.NETWORK);
    if(network == null) {
      network = Constant.DEFAULT_NETWORK;
    }
    return network;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:
      Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new GestureDetector(
              onTap:  () {
                      ///路由跳转
                  Navigator.push(context, MaterialPageRoute(builder: (_) {
                    return new setNetwork();
                  }));
                },
              child:  Container(
                width: 100,
                height: 100,

                margin: EdgeInsets.only(bottom: 30),
                // child: Image.asset("images/a.jpeg",
                // fit:BoxFit.cover,
                // ),
                child: Image.asset("images/sanyi2.png",
                  fit:BoxFit.cover,
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(left:30, right: 30, bottom: 10),
              child:
              TextField(
                controller: nameController,
                focusNode:  nameFocusNode,
                decoration: InputDecoration(
                  border: OutlineInputBorder(),
                  contentPadding: EdgeInsets.all(10.0),
                  labelText: '请输入账号',
                  prefixIcon: Icon(Icons.person),
                ),
                  style:TextStyle(fontSize: 16)
              ),
            ),
            Container(
              margin: EdgeInsets.only(left:30, right: 30, bottom: 10),
              child:
              TextField(
                  controller: passController,
                  focusNode:  passFocusNode,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      contentPadding: EdgeInsets.all(10.0),
                      labelText: '请输入密码',
                      prefixIcon: Icon(Icons.lock)),
                  style:TextStyle(fontSize: 16),
                  obscureText: true),
            ),
            Container(
              height: 40,
              margin: EdgeInsets.only(left:30, right: 30, bottom: 10, top:10),
              width: double.infinity,
              child: RaisedButton(
                onPressed: _login,
                shape: StadiumBorder(),
                child: Text('登  录',
                  style: TextStyle(
                      fontSize: 20,
                      color: Colors.white
                  ),),
                color: Colors.deepOrangeAccent,
                textColor: Colors.white,
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _login() {
    print({'phone': nameController.text, 'password': passController.text});
    String name = nameController.text.toString();
    String password = passController.text.toString();
    if(name.isEmpty) {
      ToastUtil.showText( "请输入账号");
      return;
    }
    if(password.isEmpty) {
      ToastUtil.showText( "请输入密码");
      return;
    }
    HttpUtils.login(context, {"username": name, "password": password}, success: (result) {
      var moreData = result['data'];
      LoginResult loginResult = LoginResult.fromJson(moreData.first);
       nameFocusNode.unfocus();
       passFocusNode.unfocus();
       DioUtils.TOKEN = loginResult.token;
        Navigator.push(context, MaterialPageRoute(builder: (_) {
          return new chooseWork();
        }));
      }, fail: (code) {

    });
  }

  void onTextClear() {
    setState(() {
      nameController.clear();
      passController.clear();
    });
  }
}