小编Zef*_*ndo的帖子

Flutter:使用 SharedPreferences 保持用户登录

我对共享首选项有一些问题。如何通过关闭应用程序让用户登录?

我已经用这个代码存储了用户名:

setState(() {
        preferences.setString('token', dataUser[0]['username']);
        Navigator.of(context).pushAndRemoveUntil(
            PageTransition(
                type: PageTransitionType.rightToLeftWithFade,
                child: HomePage()),
            (Route<dynamic> route) => false);
      });
Run Code Online (Sandbox Code Playgroud)

当我点击按钮登录时,它可以转到主页。为了确保用户名存储/不存储,我使用函数来获取我的令牌并获取该令牌。

我的令牌使用此代码获取我期望的内容:

String checkPref;
  getPrefIdUser() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    setState(() {
      checkPref = sharedPreferences.getString('token');
      if (checkPref == null) {
        Navigator.of(context).pushAndRemoveUntil(
            PageTransition(
                type: PageTransitionType.leftToRightWithFade,
                child: LoginPage()),
            (Route<dynamic> route) => false);
      } else {
        // Navigator.of(context).pushAndRemoveUntil(
        //     PageTransition(
        //         type: PageTransitionType.leftToRightWithFade,
        //         child: HomePage()),
        //     (Route<dynamic> route) => false);
      }
    });
  }
Run Code Online (Sandbox Code Playgroud)

我已经getPrefIdUser()在我的主页中放入了这样的 initState:

 @override
  void initState() { …
Run Code Online (Sandbox Code Playgroud)

sharedpreferences dart flutter

2
推荐指数
1
解决办法
8665
查看次数

Flutter:禁用上一个步进器单击

我正在使用Stepper Widget 来制作更改 PinCode 表单。一切正常,但我不知道如何禁用之前的步进点击。

例子 :

  • 步骤 1 输入旧密码。
  • 步骤 2 输入新密码。

如果我在第 2 步中单击步进器,我不想返回到第 1 步。

我该如何解决这个问题?

步进器

这是我的源代码:

Form(
        key: _formKey,
        child: Stepper(
          steps: steps(),
          currentStep: currStep,
          onStepContinue: () {
            setState(() {
              if (formKeys[currStep].currentState.validate()) {
                if (currStep < steps().length - 1) {
                  currStep += 1;
                } else if (steps().length == 2) {
                  print('Done');
                } else {
                  currStep = 0;
                }
              }
            });
          },
          onStepTapped: (step) {
            setState(() {
              currStep = step;
              print(step);
            });
          },
        ),
      ),
 List<Step> …
Run Code Online (Sandbox Code Playgroud)

dart flutter

2
推荐指数
1
解决办法
1069
查看次数

Flutter:将整数转换为月份

我有模型和虚拟数据来测试这样的:

模型


class TestingLoopingModel {
  String id;
  int month;
  int year;
  int value;

  TestingLoopingModel({
    this.id,
    this.month,
    this.year,
    this.value,
  });
}

Run Code Online (Sandbox Code Playgroud)

虚拟数据


List<TestingLoopingModel> listTest = [
  //TODO Month 12 And 10 is not exist
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 2,
    year: 2020,
    value: 2000,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 4,
    year: 2020,
    value: 1500,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 6,
    year: 2020,
    value: 3000,
  ),
  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 8,
    year: 2020,
    value: 3500,
  ),

  TestingLoopingModel(
    id: DateTime.now().toString(),
    month: 1,
    year: 2020, …
Run Code Online (Sandbox Code Playgroud)

dart flutter

2
推荐指数
2
解决办法
2874
查看次数

Flutter:制作选定的卡片

我有简单的水平 ListviewBuilder,里面有可以选择的卡片。但问题是,当我选择其中一张卡时,所有卡都被选择。我想要的是我只能选择一张卡,如果我选择另一张卡,删除旧的选择并创建新的选择到新卡。这个怎么做 ?

像这样的东西:

在此输入图像描述

我的失败结果

在此输入图像描述

这是我的源代码:

import 'package:flutter/material.dart';

class IconMenu {
  final IconData iconName;
  final String titleIcon;
  IconMenu({this.iconName, this.titleIcon});
}

List<IconMenu> iconList = [
  IconMenu(iconName: Icons.ac_unit, titleIcon: "AC Unit"),
  IconMenu(iconName: Icons.access_alarm, titleIcon: "Alarm"),
  IconMenu(iconName: Icons.accessibility_new, titleIcon: "accessiblity"),
  IconMenu(iconName: Icons.add, titleIcon: "Add"),
];

class TestingScreen extends StatefulWidget {
  static const routeName = "/testing-page";

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

class _TestingScreenState extends State<TestingScreen> {
  bool selectedList = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        mainAxisAlignment: MainAxisAlignment.center, …
Run Code Online (Sandbox Code Playgroud)

dart flutter

1
推荐指数
1
解决办法
8280
查看次数

Flutter:Futurebuilder 无法捕获异常

我有通用类来请求名为ReusableRequestServer 的服务器,如果在向服务器发出请求时发生错误,这个类将处理一些异常。

可重用的请求服务器

class ReusableRequestServer<T> {
  Future<T> requestServer(FutureOr<T> requestServer()) async {
    try {
      return await requestServer();
    } on FormatException catch (_) {
      throw ConstText.FORMAT_EXCEPTION;
    } on TimeoutException catch (_) {
      throw ConstText.TIMEOUT_EXCEPTION;
    } on SocketException catch (_) {
      throw ConstText.NO_CONNECTION;
    } catch (e) {
      throw e.toString();
    }
  }
}

final reusableRequestServer = ReusableRequestServer();

Run Code Online (Sandbox Code Playgroud)

我有一个简单的get请求从服务器获取版本移动,我也实现了ReusableRequestServer

获取请求

class MobileVersionApi {
  Future<List<MobileVersionModel>> getNewestMobileVersion() async {
    var result = await reusableRequestServer.requestServer(() async {
      final response = await http …
Run Code Online (Sandbox Code Playgroud)

dart flutter

1
推荐指数
1
解决办法
3562
查看次数

Flutter:Listview Builder 水平内部堆栈小部件

我有像上面这样的设计,在该设计中我实现了应用程序。我在容器内有 Stack Widget,然后在 Stack 小部件内有 ListviewBuilder ,滚动方向为水平。但问题是,我无法在 Stack 小部件内水平滚动 ListViewBuilder。我该如何解决这个问题?

我的试用失败

在此输入图像描述

源代码

class HomeScreen extends StatelessWidget {
  static const routeName = "/home-screen";
  @override
  Widget build(BuildContext context) {
    final mqHeight = MediaQuery.of(context).size.height;
    final mqWidth = MediaQuery.of(context).size.width;

    return SafeArea(
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          title: Text('Good Morning'),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.settings),
              onPressed: () => "",
            )
          ],
        ),
        body: SingleChildScrollView(
            child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              color: Colors.blue,
              height: mqHeight / 3,
              child: Stack(
                overflow: Overflow.visible,
                children: <Widget>[
                  Container( …
Run Code Online (Sandbox Code Playgroud)

dart flutter

0
推荐指数
1
解决办法
4991
查看次数

标签 统计

dart ×6

flutter ×6

sharedpreferences ×1