如何在flutter中应用页面内容内的条件

发布于 2025-01-09 23:58:12 字数 2409 浏览 0 评论 0原文

我试图在用户登录时在小部件上应用条件,显示小部件注销,否则显示登录按钮。这怎么可能?

登录前

在此输入图片描述

登录后

<图片src="https://i.sstatic.net/BaTVD.png" alt="在此处输入图像描述">

我使用 shred_preferences 包保存、获取和删除使用过的数据。 这是我的代码:

shared_preferences

import 'package:shared_preferences/shared_preferences.dart';


class PrefServices{
  Future createCache(String username, String password) async {
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.setString("username", username);
    _preferences.setString("password", password);
  }

  Future readCache(String username) async {
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    var username = _preferences.getString("username")?? "null";
    // _preferences.getString("password");
    return username;
  }

  Future<void> removeCache(String username, String password) async {
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.remove("username");
    _preferences.remove("password");
  }


}

配置文件屏幕

import 'dart:async';
import 'package:clothing_roll/shred_preferences/shred_preferences_services.dart';
import 'package:clothing_roll/ui/widget/profile/login_widget.dart';
import 'package:clothing_roll/ui/widget/profile/profiles_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';


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

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

class _ProfileScreen extends State<ProfileScreen> {

  final PrefServices _prefServices = PrefServices();

  @override
  void initState() {
    _prefServices.readCache("username").then((value) {
      print(value.toString());
      if (value != null) {
        return Timer(Duration(seconds: 1),
            () => ProfileWidget());
      } else {
        return Timer(Duration(seconds: 1),
            () => LoginWidget());
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

I am trying to apply conditions on widget when user is logged in, display widget logout else display login button.How is it possible?

Before login

enter image description here

After Login

enter image description here

I used shred_preferences package saving, getting and removing used data.
Here my codes:

shared_preferences

import 'package:shared_preferences/shared_preferences.dart';


class PrefServices{
  Future createCache(String username, String password) async {
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.setString("username", username);
    _preferences.setString("password", password);
  }

  Future readCache(String username) async {
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    var username = _preferences.getString("username")?? "null";
    // _preferences.getString("password");
    return username;
  }

  Future<void> removeCache(String username, String password) async {
    SharedPreferences _preferences = await SharedPreferences.getInstance();
    _preferences.remove("username");
    _preferences.remove("password");
  }


}

Profile Screen

import 'dart:async';
import 'package:clothing_roll/shred_preferences/shred_preferences_services.dart';
import 'package:clothing_roll/ui/widget/profile/login_widget.dart';
import 'package:clothing_roll/ui/widget/profile/profiles_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';


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

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

class _ProfileScreen extends State<ProfileScreen> {

  final PrefServices _prefServices = PrefServices();

  @override
  void initState() {
    _prefServices.readCache("username").then((value) {
      print(value.toString());
      if (value != null) {
        return Timer(Duration(seconds: 1),
            () => ProfileWidget());
      } else {
        return Timer(Duration(seconds: 1),
            () => LoginWidget());
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2025-01-16 23:58:12

注意:我使用了 Getx 包

  1. 创建一个 bool 变量用于条件检查,
  2. 将三元运算符添加到 bool 变量并更改条件
class Test1 extends StatelessWidget {
  Controller controller = Get.put(Controller());

  Test1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        width: Get.width,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () {
                  controller.isLogged.value = !controller.isLogged.value;
                },
                child: Text("Press to change value")),
            Obx(
              () => controller.isLogged.value
                  ? ElevatedButton(onPressed: () {}, child: Text("logged In"))
                  : ElevatedButton(onPressed: () {}, child: Text("Logged Out")),
            ),
          ],
        ),
      ),
    );
  }
}

Note: I have used Getx package

  1. create a bool variable for conmdition check
  2. add ternary operator to bool variable and change the conditions
class Test1 extends StatelessWidget {
  Controller controller = Get.put(Controller());

  Test1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        width: Get.width,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () {
                  controller.isLogged.value = !controller.isLogged.value;
                },
                child: Text("Press to change value")),
            Obx(
              () => controller.isLogged.value
                  ? ElevatedButton(onPressed: () {}, child: Text("logged In"))
                  : ElevatedButton(onPressed: () {}, child: Text("Logged Out")),
            ),
          ],
        ),
      ),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文