I am creating a Flutter app using the Flip Card Package. I would like to be able to flip a card and see a randomly selected image. Example: Flash cards, one side is a static image, I flip it to see a picture of a dog. I then tap again and it flips to back to the static image. I tap again, and I see a picture of a cat, etc...
I was able to get it all set up. The card flips back and forth but it is not randomly selecting a new card unless I restart the build after each flip. So, I am missing a piece somewhere that tells the app to pick again. Your help would be super appreciated!
Flip Card description https://pub.dev/packages/flip_card
snippet of code
Just as a tip as it seems you are new to SO - please post the code as text so that we can paste is in an IDE and try to run it. As an image its much harder to replicate your issue. Just a tip for going forward. Otherwise, welcome to SO!
So for your issue, I am not certain exactly why since I cannot see the entire context within that snippet BUT i suspect its because the card doesn't know it needs to rebuild since its just being replaced by another image but it doesn't know its a new image.
You can create a Widget class thats stateless and pass in the path. The Key change will ensure that the widget gets rebuilt:
The Widget class:
class FlipCardDetails extends StatelessWidget {
final String imagePath;
const FlipCardDetails({Key key, #required this.imagePath}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: Stack(
children: <Widget>[
Image.asset(
this.imagePath,
key: ValueKey<String>(imagePath),
)
],
)
);
}
}
Then in your main code where it says:
back: Container(child: _cardDetails()),
You can replace this with:
back: FlipCardDetails(imagePath: <Provide the image path>);
You can create a key based on the image path itself like so:
key: ValueKey<String>(imagePath)
This all would only work if there is a function you can run when the person clicks the card that would flip the card. That way you can run your randomizer and provide the image path to the widget.
Instead if you can't and the widget needs to calculate its own random path, then make it stateful, and on initState change the image path. That should work as well.
Without more context I can't really be sure if this is right or which solution works better. Let me know and I can to advise more.
I think that the problem is that your calling the 'Random' function only once when initializing your card. Try adding a function in which you call the Random function to change your '_random' and your 'element' and call it every time the card is flipped.You can do that by using the 'onFlipDone' function field of the FlipCard.
Related
I am facing problem to re-render the page when a variable changes in one class/widget (both parent and child widgets are stateful classes).
Here is how it goes:
I have three buttons in one class which changes a variable state (foodCategory).
int foodCategory = 0;
// down in the elevated button body - i am updating the variable
setState(() {
foodCategory = 1;});
While in the other widget, i am using this variable to perform certain actions:
for (var item in foodItems.values.elementAt(foodCategory))
GestureDetector(........ and so on...
However in the second snippet, the widget dose not know if there has been a change and it is not calling the buildcontext again...
I am not sure how to overcome this problem. I have tried valuelistenablebuilder but in vain. Maybe i dont know how to use it in the loop (i am using foodcategory as an int (iterator)).
it happned to be that i was sing valuelistenable builder in a wrong way.
It is easy. Just mark the variable and changes as valueNotifier. In my case, i needed to mark foodCategory as a valueNotifer.
Than, i needed to wrap the Widget (in my case column widget) as ValueListenableBuilder. This solved my issue.
going through some flutter code and discover this flutter code
const _validatedFormzStatuses = <FormzStatus>{
FormzStatus.valid,
FormzStatus.submissionInProgress,
FormzStatus.submissionSuccess,
FormzStatus.submissionFailure,
FormzStatus.submissionCanceled,
};
can anyone share how this constant work?
the curly bracket part, and the generic type.
also any keyword i can use to find answer in the web? am struggling to determine the term of this constant format, thanks.
This const refers that's once u initialled any value to that variable then u can't change the value of that constants variables in future.
For Eg. A man have only 2 Hands and u can not changed man hands number.
so that's why we are using const in flutter or any programming language.
consider we have these three lines of code:
1.const EdgeInsets.all(25.0)
2.const EdgeInsets.all(25.0)
3.const EdgeInsets.all(25.0)
At first line EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget and then it creates a constant object with same value for rendering if it is found in another place.
Hey there is already an object with this value, so just render it.
Hey there is already an object with this value, so just render it.
Now, let us consider these scenario:
1.EdgeInsets.all(25.0)
2.EdgeInsets.all(25.0)
3.EdgeInsets.all(25.0)
At first line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
At the second line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
At third line, EdgeInsets class creates a new object and assigns its values for left, top, right, bottom and render the given widget.
So, by using const we can reduce the time for recreating the same object every time and using it, instead, we create an object once and then reuse it at every time we need.
TL;DR
I need to render LaTeX equations in a Flutter text input field while the user is typing
The Goal
I am currently working on a Flutter project for both web/desktop where I need to be able to render LaTeX/TeX equations in an input field while the user is typing. For example, if the user types “sqrt”, a square root symbol appears where the sqrt was. This will allow for easy math input of complex functions, where the user can type something like:
“int 1 2 sqrt(sin(x/2)^2)”
While typing, this equation would continuously render in the same input box and end up looking like:
Example Equation Render
I plan to allow the user to use a lot of different mathematical symbols/functions.
An extremely similar implementation of what I am trying to achieve is the MathQuill project. However while MathQuill works well with JavaScript and React, I have not found a way to use it in Flutter. If there is a way to port it into Flutter that would work perfectly for my needs.
Requirements and Preferences
Requirements
Render the TeX in the same input box that they are typing in.
Render TeX in real time. The user should be able to see changes in their current text box while typing. A good (but very simple) demo of what I would like can be seen in the math entry boxes on Desmos. I do not want the user to have to press Enter to see their changes, it should happen while typing.
Cannot be click-based equation entry. I want the user to be able to type “sqrt”, “int”, “prod”, “sum”, etc. ... not click on a square root or integral button with some sort of on-screen keyboard. (For example, Flutter’s math_keyboard would not work for my project)
Flutter-based solution.
Preferences
I would prefer to use the KaTeX rendering engine due to it’s substantially increased speed over MathJax. I am still flexible here.
Current Attempts
I have looked through many packages on pub.dev, but have not been able to find a suitable package that suits my use case. I have managed to display TeX on the webpage using the flutter_tex package. For example:
Code:
import 'package:flutter/material.dart';
import 'package:flutter_tex/flutter_tex.dart';
void main() => runApp(TexApp());
class TexApp extends StatelessWidget {
const TexApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text("Testing flutter_tex")),
body: TeXView(
child: TeXViewColumn(children: [
// This widget displays data perfectly as it should, but it is static and
// so the user cannot dynamically edit the cell.
TeXViewDocument(r"""<p>
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}$$</p>""",
style: TeXViewStyle.fromCSS(
'padding: 15px; color: black; background: white')),
]))),
);
}
}
Resulting webpage:
Resulting TeX equations in Flutter web application
However, the above implementation does not allow continuous input/dynamic rendering as the user types their equations which I need for my project.
I am new to Flutter and web development as a whole. However, I spent a lot of time learning React and Flutter and I am certain that I want to use Flutter moving forward for a variety of reasons.
Any help here would be extremely appreciated!
UPDATE: As of August 2022 I still have not found a solution to this issue!
I want to put objects randomly, then on new screen I want player to put widgets as before. How can I do that?
What widgets/ packages would make it easy?
Say you have a list of objects in your widget, that is displayed in order by the build function.
You could use initState to shuffle your list the first time your widget loads, using a shuffle function such as the one provided in this other StackOverflow answer.
List<MyItem> items = [MyItem('first'), MyItem('second'), MyItem('third')];
#override
initState() {
items = shuffle(items);
super.initState();
}
Then when going to the next screen, you can pass the shuffled version of items, and use that as the 'correct' solution.
It's a bit difficult to see what exactly you're looking for as no code was provided, but hopefully this gives you some inspiration for your actual problem.
Try to use CustomPainter and Canvas
so can access Offset
enter image description here
please help me ..how is it possible replace the image with card view and the list with ListView (using silverappbar or anything else)
You can use the Visibility class.
It has a replacement attribute.
Its great that should work.
The code for it can be used like this:
Visibility(
visible: bool_var,
replacement: _showTopCard,
child: image,
)
When bool_var is true, the child widget will be shown and when the bool_var is false the replacement widget will shown.
But make sure that you use widgets as the parameters for replacement and child
The official documentation is:
https://api.flutter.dev/flutter/widgets/Visibility-class.html
And if you want to know more about this class in detail and how to use it in different ways, this link might help:
https://medium.com/#danle.sdev/widget-hide-and-seek-a-guide-to-managing-flutter-widgets-visibility-d7977cbaf444