Skip to content

Commit 77db215

Browse files
committed
qns: add questions from 51 up to 80
1 parent c786ac0 commit 77db215

File tree

60 files changed

+755
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+755
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: How do you handle user input in Flutter?
3+
---
4+
5+
In Flutter, we can handle user input through various widgets such as `TextField`, `Checkbox`, `Radio`, `Slider`, `DropdownButton`, `GestureDetector`, and more.
6+
7+
Here is a simple process for handling user input in Flutter:
8+
9+
1. Determine the widget that captures the user's input, such as a `TextField` for text input or a `Checkbox` for boolean input.
10+
2. Attach a callback function to the widget that should be called when the user interacts with it. For example, we can attach the `onChanged` callback to a `Checkbox` to be notified when the user taps the checkbox.
11+
3. In the callback function, update the state of the application based on the user's input. For example, we can update a bool variable when the user toggles a `Checkbox`, or update a String variable when the user types into a `TextField`.
12+
4. If the state has changed, call `setState()` to rebuild the UI with the updated state.
13+
14+
It's important to note that the specific process for handling user input will vary depending on the widget and the requirements of the app.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"slug": "how-do-you-handle-user-input-in-flutter",
3+
"rank": 60,
4+
"difficulty": "intermediate"
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: How do you navigate between screens in Flutter?
3+
---
4+
5+
In Flutter, we can navigate between screens using the `Navigator` class. We can push a new screen onto the stack using the `push` method, and pop the current screen off the stack using the `pop` method. We can also use named routes to navigate between screens. To do this, we need to define a map of named routes in the `MaterialApp`, and then use the `Navigator.pushNamed` method to navigate to a specific named route.
6+
7+
Here are the basic steps to navigate between screens in Flutter:
8+
9+
1. Define the screens or pages as separate widgets, each with its own unique name and content.
10+
2. Use the `Navigator.push()` method to push a new Route onto the stack.
11+
```dart
12+
Navigator.push(
13+
context,
14+
MaterialPageRoute(builder: (context) => SecondScreen()),
15+
);
16+
```
17+
3. Now, to go back to the previous screen use the `Navigator.pop()` method.
18+
```dart
19+
Navigator.pop(context),
20+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"slug": "how-do-you-navigate-between-screens-in-flutter",
3+
"rank": 66,
4+
"difficulty": "easy"
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
title: How do you pass data between screens in Flutter?
3+
---
4+
5+
In Flutter, we can pass data between screens using various methods. Some of the most commonly used methods are:
6+
7+
1. **Using constructor parameters**: We can pass data between screens by passing the required data as parameters to the constructor of the destination screen.
8+
9+
```dart
10+
Navigator.push(
11+
context,
12+
MaterialPageRoute(
13+
builder: (context) => SecondScreen(data: 'Hello World!'),
14+
),
15+
);
16+
17+
class SecondScreen extends StatelessWidget {
18+
final String data;
19+
20+
SecondScreen({required this.data});
21+
22+
@override
23+
Widget build(BuildContext context) {
24+
return Scaffold( ... );
25+
}
26+
}
27+
```
28+
29+
2. **Using named routes**: We can define named routes in the app and pass data as arguments to these routes.
30+
31+
```dart
32+
Navigator.pushNamed(
33+
context,
34+
'/second',
35+
arguments: 'Hello World!',
36+
);
37+
38+
class SecondScreen extends StatelessWidget {
39+
@override
40+
Widget build(BuildContext context) {
41+
final String data = ModalRoute.of(context)!.settings.arguments as String;
42+
43+
return Scaffold( ... );
44+
}
45+
}
46+
```
47+
48+
3. **Using a state management library**: We can use a state management library like Provider, Riverpod, Bloc, or MobX to manage the state of the app and pass data between screens. This approach is especially useful when complex data needs to be shared between multiple screens.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"slug": "how-do-you-pass-data-between-screens-in-flutter",
3+
"rank": 70,
4+
"difficulty": "easy"
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title: How do you use the `Positioned` widget in Flutter?
3+
---
4+
5+
In Flutter, the `Positioned` widget is used to position a child widget within a `Stack` widget. The `Positioned` widget takes four optional arguments: `left`, `top`, `right`, and `bottom`, which specify the position of the child widget relative to the edges of the parent `Stack` widget.
6+
7+
For example:
8+
9+
```dart
10+
Stack(
11+
children: [
12+
Positioned(
13+
left: 10,
14+
top: 10,
15+
child: Text('Hi there!'),
16+
),
17+
Positioned(
18+
right: 10,
19+
bottom: 10,
20+
child: Text('Bye there!'),
21+
),
22+
],
23+
),
24+
```
25+
26+
In this example, we create a Stack widget with two child widgets that are positioned at opposite corners of the Stack. The first child widget is positioned 10 pixels from the left and top edges of the Stack, while the second child widget is positioned 10 pixels from the right and bottom edges of the Stack.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"slug": "how-do-you-use-the-positioned-widget-in-flutter",
3+
"rank": 69,
4+
"difficulty": "easy"
5+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: How do you use the `ValueNotifier` class in Flutter?
3+
---
4+
5+
In Flutter, the `ValueNotifier` class is a simple class that provides a way to listen for changes to a value and update the UI accordingly. It's a convenient way to manage state for a small number of widgets, and is often used in combination with the `ChangeNotifier` and `Provider` classes from the Flutter `provider` package.
6+
7+
To use `ValueNotifier`, first we need an instance of the class with an initial value:
8+
9+
```dart
10+
final ValueNotifier<String> _myValue = ValueNotifier<String>('Initial Value');
11+
```
12+
13+
To listen for changes to the value, we can use the `ValueListenableBuilder` widget. This widget takes a `ValueNotifier` and a `builder` function, and rebuilds its child whenever the value changes:
14+
15+
```dart
16+
ValueListenableBuilder<String>(
17+
valueListenable: _myValue,
18+
builder: (BuildContext context, String value, Widget? child) {
19+
return Text(value);
20+
},
21+
),
22+
```
23+
24+
To update the value of a `ValueNotifier`, simply set its value property:
25+
26+
```dart
27+
_myValue.value = 'New Value';
28+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"slug": "how-do-you-use-the-valuenotifier-class-in-flutter",
3+
"rank": 68,
4+
"difficulty": "intermediate"
5+
}

0 commit comments

Comments
 (0)