Skip to content

Instantly share code, notes, and snippets.

@mono0926
Created March 23, 2022 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mono0926/4cb2da3e314d0c504cafeb162d68c6c7 to your computer and use it in GitHub Desktop.
Save mono0926/4cb2da3e314d0c504cafeb162d68c6c7 to your computer and use it in GitHub Desktop.
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flutter code sample for ThemeExtension
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
void main() => runApp(const ProviderScope(
child: App(),
));
class App extends StatelessWidget {
const App({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: HomePage(),
);
}
}
class HomePage extends ConsumerWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
return Scaffold(
body: Center(
child: ElevatedButton(
child: const Text('Open'),
onPressed: () => Navigator.of(context).push<void>(
MaterialPageRoute(
builder: (context) => const NowPage(),
),
),
),
),
);
}
}
final _format = DateFormat.Hms();
final nowDisposeDelay = Provider.autoDispose(
(ref) => _format.format(DateTime.now()),
disposeDelay: const Duration(seconds: 5),
);
final nowCacheTime = Provider.autoDispose(
(ref) => _format.format(DateTime.now()),
cacheTime: const Duration(seconds: 5),
);
class NowPage extends ConsumerWidget {
const NowPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final textStyle = Theme.of(context).textTheme.headlineSmall;
return Scaffold(
appBar: AppBar(),
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
const SizedBox(height: 16),
Text(
'nowDisposeDelay: ${ref.watch(nowDisposeDelay)}',
style: textStyle,
),
const SizedBox(height: 8),
Text(
'nowCacheTime: ${ref.watch(nowCacheTime)}',
style: textStyle,
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment