I have made a simple example from the flip-card-animation where the coin should be toggled with, fpc.toggleCard()
, when it has been toggled 3 times, but it does not work.
import 'package:flip_card/flip_card.dart';
import 'package:flip_card/flip_card_controller.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(
ChangeNotifierProvider(
create: (context) => Counter(),
child: const AppWidget(),
),
);
class Counter with ChangeNotifier {
int value = 0;
var fpc = FlipCardController();
void increment() {
value += 1;
print('Value is now: $value');
if (value == 3) {
print('Value is 3 - toogle card');
fpc.toggleCard();
}
notifyListeners();
}
}
class AppWidget extends StatefulWidget {
const AppWidget({Key? key}) : super(key: key);
@override
State<AppWidget> createState() => _AppWidgetState();
}
class _AppWidgetState extends State<AppWidget> {
late FlipCardController _controller;
@override
void initState() {
super.initState();
_controller = FlipCardController();
}
@override
Widget build(BuildContext context) {
var counter = context.read<Counter>();
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Material App',
home: Scaffold(
backgroundColor: const Color.fromARGB(172, 81, 187, 177),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlipCard(
// controller: _controller,
back: const Image(
image: AssetImage('assets/images/coin-back.png'),
width: 200,
),
front: const Image(
image: AssetImage('assets/images/coin-front.png'),
width: 200,
),
onFlip: () {
print('Flipped');
counter.increment();
_controller.toggleCard();
},
),
const SizedBox(
height: 20,
),
const Text('You have clicked...'),
Consumer<Counter>(
builder: (context, counter, child) =>
Text('Clicked: ${counter.value}'),),
IconButton(
onPressed: () {
if (!_controller.state!.isFront) {
_controller.toggleCard();
}
},
icon: const Icon(Icons.threesixty_rounded),
iconSize: 70,
color: const Color.fromARGB(255, 255, 255, 255),
),
],
),
),
),
);
}
}
Github: github.com/Genjito/flutter-flip-coin-test and give some advice on it.
How to let it toggle? Later I want to add some more coins and a provider for state management.
2
Answers
@HandLeg To avoid the infinite loop I have put the toogleCard() method in " onFlipDone". It is theoretical working. The counter is increased, but the card is not flipped. Does the app not noticed that it should change the state?
looks like you are creating new controller in _appWidgetState
try passing controller from Counter class