Created with <3 with dartpad.dev.
Created
October 17, 2022 11:01
-
-
Save iBelow/6b6a9e40587a54b4b0e771414d869241 to your computer and use it in GitHub Desktop.
Counter example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:provider/provider.dart'; | |
import 'dart:async'; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const BookmarkScreen(), | |
); | |
} | |
} | |
class BookmarkScreen extends StatefulWidget { | |
const BookmarkScreen({super.key}); | |
@override | |
State<BookmarkScreen> createState() => _BookmarkScreenState(); | |
} | |
class _BookmarkScreenState extends State<BookmarkScreen> { | |
ButtonExampleProvider? provider; | |
@override | |
void initState() { | |
provider = ButtonExampleProvider(); | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
body: Center( | |
///Провайдер | |
child: ChangeNotifierProvider.value( | |
value: provider, | |
builder: (context, child) { | |
return Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: [ | |
ElevatedButton( | |
style: ElevatedButton.styleFrom( | |
backgroundColor: context.watch<ButtonExampleProvider>().isStarted | |
? Colors.grey | |
: Colors.green), | |
onPressed: () => context.read<ButtonExampleProvider>().isStarted | |
? {} | |
: Provider.of<ButtonExampleProvider>(context, listen: false).timerStart(), | |
child: Text(context.watch<ButtonExampleProvider>().isStarted | |
? "Not working now" | |
: 'Press me')), | |
Text( | |
'Timer: ${context.watch<ButtonExampleProvider>().duration?.inMinutes}m:${context.watch<ButtonExampleProvider>().duration?.inSeconds}s'), | |
ElevatedButton(onPressed: () {}, child: const Text('Permanent switch-off button')) | |
], | |
); | |
}, | |
)), | |
); | |
} | |
} | |
class ButtonExampleProvider extends ChangeNotifier { | |
bool isStarted = false; | |
Timer? mytimer; | |
Duration? duration = const Duration(seconds: 0); | |
@override | |
void dispose() { | |
mytimer?.cancel(); | |
super.dispose(); | |
} | |
void timerStart() { | |
var startTime = DateTime.now(); | |
mytimer = Timer.periodic(const Duration(seconds: 1), (timer) async { | |
isStarted = true; | |
duration = DateTime.now().difference(startTime); | |
if (timer.tick < 11) { | |
isStarted = true; | |
} else { | |
isStarted = false; | |
timer.cancel(); | |
} | |
notifyListeners(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment