简体   繁体   English

Dart/Flutter 如何实现一个有两个版本(PRO 和 LITE)的颤振项目

[英]Dart/Flutter How can I implement a flutter project who has two versions (PRO and LITE)

In my flutter project I have some features that I only want to have on a PRO version of my app.在我的颤振项目中,我有一些我只想在我的应用程序的PRO版本上拥有的功能。 How can I build two different final projects, one with the feature and another without?如何构建两个不同的最终项目,一个具有该功能,另一个没有?

in your user model, add a field like "status", and then set it to lite by default, then in your UI, after user logs in, grab the currentUserData, and instantiate the User, then check if User.status is pro or lite then build as such在您的用户模型中,添加“状态”之类的字段,然后将其默认设置为 lite,然后在您的 UI 中,用户登录后,获取 currentUserData,并实例化用户,然后检查 User.status 是否为 pro 或lite 然后像这样构建

@override
Widget build(BuildContext context) {
  return user.status == lite ? LiteHomeScreen() : ProHomeScreen();
}

Long Example长示例

User用户

enum Status {
  lite,
  pro,
}

class User {
  String? name;
  int? age;
  Status status;
  
  User({this.name, this.age, this.status = Status.lite});

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'age': age,
      'status': status,
    };
  }

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      name: json['name'],
      age: json['age'],
      status: json['status'],
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is User &&
          runtimeType == other.runtimeType &&
          name == other.name &&
          age == other.age &&
          status == other.status;

  @override
  int get hashCode => name.hashCode ^ age.hashCode ^ status.hashCode;
}

UI用户界面

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

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  late User user;
  void initializeUser() async {
    Response response = await get("https://fakedatabase.myfake.com/users/1");
    user = User.fromJson(jsonDecode(response.body));
  }
  @override
  void initState() {
    initializeUser();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return user.status == Status.lite ? LiteHome() : ProHome();
  }
}

then write a method that will execute when user pays for pro or something然后编写一个方法,当用户支付 pro 或其他东西时将执行

void upgradeToPro() {
  user.status = Status.pro;
  
  /// Save the user to the database.
  post(...uri, body: jsonEncode(user.toJson()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM