Im recreating the Google Translate app for a school project. When you translate a text the user can save userInput and translation so that they can go back to se it. The problem with my code starts when the user wants to remove the previous saved translation. When the user presses StarButton the isStarred value changes and I want to delete this previously saved translation and userInput. The problem is that I need to get the index of where the user pressed StarButton and then send it to removeFromSharedPreferences(). I have tried multiple options and I will be grateful for every help.
Since there is no code provided i will assume that StarButton is a custom widget that you created. You could simply put an attribute index to its class and then pass it in the constructor. In this way, every time you initialize a StarButton, you're gonna pass and index that identifies its position.
class StarButton extends StatefulWidget {
final int index;
const StarButton ({ super.key, required this.index});
#override
State<StarButton> createState() => _StarButtonState();
}
class _StarButtonState extends State<StarButton> {
#override
Widget build(BuildContext context) {
return YourWidget();
}
}
Related
I have voter id api and I want show the show specific voter details input from the textfield eg if the user put in text field voter name and voter show the all details in next page specific voteruser details
1. Create a TextEditController, which is a class you can link to the input field to get the text inside it (it’s good practice to put it inside a StatefulWidget, in this way we can dispose it):
// Define a custom Form widget.
class MyCustomForm extends StatefulWidget {
const MyCustomForm({Key? key}) : super(key: key);
#override
_MyCustomFormState createState() => _MyCustomFormState();
}
// Define a corresponding State class.
// This class holds the data related to the Form.
class _MyCustomFormState extends State<MyCustomForm> {
// Create a text controller and use it to retrieve the current value
// of the TextField.
final myController = TextEditingController();
#override
void dispose() {
// Clean up the controller when the widget is disposed.
myController.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
// Fill this out in the next step.
}
}
2. Link the controller with your text field:
TextField(
controller: myController,
);
3. Get text with:
myController.text
4. Implementing Navigator class, send data as argument of your next page following the official documentation:
Basic Navigator implementation: https://flutter.dev/docs/cookbook/navigation/navigation-basics
Pass arguments to the next screen: https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments
I'm trying Flutter and I need (I think I do) an app state management to share datas across widgets and dont have to make an http request each time a route is called.
I have Places and Events, so I first load my Places to list them at creation of app state with :
void main() {
runApp(
ChangeNotifierProvider(
create: (context) => AppStateModel()..fetchPlaces(),
...
)
);
}
When I click on a Place, I go on place/id screen and as Places doesnt have events props yet, I'm trying to load them with :
class PlacePageArguments {
final String id;
PlacePageArguments(this.id);
}
class PlacePage extends StatefulWidget {
const PlacePage({Key? key}) : super(key: key);
#override
State<PlacePage> createState() => _PlacePageState();
}
class _PlacePageState extends State<PlacePage> {
String id = '';
#override
Widget build(BuildContext context) {
final args =
ModalRoute.of(context)!.settings.arguments as PlacePageArguments;
return Consumer<AppStateModel>(builder: (context, appState, child) {
id = args.id;
appState.fetchEvents(id);
final place = appState.getPlaceById(id);
return Scaffold(...);
})
}
}
But for sure, as I notifyChange to update widget, It does an infinite loop on fetch events.
What should I do ?
What is the best to achieve something like that, maybe a simple futurBuilder will work, but I want to add events and stay on the same page (add event with modal) and want instant result.
thanks for all
You have two options for bringing in the data for this and none of them require ChangeNotifierProvider.
You can pass data via constructors. This works fine for small widget trees but it can easily get complicated.
You can use Providers. Providers allow you to manage data and functions in one class that stays in one file. For more deals please look here.
Actually I was working on a GoogleSignIn button, where when signed in, I am navigating and sending data to another widget/page for the Drawer. What I want is that this accessed data from google should be sent to all other drawers built in scaffold for my entire app. I know there is a better approach for Global drawer, but having different drawers for each of my scaffold is my app's requirement.
This is the first widget's code:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
RestaurantHome(detailsUser: details)));
This is code of where I'm getting data using constructor for other widget.
class Home extends StatefulWidget {
final UserDetails detailsUser;
Home({Key key, this.detailsUser}) : super(key: key);
#override
_HomeState createState() => _HomeState();
}
In above scenario, I'm only able to send data to Home class using constructor. But I actually want to send data to multiple classes for accessing drawer details there while navigating at once.
Please guide me the best approach in this problem.
Think of me as a beginner. Thanks
you could provide your user details class by the entire app, a recommend using get_it(https://pub.dev/packages/get_it) to that
you create a class like:
class AuthController {
UserDetails detailsUser;
}
on login function:
void login() {
/// do whatherver you want
getIt<AuthController>().detailsUser = details;
}
on anywhere in your app
class Home extends StatefulWidget {
final UserDetails detailsUser = getIt<AuthController>().detailsUser;
Home();
#override
_HomeState createState() => _HomeState();
}
so this way you can acess the user details in any of your drawers.
just take a look at get_it documentation to see how to configure it, it's simple.
You can do this thing with redux ,provider,bloc. there is multiple to get data in any where in app.
If you want to create this pattern I can help your.
How do I preserve my data when I navigate between two different tabs in Flutter... Basically what I want to achieve is, on one side of the tab, I see users data, and on the other side of the tab, I want to be able to copy some of those user data and paste into some textfields on the other tab without losing the data in the text fields when I navigate back and forth and still also preserving the level of scrolling I might have done on the tab where the users data show.
You need to use AutomaticKeepAliveClientMixin This will ensure the State instance is not destroyed when leaving the screen.Extend your every tabview's state class with it and create an override method wannaKeepAlive and set it to true.Here is an example of a widget using AutomaticKeepAliveClientMixin.
class Example extends StatefulWidget {
#override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> with AutomaticKeepAliveClientMixin {
#override
Widget build(BuildContext context) {
return Container(
);
}
#override
// TODO: implement wantKeepAlive
bool get wantKeepAlive => true;
}
The variable value is not refreshing in flutter(which I created outside the class). The following way I have added.So in button press am setting different value. So if I come back to that screen again it is not showing 1. May I know why it is happening?
int _currVal = 1;
class AskFragment extends StatefulWidget {
static const String routeName = "//";
static const
int currState = -1;
#override
HomeScreenState createState() => HomeScreenState();
}
To update the values, use Stateful Widget and initialise values inside that Widget only, for any updates in the values of variables mention that in setState((){}) method to notify the change in the values.
See this for documentation of stateful widgets
See this for documentation of setState((){}) method
setState(() { _myVariable = newValue });
Note:- Never initialise changing values in build method they will not get updated instead they will be reinitialised with same values again and again