Subscription Page
Loading preview
Starting the Flutter app
A showcase of Flutter's subscription page functionality
import 'package:flutter/material.dart';
class SubscriptionPage extends StatefulWidget {
const SubscriptionPage({super.key});
@override
State<SubscriptionPage> createState() => _SubscriptionPageState();
}
class _SubscriptionPageState extends State<SubscriptionPage> {
var _interval = 'monthly';
var _selectedPlan = 'pro';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Subscription')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SegmentedButton<String>(
segments: const [
ButtonSegment(value: 'monthly', label: Text('Monthly')),
ButtonSegment(value: 'yearly', label: Text('Yearly')),
],
selected: {_interval},
onSelectionChanged: (s) => setState(() => _interval = s.first),
),
const SizedBox(height: 24),
RadioGroup<String>(
groupValue: _selectedPlan,
onChanged: (v) => setState(() => _selectedPlan = v ?? 'pro'),
child: Column(
children: [
Card(
child: RadioListTile<String>(
value: 'pro',
secondary: const Icon(Icons.star),
title: const Text('Pro Plan'),
subtitle: Text(_interval == 'yearly' ? '\$190/year (save 17%)' : '\$19/month'),
),
),
const SizedBox(height: 8),
Card(
child: RadioListTile<String>(
value: 'ent',
secondary: const Icon(Icons.business),
title: const Text('Enterprise'),
subtitle: Text(_interval == 'yearly' ? '\$990/year' : '\$99/month'),
),
),
],
),
),
const Spacer(),
FilledButton(
onPressed: () => _showDemo(context, 'Continue (demo)'),
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 12),
child: Text('Continue'),
),
),
],
),
),
);
}
}
void _showDemo(BuildContext context, String message) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(message)));
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
_interval and _selectedPlan track a SegmentedButton and a RadioGroup. RadioGroup owns the group value, so each RadioListTile only declares its own value. Prices are ternaries on _interval written directly in the subtitles, with save 17% shown only for Pro though Enterprise has the same discount. Continue shows a demo SnackBar.
Adapting it
- Move prices to a plan model, and calculate the
save 17%figure instead of typing it. - Start checkout from
Continueusing_selectedPlanand_interval. - Replace the string keys
'pro'and'ent'with an enum.