Skip to content

Commit

Permalink
Updated examples and docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
larryaasen committed Dec 29, 2023
1 parent e654355 commit 0635af7
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 68 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ The alert can be customized by changing the `DialogTheme` on the `MaterialApp`,
- [example/lib/main-alert-theme.dart](example/lib/main-alert-theme.dart)
- [example/lib/main-custom-alert.dart](example/lib/main-custom-alert.dart)

The card can be customized by changing the `CardTheme` on the `MaterialApp`, or by overriding methods in the `UpgradeCard` class. See these examples for more details:
- [example/lib/main-card-theme.dart](example/lib/main-card-theme.dart)
- [example/lib/main-custom-card.dart](example/lib/main-custom-card.dart)

Here are the custom parameters for `UpgradeAlert`:

* canDismissDialog: can alert dialog be dismissed on tap outside of the alert dialog, which defaults to ```false``` (not used by UpgradeCard)
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main-alert-theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MyApp extends StatelessWidget {
title: 'Upgrader Example',
home: UpgradeAlert(
child: Scaffold(
appBar: AppBar(title: Text('Upgrader Example')),
appBar: AppBar(title: Text('Upgrader Alert Theme Example')),
body: Center(child: Text('Checking...')),
)),
theme: light,
Expand Down
69 changes: 69 additions & 0 deletions example/lib/main-card-theme.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2023 Larry Aasen. All rights reserved.

import 'package:flutter/material.dart';
import 'package:upgrader/upgrader.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

// Only call clearSavedSettings() during testing to reset internal values.
await Upgrader.clearSavedSettings(); // REMOVE this for release builds

// On Android, the default behavior will be to use the Google Play Store
// version of the app.
// On iOS, the default behavior will be to use the App Store version of
// the app, so update the Bundle Identifier in example/ios/Runner with a
// valid identifier already in the App Store.
runApp(MyApp());
}

class MyApp extends StatelessWidget {
MyApp({super.key});

final dark = ThemeData.dark(useMaterial3: true);

final light = ThemeData(
cardTheme: CardTheme(color: Colors.greenAccent),
// Change the text buttons.
textButtonTheme: const TextButtonThemeData(
style: ButtonStyle(
// Change the color of the text buttons.
foregroundColor: MaterialStatePropertyAll(Colors.orange),
),
),
);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Upgrader Card Example',
home: Scaffold(
appBar: AppBar(title: Text('Upgrader Card Theme Example')),
body: Container(
margin: EdgeInsets.only(left: 12.0, right: 12.0),
child: SingleChildScrollView(
child: Column(
children: [
_simpleCard,
_simpleCard,
UpgradeCard(),
_simpleCard,
_simpleCard,
],
),
),
),
),
theme: light,
darkTheme: dark,
);
}

Widget get _simpleCard => Card(
child: SizedBox(
width: 200,
height: 50,
child: Center(child: Text('Card')),
),
);
}
2 changes: 1 addition & 1 deletion example/lib/main-custom-alert.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MyApp extends StatelessWidget {
home: MyUpgradeAlert(
upgrader: upgrader,
child: Scaffold(
appBar: AppBar(title: Text('Upgrader Example')),
appBar: AppBar(title: Text('Upgrader Custom Alert Example')),
body: Center(child: Text('Checking...')),
)),
);
Expand Down
34 changes: 32 additions & 2 deletions example/lib/main-custom-card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Upgrader Card Example',
title: 'Upgrader Example',
home: Scaffold(
appBar: AppBar(title: Text('Upgrader Card Example')),
appBar: AppBar(title: Text('Upgrader Custom Card Example')),
body: Container(
margin: EdgeInsets.only(left: 12.0, right: 12.0),
child: SingleChildScrollView(
Expand Down Expand Up @@ -55,4 +55,34 @@ class MyApp extends StatelessWidget {

class MyUpgradeCard extends UpgradeCard {
MyUpgradeCard({super.upgrader});

/// Override the [createState] method to provide a custom class
/// with overridden methods.
@override
UpgradeCardState createState() => MyUpgradeCardState();
}

class MyUpgradeCardState extends UpgradeCardState {
@override
Widget buildUpgradeCard(BuildContext context) {
final appMessages = widget.upgrader.determineMessages(context);
final title = appMessages.message(UpgraderMessage.title);
return Card(
color: Colors.greenAccent,
child: AlertStyleWidget(
actions: [
TextButton(
child: Text(
appMessages.message(UpgraderMessage.buttonTitleUpdate) ?? ''),
onPressed: () {
widget.upgrader.saveLastAlerted();
onUserUpdated();
},
),
],
content: Text(''),
title: Text(title ?? ''),
),
);
}
}
133 changes: 69 additions & 64 deletions lib/src/upgrade_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import 'upgrade_messages.dart';
import 'upgrader.dart';

/// A widget to display the upgrade card.
/// The only reason this is a [StatefulWidget] and not a [StatelessWidget] is that
/// the widget needs to rebulid after one of the buttons have been tapped.
/// Override the [createState] method to provide a custom class
/// with overridden methods.
class UpgradeCard extends StatefulWidget {
/// Creates a new [UpgradeCard].
UpgradeCard({
Expand Down Expand Up @@ -60,10 +64,11 @@ class UpgradeCard extends StatefulWidget {
final bool showReleaseNotes;

@override
UpgradeCardBaseState createState() => UpgradeCardBaseState();
UpgradeCardState createState() => UpgradeCardState();
}

class UpgradeCardBaseState extends State<UpgradeCard> {
/// The [UpgradeCard] widget state.
class UpgradeCardState extends State<UpgradeCard> {
@override
void initState() {
super.initState();
Expand Down Expand Up @@ -98,17 +103,13 @@ class UpgradeCardBaseState extends State<UpgradeCard> {
});
}

/// Build the UpgradeCard Widget.
/// Build the UpgradeCard widget.
Widget buildUpgradeCard(BuildContext context) {
final appMessages = widget.upgrader.determineMessages(context);
final title = appMessages.message(UpgraderMessage.title);
final message = widget.upgrader.body(appMessages);
final releaseNotes = widget.upgrader.releaseNotes;

final isBlocked = widget.upgrader.blocked();
final showIgnore = isBlocked ? false : widget.showIgnore;
final showLater = isBlocked ? false : widget.showLater;

if (widget.upgrader.debugLogging) {
print('upgrader: UpgradeCard: will display');
print('upgrader: UpgradeCard: showDialog title: $title');
Expand Down Expand Up @@ -139,62 +140,66 @@ class UpgradeCardBaseState extends State<UpgradeCard> {
}

return Card(
// color: Colors.white,
margin: widget.margin,
child: AlertStyleWidget(
title: Text(title ?? ''),
content: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(message),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text(
appMessages.message(UpgraderMessage.prompt) ?? '')),
if (notes != null) notes,
],
),
actions: <Widget>[
if (showIgnore)
TextButton(
child: Text(appMessages
.message(UpgraderMessage.buttonTitleIgnore) ??
''),
onPressed: () {
// Save the date/time as the last time alerted.
widget.upgrader.saveLastAlerted();

onUserIgnored();
forceUpdateState();
}),
if (showLater)
TextButton(
child: Text(
appMessages.message(UpgraderMessage.buttonTitleLater) ??
''),
onPressed: () {
// Save the date/time as the last time alerted.
widget.upgrader.saveLastAlerted();

onUserLater();
forceUpdateState();
}),
TextButton(
child: Text(
appMessages.message(UpgraderMessage.buttonTitleUpdate) ??
''),
onPressed: () {
// Save the date/time as the last time alerted.
widget.upgrader.saveLastAlerted();

onUserUpdated();
}),
]));
margin: widget.margin,
child: AlertStyleWidget(
title: Text(title ?? ''),
content: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(message),
Padding(
padding: const EdgeInsets.only(top: 15.0),
child: Text(appMessages.message(UpgraderMessage.prompt) ?? '')),
if (notes != null) notes,
],
),
actions: actions(appMessages),
),
);
}

void forceUpdateState() => setState(() {});
void forceRebuild() => setState(() {});

List<Widget> actions(UpgraderMessages appMessages) {
final isBlocked = widget.upgrader.blocked();
final showIgnore = isBlocked ? false : widget.showIgnore;
final showLater = isBlocked ? false : widget.showLater;
return <Widget>[
if (showIgnore)
TextButton(
child: Text(
appMessages.message(UpgraderMessage.buttonTitleIgnore) ?? ''),
onPressed: () {
// Save the date/time as the last time alerted.
widget.upgrader.saveLastAlerted();

onUserIgnored();
forceRebuild();
}),
if (showLater)
TextButton(
child: Text(
appMessages.message(UpgraderMessage.buttonTitleLater) ?? ''),
onPressed: () {
// Save the date/time as the last time alerted.
widget.upgrader.saveLastAlerted();

onUserLater();
forceRebuild();
}),
TextButton(
child: Text(
appMessages.message(UpgraderMessage.buttonTitleUpdate) ?? ''),
onPressed: () {
// Save the date/time as the last time alerted.
widget.upgrader.saveLastAlerted();

onUserUpdated();
}),
];
}

bool get shouldDisplayReleaseNotes =>
widget.showReleaseNotes &&
Expand All @@ -212,7 +217,7 @@ class UpgradeCardBaseState extends State<UpgradeCard> {
widget.upgrader.saveIgnored();
}

forceUpdateState();
forceRebuild();
}

void onUserLater() {
Expand All @@ -223,7 +228,7 @@ class UpgradeCardBaseState extends State<UpgradeCard> {
// If this callback has been provided, call it.
widget.onLater?.call();

forceUpdateState();
forceRebuild();
}

void onUserUpdated() {
Expand All @@ -238,6 +243,6 @@ class UpgradeCardBaseState extends State<UpgradeCard> {
widget.upgrader.sendUserToAppStore();
}

forceUpdateState();
forceRebuild();
}
}

0 comments on commit 0635af7

Please sign in to comment.