Notification Settings
Loading preview
Starting the Flutter app
A showcase of Flutter's notification settings functionality
import 'package:flutter/material.dart';
class NotificationSettings extends StatefulWidget {
const NotificationSettings({super.key});
@override
State<NotificationSettings> createState() => _NotificationSettingsState();
}
class _NotificationSettingsState extends State<NotificationSettings> {
var _push = true;
var _email = true;
var _marketing = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Notification Settings')),
body: ListView(
children: [
SwitchListTile(
title: const Text('Push Notifications'),
subtitle: const Text('Receive notifications on your device'),
value: _push,
onChanged: (v) => setState(() => _push = v),
),
SwitchListTile(
title: const Text('Email Notifications'),
subtitle: const Text('Get updates via email'),
value: _email,
onChanged: (v) => setState(() => _email = v),
),
SwitchListTile(
title: const Text('Marketing'),
subtitle: const Text('Promotional offers and news'),
value: _marketing,
onChanged: (v) => setState(() => _marketing = v),
),
const Divider(),
ListTile(
leading: const Icon(Icons.schedule),
title: const Text('Quiet Hours'),
subtitle: const Text('10:00 PM - 8:00 AM'),
trailing: const Icon(Icons.chevron_right),
onTap: () => _showDemo(context, 'Quiet Hours (demo)'),
),
],
),
);
}
}
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
Three booleans, _push, _email and _marketing, each bound to a SwitchListTile. Toggles respond immediately but live only in memory, so they revert to defaults when the page is recreated. Below the divider, Quiet Hours is a static ListTile with a fixed time range and a demo SnackBar on tap.
Adapting it
- Load initial values in
initState, and save from eachonChangedtoshared_preferencesor your API. - When
_pushis switched on, request OS notification permission and revert if it’s denied. - Use
showTimePickerin theQuiet HoursonTap, then render the stored times.