【问题标题】:Use of rate_my_app package in flutter?在flutter中使用rate_my_app包?
【发布时间】:2019-11-01 03:43:02
【问题描述】:

谁能告诉我,你是如何在 Flutter 应用中使用 rate_my_app https://pub.dev/packages/rate_my_app 包来增加用户评论的?

一个示例代码会很有用吗?

【问题讨论】:

    标签: flutter flutter-plugin


    【解决方案1】:

    rate_my_app 只是一个提醒用户评价您的应用的插件。如果您的应用满足用户的需求,它将增加用户的正面评价。

    Skyost的示例代码:

    import 'package:flutter/material.dart';
    import 'package:rate_my_app/rate_my_app.dart';
    
    /// Main rate my app instance.
    RateMyApp _rateMyApp = RateMyApp();
    
    /// First plugin test method.
    void main() {
      _rateMyApp.init().then((_) {
        runApp(_RateMyAppTestApp());
        print('Minimum days : ' + _rateMyApp.minDays.toString());
        print('Minimum launches : ' + _rateMyApp.minLaunches.toString());
    
        print('Base launch : ' + _dateToString(_rateMyApp.baseLaunchDate));
        print('Launches : ' + _rateMyApp.launches.toString());
        print('Do not open again ? ' + (_rateMyApp.doNotOpenAgain ? 'Yes' : 'No'));
    
        print('Are conditions met ? ' + (_rateMyApp.shouldOpenDialog ? 'Yes' : 'No'));
      });
    }
    
    /// Returns a formatted date string.
    String _dateToString(DateTime date) => date.day.toString().padLeft(2, '0') + '/' + date.month.toString().padLeft(2, '0') + '/' + date.year.toString();
    
    /// The main rate my app test widget.
    class _RateMyAppTestApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) => MaterialApp(
            home: Scaffold(
              appBar: AppBar(
                title: Text('Rate my app !'),
              ),
              body: _RateMyAppTestAppBody(),
            ),
          );
    }
    
    /// The body of the main rate my app test widget.
    class _RateMyAppTestAppBody extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => _RateMyAppTestAppBodyState();
    }
    
    /// The body state of the main rate my app test widget.
    class _RateMyAppTestAppBodyState extends State<_RateMyAppTestAppBody> {
      @override
      Widget build(BuildContext context) => Padding(
            padding: EdgeInsets.symmetric(
              horizontal: 40,
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                _textCenter('Minimum days : ' + _rateMyApp.minDays.toString()),
                _textCenter('Minimum launches : ' + _rateMyApp.minLaunches.toString()),
                _textCenter('Base launch : ' + _dateToString(_rateMyApp.baseLaunchDate)),
                _textCenter('Launches : ' + _rateMyApp.launches.toString()),
                _textCenter('Do not open again ? ' + (_rateMyApp.doNotOpenAgain ? 'Yes' : 'No')),
                _textCenter('Are conditions met ? ' + (_rateMyApp.shouldOpenDialog ? 'Yes' : 'No')),
                Padding(
                  padding: EdgeInsets.only(top: 10),
                  child: RaisedButton(
                    child: Text('Launch "Rate my app" dialog'),
                    onPressed: () => _rateMyApp.showRateDialog(context).then((v) => setState(() {})),
                  ),
                ),
                RaisedButton(
                  child: Text('Launch "Rate my app" star dialog'),
                  onPressed: () => _rateMyApp.showStarRateDialog(context, onRatingChanged: (count) {
                    FlatButton cancelButton = FlatButton(
                      child: Text('CANCEL'),
                      onPressed: () {
                        _rateMyApp.doNotOpenAgain = true;
                        _rateMyApp.save().then((v) => Navigator.pop(context));
                        setState(() {});
                      },
                    );
    
                    if (count == null || count == 0) {
                      return [cancelButton];
                    }
    
                    String message = 'You put ' + count.round().toString() + ' star(s). ';
                    Color color;
                    switch (count.round()) {
                      case 1:
                        message += 'Did this app hurt you physically ?';
                        color = Colors.red;
                        break;
                      case 2:
                        message += 'That\'s not really cool man.';
                        color = Colors.orange;
                        break;
                      case 3:
                        message += 'Well, it\'s average.';
                        color = Colors.yellow;
                        break;
                      case 4:
                        message += 'This is cool, like this app.';
                        color = Colors.lime;
                        break;
                      case 5:
                        message += 'Great ! <3';
                        color = Colors.green;
                        break;
                    }
    
                    return [
                      FlatButton(
                        child: Text('OK'),
                        onPressed: () {
                          print(message);
                          Scaffold.of(context).showSnackBar(
                            SnackBar(
                              content: Text(message),
                              backgroundColor: color,
                            ),
                          );
                          _rateMyApp.doNotOpenAgain = true;
                          _rateMyApp.save().then((v) => Navigator.pop(context));
                          setState(() {});
                        },
                      ),
                      cancelButton,
                    ];
                  }),
                ),
                RaisedButton(
                  child: Text('Reset'),
                  onPressed: () => _rateMyApp.reset().then((v) => setState(() {})),
                ),
              ],
            ),
          );
    
      /// Returns a centered text.
      Text _textCenter(String content) => Text(
            content,
            textAlign: TextAlign.center,
          );
    }
    

    GitHub code

    【讨论】:

      【解决方案2】:

      请按照以下代码。

          import 'package:flutter/material.dart';
          import 'package:rate_my_app/rate_my_app.dart';
      
          void main() => runApp(MyApp());
      
          class MyApp extends StatelessWidget {
            // This widget is the root of your application.
            @override
            Widget build(BuildContext context) {
              return MaterialApp(
                title: 'Flutter Demo',
                theme: ThemeData(
                  primarySwatch: Colors.blue,
                ),
                home: MyHomePage(title: 'Flutter Rating Prompt'),
              );
            }
          }
      
          class MyHomePage extends StatefulWidget {
            MyHomePage({Key key, this.title}) : super(key: key);
      
            final String title;
      
            @override
            _MyHomePageState createState() => _MyHomePageState();
          }
      
          class _MyHomePageState extends State<MyHomePage> {
            RateMyApp _rateMyApp = RateMyApp(
              preferencesPrefix: 'rateMyApp_',
              minDays: 3,
              minLaunches: 7,
              remindDays: 2,
              remindLaunches: 5,
              // appStoreIdentifier: '',
              // googlePlayIdentifier: '',
            );
      
            @override
            void initState() {
              super.initState();
              _rateMyApp.init().then((_) {
                // TODO: Comment out this if statement to test rating dialog (Remember to uncomment)
                // if (_rateMyApp.shouldOpenDialog) {
                  _rateMyApp.showStarRateDialog(
                    context,
                    title: 'Enjoying Flutter Rating Prompt?',
                    message: 'Please leave a rating!',
                    onRatingChanged: (stars) {
                      return [
                        FlatButton(
                          child: Text('Ok'),
                          onPressed: () {
                            if (stars != null) {
                              _rateMyApp.doNotOpenAgain = true;
                              _rateMyApp.save().then((v) => Navigator.pop(context));
      
                              if (stars <= 3) {
                                print('Navigate to Contact Us Screen');
                                // Navigator.push(
                                //   context,
                                //   MaterialPageRoute(
                                //     builder: (_) => ContactUsScreen(),
                                //   ),
                                // );
                              } else if (stars <= 5) {
                                print('Leave a Review Dialog');
                                // showDialog(...);
                              }
                            } else {
                              Navigator.pop(context);
                            }
                          },
                        ),
                      ];
                    },
                    dialogStyle: DialogStyle(
                      titleAlign: TextAlign.center,
                      messageAlign: TextAlign.center,
                      messagePadding: EdgeInsets.only(bottom: 20.0),
                    ),
                    starRatingOptions: StarRatingOptions(),
                  );
                // }
              });
            }
      
            @override
            Widget build(BuildContext context) {
              return Scaffold(
                appBar: AppBar(
                  title: Text(widget.title),
                ),
              );
            }
          }
      

      【讨论】:

        【解决方案3】:

        这里是从 main 调用的屏幕中使用 rate_my_app 的示例

        import 'package:flutter/material.dart';
        import 'package:rate_my_app/rate_my_app.dart';
        
        /// Main Rate my app instance.
        RateMyApp _rateMyApp = RateMyApp(
          preferencesPrefix: 'rateMyApp_',
          minDays: 7,
          minLaunches: 10,
          remindDays: 7,
          remindLaunches: 10,
          // appStoreIdentifier: '',
          // googlePlayIdentifier: '',
        );
        
        class HomeScreen extends StatefulWidget {
          @override
          _HomeScreenState createState() => _HomeScreenState();
        }
        
        class _HomeScreenState extends State<HomeScreen> {
          @override
          void initState() {
            super.initState();
            _rateMyApp.init().then((_) {
              _rateMyApp.conditions.forEach((condition) {
                if (condition is DebuggableCondition) {
                  print(condition.valuesAsString());
                  // We iterate through our list of conditions and we print all debuggable ones.
                }
              });
        
              print('Are all conditions met ? ' +
                  (_rateMyApp.shouldOpenDialog ? 'Yes' : 'No'));
        
              if (_rateMyApp.shouldOpenDialog) _buildShowStarRateDialog(context);
            });
          }
        
            _buildShowStarRateDialog(BuildContext context) {
            _rateMyApp.showStarRateDialog(context, actionsBuilder: (_, count) {
              final Widget cancelButton = RateMyAppNoButton(
                // We create a custom "Cancel" button using the RateMyAppNoButton class.
                _rateMyApp,
                text: 'CANCEL',
                callback: () => setState(() {}),
              );
              if (count == null || count == 0) {
                // If there is no rating (or a 0 star rating), we only have to return our cancel button.
                return [cancelButton];
              }
        
              // Otherwise we can do some little more things...
              String message = 'You\'ve put ' + count.round().toString() + ' star(s). ';
              Color color;
              switch (count.round()) {
                case 1:
                  message += 'Did this app hurt you physically ?';
                  color = Colors.red;
                  break;
                case 2:
                  message += 'That\'s not really cool man.';
                  color = Colors.orange;
                  break;
                case 3:
                  message += 'Well, it\'s average.';
                  color = Colors.yellow;
                  break;
                case 4:
                  message += 'This is cool, like this app.';
                  color = Colors.lime;
                  break;
                case 5:
                  message += 'Great ! <3';
                  color = Colors.green;
                  break;
              }
        
              return [
                FlatButton(
                  child: const Text('OK'),
                  onPressed: () async {
                    print(message);
                    Toast.show(message, context,
                        duration: Toast.LENGTH_LONG, gravity: Toast.TOP);
        
                    // This allow to mimic a click on the default "Rate" button and thus update the conditions based on it ("Do not open again" condition for example) :
                    await _rateMyApp.callEvent(RateMyAppEventType.rateButtonPressed);
                    Navigator.pop<RateMyAppDialogButton>(
                        context, RateMyAppDialogButton.rate);
        
                    setState(() {});
                  },
                ),
                cancelButton,
              ];
            });
        
        }
        

        【讨论】:

        • 虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,纯代码的答案没有用处。
        猜你喜欢
        • 2019-10-28
        • 2019-06-30
        • 2021-03-31
        • 1970-01-01
        • 2022-12-15
        • 2020-01-21
        • 2020-09-10
        • 2020-10-01
        • 1970-01-01
        相关资源
        最近更新 更多