setNetwork.dart 3.38 KB
import 'package:centercontrol/utils/ToastUtil.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'https/dio_utils.dart';
import 'utils/Constant.dart';

class setNetwork extends StatefulWidget {

  @override
  State<StatefulWidget> createState() => StateFulWidgetDemo1State();
}

class StateFulWidgetDemo1State extends State<setNetwork> {
  //手机号的控制器
  TextEditingController networkController = TextEditingController();

  var network;

  save() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString(Constant.NETWORK, network);
  }

  get() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    network = prefs.getString(Constant.NETWORK);
    setState(() {

    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    get();
  }

    @override
    Widget build(BuildContext context) {

      return Scaffold(
        appBar: AppBar(
          title: Text('设置网络'),
          centerTitle: true, // 标题居中
        ),
        body:
      Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(left:30, right: 30, bottom: 10),
              child:
              Text("网络地址:$network",style:TextStyle(fontSize: 16)),
            ),
            Container(
              margin: EdgeInsets.only(left:30, right: 30, bottom: 10),
              child:
              TextField(
                  controller: networkController,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      contentPadding: EdgeInsets.all(10.0),
                  ),),
            ),
            Container(
              height: 40,
              margin: EdgeInsets.only(left:30, right: 30, bottom: 10, top: 10),
              width: double.infinity,
              child: RaisedButton(
                onPressed: () {
                  network = Constant.DEFAULT_NETWORK;
                  networkController.text = Constant.DEFAULT_NETWORK;
                },
                child: Text('默认地址',
                  style: TextStyle(
                      fontSize: 20,
                      color: Colors.white
                  ),),
                color: Colors.deepOrangeAccent,
                textColor: Colors.white,
                shape: StadiumBorder(),
              ),
            ),

            Container(
              height: 40,
              margin: EdgeInsets.only(left:30, right: 30, bottom: 10, top: 10),
              width: double.infinity,
              child: RaisedButton(
                onPressed:() {
                  setState(() {
                    ToastUtil.showText( "设置成功");
                    network = networkController.value.text.toString();
                    save();
                    DioUtils.network = network;
                  });
                },
                child: Text('设置',
                  style: TextStyle(
                      fontSize: 20,
                      color: Colors.white
                  ),),
                color: Colors.deepOrangeAccent,
                textColor: Colors.white,
                shape: StadiumBorder(),
              ),
            ),

          ],
        ),
      ),
    );
  }



}