Final Suggetion
Final Suggetion
SafeArea Widget:
Purpose: Ensures UI elements stay within the safe area of the screen.
Use Case: Wrap your app's main content with SafeArea for consistent layouts.
@override
return SafeArea(
child: Scaffold(
),
);
int counter = 0;
void increment() {
counter++;
notifyListeners();
@override
return ChangeNotifierProvider(
child: Scaffold(
appBar: AppBar(
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Count: ${Provider.of<MyAppState>(context).counter}',
),
ElevatedButton(
child: Text('Increment'),
),
],
),
),
),
);
@override
List<Contact> _contacts = [
@override
return Scaffold(
appBar: AppBar(
title: Text('Contacts'),
actions: [
IconButton(
icon: Icon(Icons.search),
context: context,
delegate: MySearchDelegate(_contacts),
),
),
],
),
body: ListView.builder(
contact.name.toLowerCase().contains(_query.toLowerCase())).toList().length,
_contacts.where((contact) =>
contact.name.toLowerCase().contains(_query.toLowerCase())).toList()[index],
),
),
);
Concept: Base class for managing state that can be inherited by descendant widgets.
Use Case: Suitable for small-scale apps or limited widget state sharing.
return context.dependOnInheritedWidgetOfExactType<MyThemeData>();
@override
@override
9. Keys in Flutter:
@override
return MyThemeData(
data: ThemeData(
),
child: MaterialApp(
),
);
StatefulWidget: Manages state (data that can change) and rebuilds UI when state
changes (more complex).
o When to use: UI that needs to react to user interaction or data updates (e.g., forms,
lists with dynamic content).
11. setState():
Creating a custom widget in Flutter involves defining a new class that extends either
StatelessWidget or StatefulWidget. Here’s an example of a custom widget that displays a
styled text:
dart
Copy code
import 'package:flutter/material.dart';
CustomTextWidget({required this.text});
@override
Widget build(BuildContext context) {
return Text(
text,
style: TextStyle(fontSize: 24, color: Colors.blue, fontWeight:
FontWeight.bold),
);
}
}
void main() {
runApp(MyApp());
}
The GestureDetector widget in Flutter is used to detect and respond to various touch events.
Here’s an example of how to use it to handle a tap event:
dart
Copy code
import 'package:flutter/material.dart';
void main() {
runApp(GestureDetectorExample());
}
The Scaffold widget in Flutter provides a basic visual structure for an app. It includes
commonly used UI components like an app bar, drawer, floating action button, and bottom
navigation bar. Here’s an example:
dart
Copy code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}