Material 3 की मदद से, ऐनिमेशन वाला रिस्पॉन्सिव ऐप्लिकेशन लेआउट बनाना

Material 3 की मदद से, ऐनिमेशन वाला रिस्पॉन्सिव ऐप्लिकेशन लेआउट बनाना

इस कोडलैब (कोड बनाना सीखने के लिए ट्यूटोरियल) के बारे में जानकारी

subjectपिछली बार जून 6, 2025 को अपडेट किया गया
account_circleBrett Morgan ने लिखा

1. परिचय

Material 3, Google के ओपन-सोर्स डिज़ाइन सिस्टम का नया वर्शन है. Flutter, Material 3 का इस्तेमाल करके खूबसूरत ऐप्लिकेशन बनाने के लिए सहायता को बढ़ा रहा है. इस कोडलैब में, आपको एक खाली Flutter ऐप्लिकेशन से शुरुआत करनी होगी. इसके बाद, Flutter के साथ Material 3 का इस्तेमाल करके, पूरी तरह से स्टाइल किया गया और ऐनिमेशन वाला ऐप्लिकेशन बनाना होगा.

आपको क्या बनाना है

इस कोडलैब में, आपको एक मॉक मैसेजिंग ऐप्लिकेशन बनाना है. आपका ऐप्लिकेशन:

  • डेस्कटॉप या मोबाइल पर काम करने के लिए, अडैप्टिव डिज़ाइन का इस्तेमाल करें.
  • अलग-अलग लेआउट के बीच आसानी से स्विच करने के लिए, ऐनिमेशन का इस्तेमाल करें.
  • स्टाइल को बेहतर बनाने के लिए, Material 3 का इस्तेमाल करें.
  • Android, iOS, वेब, Windows, Linux, और macOS पर काम करता हो.

4111f0eb4aba816f.png

इस कोडलैब में, Flutter के साथ Material 3 पर फ़ोकस किया गया है. काम के नहीं लगने वाले कॉन्सेप्ट और कोड ब्लॉक को हटा दिया जाता है. साथ ही, उन्हें कॉपी और चिपकाने के लिए उपलब्ध कराया जाता है.

2. Flutter एनवायरमेंट सेट अप करना

आपको किन चीज़ों की ज़रूरत होगी

इस कोडलैब को Android, iOS, वेब, Windows, Linux, और macOS पर डिप्लॉय करने के लिए टेस्ट किया गया है. डिप्लॉयमेंट के लिए, इनमें से कुछ टारगेट पर अतिरिक्त सॉफ़्टवेयर इंस्टॉल करना ज़रूरी होता है. flutter doctor को चलाकर, यह पता लगाया जा सकता है कि आपका प्लैटफ़ॉर्म सही तरीके से सेट अप किया गया है या नहीं.

$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.32.1, on macOS 15.5 24F74 darwin-arm64, locale en-AU)
[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] IntelliJ IDEA Community Edition (version 2024.3.1.1)
[✓] VS Code (version 1.100.3)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

अगर आउटपुट में ऐसी समस्याएं हैं जिनका असर, डिप्लॉयमेंट के लिए चुने गए टारगेट पर पड़ता है, तो ज़्यादा जानकारी पाने के लिए flutter doctor -v चलाएं. अगर flutter doctor -v ने जो तरीके बताए हैं उन्हें आज़माने के बाद भी समस्या हल नहीं होती है, तो Flutter कम्यूनिटी से संपर्क करें.

3. शुरू करना

खाली Flutter ऐप्लिकेशन बनाना

ज़्यादातर Flutter डेवलपर, flutter create का इस्तेमाल करके "बटन टैप की संख्या गिनने वाला" बुनियादी ऐप्लिकेशन बनाते हैं. इसके बाद, वे कुछ मिनट बिताकर उस कॉन्टेंट को हटा देते हैं जिसकी उन्हें ज़रूरत नहीं है. --empty पैरामीटर का इस्तेमाल करके, खाली Flutter प्रोजेक्ट बनाया जा सकता है. इसमें सिर्फ़ वे ज़रूरी चीज़ें होती हैं जो ऐप्लिकेशन को चलाने के लिए ज़रूरी होती हैं.

$ flutter create animated_responsive_layout --empty
Creating project animated_responsive_layout...
Resolving dependencies in `animated_responsive_layout`...
Downloading packages...
Got dependencies in `animated_responsive_layout`.
Wrote 129 files.

All done!
You can find general documentation for Flutter at: https://docs.flutter.dev/
Detailed API documentation is available at: https://api.flutter.dev/
If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev

In order to run your empty application, type:

  $ cd animated_responsive_layout
  $ flutter run

Your empty application code is in animated_responsive_layout/lib/main.dart.

इस कोड को, कोड एडिटर या सीधे कमांड लाइन से चलाया जा सकता है. आपने कौनसी टूलचेन इंस्टॉल की हैं और आपके पास सिम्युलेटर या एमुलेटर चल रहे हैं या नहीं, इसके आधार पर आपसे यह तय करने के लिए कहा जा सकता है कि ऐप्लिकेशन को किस डिप्लॉयमेंट टारगेट पर चलाना है. उदाहरण के लिए, यहां बताया गया है कि "Chrome" विकल्प चुनकर, वेब ब्राउज़र में खाली ऐप्लिकेशन को कैसे चलाया जा सकता है.

$ cd animated_responsive_layout
$ flutter run
Connected devices:
macOS (desktop) • macos  • darwin-arm64   • macOS 15.5 24F74 darwin-arm64
Chrome (web)    • chrome • web-javascript • Google Chrome 137.0.7151.56

[1]: macOS (macos)
[2]: Chrome (chrome)
Please choose one (or "q" to quit): 2
Launching lib/main.dart on Chrome in debug mode...
Waiting for connection from debug service on Chrome...              6.4s
This app is linked to the debug service: ws://127.0.0.1:60848/AM68Aq_ZiB8=/ws
Debug service listening on ws://127.0.0.1:60848/AM68Aq_ZiB8=/ws

Flutter run key commands.
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

A Dart VM Service on Chrome is available at: http://127.0.0.1:60848/AM68Aq_ZiB8=
The Flutter DevTools debugger and profiler on Chrome is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:60848/AM68Aq_ZiB8=
Application finished.

इस स्थिति में, आपको Chrome वेब ब्राउज़र में खाली ऐप्लिकेशन दिखेगा. इसे Android, iOS या अपने डेस्कटॉप ऑपरेटिंग सिस्टम में भी चलाया जा सकता है.

152efb79ce615edb.png

4. मैसेंजर ऐप्लिकेशन बनाना

अवतार बनाना

हर मैसेजिंग ऐप्लिकेशन को अपने उपयोगकर्ताओं की इमेज की ज़रूरत होती है. ये इमेज, उपयोगकर्ताओं को दिखाती हैं और इन्हें अवतार कहा जाता है. इसके बाद, प्रोजेक्ट ट्री में सबसे ऊपर एक ऐसेट डायरेक्ट्री बनाएं. साथ ही, उसमें इस कोडलैब के लिए GitHub डेटा स्टोर करने की जगह से इमेज की सीरीज़ जोड़ें. ऐसा करने का एक तरीका यह है कि wget कमांड-लाइन टूल का इस्तेमाल यहां बताए गए तरीके से करें.

$ mkdir assets
$ cd assets
$ for name in avatar_1 avatar_2 avatar_3 avatar_4 \
              avatar_5 avatar_6 avatar_7 thumbnail_1; \
  do wget https://raw.githubusercontent.com/flutter/codelabs/main/animated-responsive-layout/step_04/assets/$name.png ; \
  done

इससे, आपके ऐप्लिकेशन की assets डायरेक्ट्री में ये इमेज डाउनलोड हो जाती हैं:

avatar_1.png

avatar_2.png

avatar_3.png

avatar_4.png

avatar_5.png

avatar_6.png

avatar_7.png

thumbnail_1.png

अवतार की इमेज ऐसेट तैयार हो जाने के बाद, आपको उन्हें pubspec.yaml फ़ाइल में इस तरह जोड़ना होगा:

pubspec.yaml

name: animated_responsive_layout
description: "A new Flutter project."
publish_to: 'none'
version: 0.1.0

environment:
  sdk: ^3.8.0

dependencies:
  flutter:
    sdk: flutter

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^5.0.0

flutter:
  uses-material-design: true

                                        # Add from here...
  assets:
    - assets/avatar_1.png
    - assets/avatar_2.png
    - assets/avatar_3.png
    - assets/avatar_4.png
    - assets/avatar_5.png
    - assets/avatar_6.png
    - assets/avatar_7.png
    - assets/thumbnail_1.png
                                        # ... to here.

ऐप्लिकेशन को दिखाए जाने वाले मैसेज के लिए, डेटा सोर्स की ज़रूरत होती है. अपने प्रोजेक्ट की lib डायरेक्ट्री में, models सबडायरेक्ट्री बनाएं. ऐसा करने के लिए, mkdir का इस्तेमाल करके कमांड लाइन पर या अपने पसंदीदा टेक्स्ट एडिटर में जाकर किया जा सकता है. lib/models डायरेक्ट्री में models.dart फ़ाइल बनाएं और उसमें यह कॉन्टेंट शामिल करें:

lib/models/models.dart

class Attachment {
  const Attachment({required this.url});

  final String url;
}

class Email {
  const Email({
    required this.sender,
    required this.recipients,
    required this.subject,
    required this.content,
    this.replies = 0,
    this.attachments = const [],
  });

  final User sender;
  final List<User> recipients;
  final String subject;
  final String content;
  final List<Attachment> attachments;
  final double replies;
}

class Name {
  const Name({required this.first, required this.last});

  final String first;
  final String last;
  String get fullName => '$first $last';
}

class User {
  const User({
    required this.name,
    required this.avatarUrl,
    required this.lastActive,
  });

  final Name name;
  final String avatarUrl;
  final DateTime lastActive;
}

अब आपके पास डेटा के शेप की परिभाषा है. इसलिए, lib/models डायरेक्ट्री में data.dart फ़ाइल बनाएं और उसमें यह कॉन्टेंट शामिल करें:

lib/models/data.dart

import 'models.dart';

final User user_0 = User(
 
name: const Name(first: 'Me', last: ''),
 
avatarUrl: 'assets/avatar_1.png',
 
lastActive: DateTime.now(),
);
final User user_1 = User(
 
name: const Name(first: '老', last: '强'),
 
avatarUrl: 'assets/avatar_2.png',
 
lastActive: DateTime.now().subtract(const Duration(minutes: 10)),
);
final User user_2 = User(
 
name: const Name(first: 'So', last: 'Duri'),
 
avatarUrl: 'assets/avatar_3.png',
 
lastActive: DateTime.now().subtract(const Duration(minutes: 20)),
);
final User user_3 = User(
 
name: const Name(first: 'Lily', last: 'MacDonald'),
 
avatarUrl: 'assets/avatar_4.png',
 
lastActive: DateTime.now().subtract(const Duration(hours: 2)),
);
final User user_4 = User(
 
name: const Name(first: 'Ziad', last: 'Aouad'),
 
avatarUrl: 'assets/avatar_5.png',
 
lastActive: DateTime.now().subtract(const Duration(hours: 6)),
);

final List<Email> emails = [
 
Email(
   
sender: user_1,
   
recipients: [],
   
subject: '豆花鱼',
   
content: '最近忙吗?昨晚我去了你最爱的那家饭馆,点了他们的特色豆花鱼,吃着吃着就想你了。',
 
),
 
Email(
   
sender: user_2,
   
recipients: [],
   
subject: 'Dinner Club',
   
content:
       
'I think it's time for us to finally try that new noodle shop downtown that doesn't use menus. Anyone else have other suggestions for dinner club this week? I'm so intrigued by this idea of a noodle restaurant where no one gets to order for themselves - could be fun, or terrible, or both :)\n\nSo',
 
),
 
Email(
   
sender: user_3,
   
recipients: [],
   
subject: 'This food show is made for you',
   
content:
       
'Ping you'd love this new food show I started watching. It's produced by a Thai drummer who started getting recognized for the amazing vegan food she always brought to shows.',
   
attachments: [const Attachment(url: 'assets/thumbnail_1.png')],
 
),
 
Email(
   
sender: user_4,
   
recipients: [],
   
subject: 'Volunteer EMT with me?',
   
content:
       
'What do you think about training to be volunteer EMTs? We could do it together for moral support. Think about it??',
 
),
];

final List<Email> replies = [
 
Email(
   
sender: user_2,
   
recipients: [user_3, user_2],
   
subject: 'Dinner Club',
   
content:
       
'I think it's time for us to finally try that new noodle shop downtown that doesn't use menus. Anyone else have other suggestions for dinner club this week? I'm so intrigued by this idea of a noodle restaurant where no one gets to order for themselves - could be fun, or terrible, or both :)\n\nSo',
 
),
 
Email(
   
sender: user_0,
   
recipients: [user_3, user_2],
   
subject: 'Dinner Club',
   
content:
       
'Yes! I forgot about that place! I'm definitely up for taking a risk this week and handing control over to this mysterious noodle chef. I wonder what happens if you have allergies though? Lucky none of us have any otherwise I'd be a bit concerned.\n\nThis is going to be great. See you all at the usual time?',
 
),
];

इस डेटा को दिखाने के लिए, कुछ विजेट तय करने का समय आ गया है. lib के तहत widgets नाम की सबडायरेक्ट्री बनाएं. आपको widgets में चार फ़ाइलें बनानी होंगी. जब तक चारों फ़ाइलें नहीं बन जातीं, तब तक आपको एडिटर से कुछ चेतावनियां मिल सकती हैं. याद रखें, इस कोडलैब का मकसद Material 3 का इस्तेमाल करके, ऐप्लिकेशन को स्टाइल करना है. इसलिए, यहां दी गई चारों फ़ाइलों को सूची में दिए गए कॉन्टेंट के साथ जोड़ें:

lib/widgets/email_list_view.dart

import 'package:flutter/material.dart';

import '../models/data.dart' as data;
import '../models/models.dart';
import 'email_widget.dart';
import 'search_bar.dart' as search_bar;

class EmailListView extends StatelessWidget {
 
const EmailListView({
   
super.key,
   
this.selectedIndex,
   
this.onSelected,
   
required this.currentUser,
 
});

 
final int? selectedIndex;
 
final ValueChanged<int>? onSelected;
 
final User currentUser;

 
@override
 
Widget build(BuildContext context) {
   
return Padding(
     
padding: const EdgeInsets.symmetric(horizontal: 8.0),
     
child: ListView(
       
children: [
         
const SizedBox(height: 8),
         
search_bar.SearchBar(currentUser: currentUser),
         
const SizedBox(height: 8),
         
...List.generate(data.emails.length, (index) {
           
return Padding(
             
padding: const EdgeInsets.only(bottom: 8.0),
             
child: EmailWidget(
               
email: data.emails[index],
               
onSelected: onSelected != null
                   
? () {
                       
onSelected!(index);
                     
}
                   
: null,
               
isSelected: selectedIndex == index,
             
),
           
);
         
}),
       
],
     
),
   
);
 
}
}

ईमेल की सूची दिखाने की सुविधा, मैसेजिंग ऐप्लिकेशन में होनी चाहिए. आपको एडिटर से कुछ शिकायतें मिलेंगी, लेकिन अगली फ़ाइल email_widget.dart जोड़कर, इनमें से कुछ को ठीक किया जा सकता है.

lib/widgets/email_widget.dart

import 'package:flutter/material.dart';
import '../models/models.dart';
import 'star_button.dart';

enum EmailType { preview, threaded, primaryThreaded }

class EmailWidget extends StatefulWidget {
 
const EmailWidget({
   
super.key,
   
required this.email,
   
this.isSelected = false,
   
this.isPreview = true,
   
this.isThreaded = false,
   
this.showHeadline = false,
   
this.onSelected,
 
});

 
final bool isSelected;
 
final bool isPreview;
 
final bool showHeadline;
 
final bool isThreaded;
 
final void Function()? onSelected;
 
final Email email;

 
@override
 
State<EmailWidget> createState() => _EmailWidgetState();
}

class _EmailWidgetState extends State<EmailWidget> {
 
late final ColorScheme _colorScheme = Theme.of(context).colorScheme;
 
late Color unselectedColor = Color.alphaBlend(
   
_colorScheme.primary.withAlpha(20),
   
_colorScheme.surface,
 
);

 
Color get _surfaceColor => switch (widget) {
   
EmailWidget(isPreview: false) => _colorScheme.surface,
   
EmailWidget(isSelected: true) => _colorScheme.primaryContainer,
   
_ => unselectedColor,
 
};

 
@override
 
Widget build(BuildContext context) {
   
return GestureDetector(
     
onTap: widget.onSelected,
     
child: Card(
       
elevation: 0,
       
color: _surfaceColor,
       
clipBehavior: Clip.hardEdge,
       
child: Column(
         
crossAxisAlignment: CrossAxisAlignment.start,
         
mainAxisSize: MainAxisSize.min,
         
children: [
           
if (widget.showHeadline) ...[
             
EmailHeadline(email: widget.email, isSelected: widget.isSelected),
           
],
           
EmailContent(
             
email: widget.email,
             
isPreview: widget.isPreview,
             
isThreaded: widget.isThreaded,
             
isSelected: widget.isSelected,
           
),
         
],
       
),
     
),
   
);
 
}
}

class EmailContent extends StatefulWidget {
 
const EmailContent({
   
super.key,
   
required this.email,
   
required this.isPreview,
   
required this.isThreaded,
   
required this.isSelected,
 
});

 
final Email email;
 
final bool isPreview;
 
final bool isThreaded;
 
final bool isSelected;

 
@override
 
State<EmailContent> createState() => _EmailContentState();
}

class _EmailContentState extends State<EmailContent> {
 
late final ColorScheme _colorScheme = Theme.of(context).colorScheme;
 
late final TextTheme _textTheme = Theme.of(context).textTheme;

 
Widget get contentSpacer => SizedBox(height: widget.isThreaded ? 20 : 2);

 
String get lastActiveLabel {
   
final DateTime now = DateTime.now();
   
if (widget.email.sender.lastActive.isAfter(now)) throw ArgumentError();
   
final Duration elapsedTime = widget.email.sender.lastActive
       
.difference(now)
       
.abs();
   
return switch (elapsedTime) {
     
Duration(inSeconds: < 60) => '${elapsedTime.inSeconds}s',
     
Duration(inMinutes: < 60) => '${elapsedTime.inMinutes}m',
     
Duration(inHours: < 24) => '${elapsedTime.inHours}h',
     
Duration(inDays: < 365) => '${elapsedTime.inDays}d',
     
_ => throw UnimplementedError(),
   
};
 
}

 
TextStyle? get contentTextStyle => switch (widget) {
   
EmailContent(isThreaded: true) => _textTheme.bodyLarge,
   
EmailContent(isSelected: true) => _textTheme.bodyMedium?.copyWith(
     
color: _colorScheme.onPrimaryContainer,
   
),
   
_ => _textTheme.bodyMedium?.copyWith(color: _colorScheme.onSurfaceVariant),
 
};

 
@override
 
Widget build(BuildContext context) {
   
return Padding(
     
padding: const EdgeInsets.all(20),
     
child: Column(
       
crossAxisAlignment: CrossAxisAlignment.start,
       
children: [
         
LayoutBuilder(
           
builder: (context, constraints) {
             
return Row(
               
crossAxisAlignment: CrossAxisAlignment.center,
               
children: [
                 
if (constraints.maxWidth - 200 > 0) ...[
                   
CircleAvatar(
                     
backgroundImage: AssetImage(
                       
widget.email.sender.avatarUrl,
                     
),
                   
),
                   
const Padding(
                     
padding: EdgeInsets.symmetric(horizontal: 6.0),
                   
),
                 
],
                 
Expanded(
                   
child: Column(
                     
crossAxisAlignment: CrossAxisAlignment.start,
                     
children: [
                       
Text(
                         
widget.email.sender.name.fullName,
                         
overflow: TextOverflow.fade,
                         
maxLines: 1,
                         
style: widget.isSelected
                             
? _textTheme.labelMedium?.copyWith(
                                 
color: _colorScheme.onSecondaryContainer,
                               
)
                             
: _textTheme.labelMedium?.copyWith(
                                 
color: _colorScheme.onSurface,
                               
),
                       
),
                       
Text(
                         
lastActiveLabel,
                         
overflow: TextOverflow.fade,
                         
maxLines: 1,
                         
style: widget.isSelected
                             
? _textTheme.labelMedium?.copyWith(
                                 
color: _colorScheme.onSecondaryContainer,
                               
)
                             
: _textTheme.labelMedium?.copyWith(
                                 
color: _colorScheme.onSurfaceVariant,
                               
),
                       
),
                     
],
                   
),
                 
),
                 
if (constraints.maxWidth - 200 > 0) ...[const StarButton()],
               
],
             
);
           
},
         
),
         
const SizedBox(width: 8),
         
Column(
           
crossAxisAlignment: CrossAxisAlignment.start,
           
children: [
             
if (widget.isPreview) ...[
               
Text(
                 
widget.email.subject,
                 
style: const TextStyle(
                   
fontSize: 18,
                 
).copyWith(color: _colorScheme.onSurface),
               
),
             
],
             
if (widget.isThreaded) ...[
               
contentSpacer,
               
Text(
                 
"To ${widget.email.recipients.map((recipient) => recipient.name.first).join(", ")}",
                 
style: _textTheme.bodyMedium,
               
),
             
],
             
contentSpacer,
             
Text(
               
widget.email.content,
               
maxLines: widget.isPreview ? 2 : 100,
               
overflow: TextOverflow.ellipsis,
               
style: contentTextStyle,
             
),
           
],
         
),
         
const SizedBox(width: 12),
         
widget.email.attachments.isNotEmpty
             
? Container(
                 
height: 96,
                 
decoration: BoxDecoration(
                   
borderRadius: BorderRadius.circular(8.0),
                   
image: DecorationImage(
                     
fit: BoxFit.cover,
                     
image: AssetImage(widget.email.attachments.first.url),
                   
),
                 
),
               
)
             
: const SizedBox.shrink(),
         
if (!widget.isPreview) ...[const EmailReplyOptions()],
       
],
     
),
   
);
 
}
}

class EmailHeadline extends StatefulWidget {
 
const EmailHeadline({
   
super.key,
   
required this.email,
   
required this.isSelected,
 
});

 
final Email email;
 
final bool isSelected;

 
@override
 
State<EmailHeadline> createState() => _EmailHeadlineState();
}

class _EmailHeadlineState extends State<EmailHeadline> {
 
late final TextTheme _textTheme = Theme.of(context).textTheme;
 
late final ColorScheme _colorScheme = Theme.of(context).colorScheme;

 
@override
 
Widget build(BuildContext context) {
   
return LayoutBuilder(
     
builder: (context, constraints) {
       
return Container(
         
height: 84,
         
color: Color.alphaBlend(
           
_colorScheme.primary.withAlpha(12),
           
_colorScheme.surface,
         
),
         
child: Padding(
           
padding: const EdgeInsets.fromLTRB(24, 12, 12, 12),
           
child: Row(
             
mainAxisSize: MainAxisSize.max,
             
crossAxisAlignment: CrossAxisAlignment.center,
             
children: [
               
Expanded(
                 
child: Column(
                   
mainAxisSize: MainAxisSize.min,
                   
crossAxisAlignment: CrossAxisAlignment.start,
                   
children: [
                     
Text(
                       
widget.email.subject,
                       
maxLines: 1,
                       
overflow: TextOverflow.fade,
                       
style: const TextStyle(
                         
fontSize: 18,
                         
fontWeight: FontWeight.w400,
                       
),
                     
),
                     
Text(
                       
'${widget.email.replies.toString()} Messages',
                       
maxLines: 1,
                       
overflow: TextOverflow.fade,
                       
style: _textTheme.labelMedium?.copyWith(
                         
fontWeight: FontWeight.w500,
                       
),
                     
),
                   
],
                 
),
               
),
               
// Display a "condensed" version if the widget in the row are
               
// expected to overflow.
               
if (constraints.maxWidth - 200 > 0) ...[
                 
SizedBox(
                   
height: 40,
                   
width: 40,
                   
child: FloatingActionButton(
                     
onPressed: () {},
                     
elevation: 0,
                     
backgroundColor: _colorScheme.surface,
                     
child: const Icon(Icons.delete_outline),
                   
),
                 
),
                 
const Padding(padding: EdgeInsets.only(right: 8.0)),
                 
SizedBox(
                   
height: 40,
                   
width: 40,
                   
child: FloatingActionButton(
                     
onPressed: () {},
                     
elevation: 0,
                     
backgroundColor: _colorScheme.surface,
                     
child: const Icon(Icons.more_vert),
                   
),
                 
),
               
],
             
],
           
),
         
),
       
);
     
},
   
);
 
}
}

class EmailReplyOptions extends StatefulWidget {
 
const EmailReplyOptions({super.key});

 
@override
 
State<EmailReplyOptions> createState() => _EmailReplyOptionsState();
}

class _EmailReplyOptionsState extends State<EmailReplyOptions> {
 
late final ColorScheme _colorScheme = Theme.of(context).colorScheme;

 
@override
 
Widget build(BuildContext context) {
   
return LayoutBuilder(
     
builder: (context, constraints) {
       
if (constraints.maxWidth < 100) {
         
return const SizedBox.shrink();
       
}
       
return Row(
         
children: [
           
Expanded(
             
child: TextButton(
               
style: ButtonStyle(
                 
backgroundColor: WidgetStateProperty.all(
                   
_colorScheme.onInverseSurface,
                 
),
               
),
               
onPressed: () {},
               
child: Text(
                 
'Reply',
                 
style: TextStyle(color: _colorScheme.onSurfaceVariant),
               
),
             
),
           
),
           
const SizedBox(width: 8),
           
Expanded(
             
child: TextButton(
               
style: ButtonStyle(
                 
backgroundColor: WidgetStateProperty.all(
                   
_colorScheme.onInverseSurface,
                 
),
               
),
               
onPressed: () {},
               
child: Text(
                 
'Reply All',
                 
style: TextStyle(color: _colorScheme.onSurfaceVariant),
               
),
             
),
           
),
         
],
       
);
     
},
   
);
 
}
}

हां, उस विजेट में बहुत कुछ चल रहा है. इस बारे में ज़्यादा जानकारी देखना ज़रूरी है. खास तौर पर, यह जानना कि पूरे विजेट में रंग कैसे लागू किया जाता है. यह एक बार नहीं, बल्कि बार-बार दिखेगी. इसके बाद, search_bar.dart.

lib/widgets/search_bar.dart

import 'package:flutter/material.dart';

import '../models/models.dart';

class SearchBar extends StatelessWidget {
 
const SearchBar({super.key, required this.currentUser});

 
final User currentUser;

 
@override
 
Widget build(BuildContext context) {
   
return SizedBox(
     
height: 56,
     
child: Container(
       
decoration: BoxDecoration(
         
borderRadius: BorderRadius.circular(100),
         
color: Colors.white,
       
),
       
padding: const EdgeInsets.fromLTRB(31, 12, 12, 12),
       
child: Row(
         
crossAxisAlignment: CrossAxisAlignment.center,
         
children: [
           
const Icon(Icons.search),
           
const SizedBox(width: 23.5),
           
Expanded(
             
child: TextField(
               
maxLines: 1,
               
decoration: InputDecoration(
                 
isDense: true,
                 
border: InputBorder.none,
                 
hintText: 'Search replies',
                 
hintStyle: Theme.of(context).textTheme.bodyMedium,
               
),
             
),
           
),
           
CircleAvatar(backgroundImage: AssetImage(currentUser.avatarUrl)),
         
],
       
),
     
),
   
);
 
}
}

यह एक बहुत ही आसान और स्टेटलेस विजेट है. इसके बाद, कोई दूसरा विजेट, star_button.dart जोड़ें:

lib/widgets/star_button.dart

impoimport 'package:flutter/material.dart';

class StarButton extends StatefulWidget {
 
const StarButton({super.key});

 
@override
 
State<StarButton> createState() => _StarButtonState();
}

class _StarButtonState extends State<StarButton> {
 
bool state = false;
 
late final ColorScheme _colorScheme = Theme.of(context).colorScheme;

 
Icon get icon {
   
final IconData iconData = state ? Icons.star : Icons.star_outline;

   
return Icon(iconData, color: Colors.grey, size: 20);
 
}

 
void _toggle() {
   
setState(() {
     
state = !state;
   
});
 
}

 
double get turns => state ? 1 : 0;

 
@override
 
Widget build(BuildContext context) {
   
return AnimatedRotation(
     
turns: turns,
     
curve: Curves.decelerate,
     
duration: const Duration(milliseconds: 300),
     
child: FloatingActionButton(
       
elevation: 0,
       
shape: const CircleBorder(),
       
backgroundColor: _colorScheme.surface,
       
onPressed: () => _toggle(),
       
child: Padding(padding: const EdgeInsets.all(10.0), child: icon),
     
),
   
);
 
}
}

इसके बाद, शो के मुख्य स्टार lib/main.dart की जानकारी अपडेट करें. उस फ़ाइल के मौजूदा कॉन्टेंट को इनसे बदलें.

lib/main.dart

import 'package:flutter/material.dart';

import 'models/data.dart' as data;
import 'models/models.dart';
import 'widgets/email_list_view.dart';

void main() {
 
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
 
const MainApp({super.key});

 
@override
 
Widget build(BuildContext context) {
   
return MaterialApp(
     
theme: ThemeData.light(),
     
home: Feed(currentUser: data.user_0),
   
);
 
}
}

class Feed extends StatefulWidget {
 
const Feed({super.key, required this.currentUser});

 
final User currentUser;

 
@override
 
State<Feed> createState() => _FeedState();
}

class _FeedState extends State<Feed> {
 
late final _colorScheme = Theme.of(context).colorScheme;
 
late final _backgroundColor = Color.alphaBlend(
   
_colorScheme.primary.withAlpha(36),
   
_colorScheme.surface,
 
);

 
@override
 
Widget build(BuildContext context) {
   
return Scaffold(
     
body: Container(
       
color: _backgroundColor,
       
child: EmailListView(currentUser: widget.currentUser),
     
),
     
floatingActionButton: FloatingActionButton(
       
backgroundColor: _colorScheme.tertiaryContainer,
       
foregroundColor: _colorScheme.onTertiaryContainer,
       
onPressed: () {},
       
child: const Icon(Icons.add),
     
),
   
);
 
}
}

ऐप्लिकेशन को चलाकर देखें कि आपको किस तरह की सुविधाएं मिल रही हैं.

43f8d99b09e0f983.png

5. NavigationBar जोड़ना

पिछले चरण के आखिर में, स्टार्टर ऐप्लिकेशन में मैसेज की सूची थी, लेकिन उसमें और कुछ नहीं था. इस चरण में, दर्शकों का ध्यान खींचने के लिए NavigationBar जोड़ा जाता है. जब ऐप्लिकेशन, यूज़र इंटरफ़ेस (यूआई) स्केच से असली ऐप्लिकेशन में बदलता है, तो नेविगेशन बार में उपयोगकर्ता के लिए ऐप्लिकेशन के अलग-अलग सेक्शन उपलब्ध होते हैं.

NavigationBar का मतलब है कि नेविगेट करने के लिए डेस्टिनेशन मौजूद हैं. lib डायरेक्ट्री में destinations.dart नाम से नई फ़ाइल बनाएं और उसमें यह कोड डालें.

lib/destinations.dart

import 'package:flutter/material.dart';

class Destination {
 
const Destination(this.icon, this.label);
 
final IconData icon;
 
final String label;
}

const List<Destination> destinations = <Destination>[
 
Destination(Icons.inbox_rounded, 'Inbox'),
 
Destination(Icons.article_outlined, 'Articles'),
 
Destination(Icons.messenger_outline_rounded, 'Messages'),
 
Destination(Icons.group_outlined, 'Groups'),
];

इससे ऐप्लिकेशन को NavigationBar दिखाने के लिए चार डेस्टिनेशन मिलते हैं. इसके बाद, डेस्टिनेशन की इस सूची को lib/main.dart फ़ाइल में इस तरह जोड़ें:

lib/main.dart

import 'package:flutter/material.dart';

import 'destinations.dart';                    // Add this import
import 'models/data.dart' as data;
import 'models/models.dart';
import 'widgets/email_list_view.dart';

void main() {
 
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
 
const MainApp({super.key});

 
@override
 
Widget build(BuildContext context) {
   
return MaterialApp(
     
theme: ThemeData.light(),
     
home: Feed(currentUser: data.user_0),
   
);
 
}
}

class Feed extends StatefulWidget {
 
const Feed({super.key, required this.currentUser});

 
final User currentUser;

 
@override
 
State<Feed> createState() => _FeedState();
}

class _FeedState extends State<Feed> {
 
late final _colorScheme = Theme.of(context).colorScheme;
 
late final _backgroundColor = Color.alphaBlend(
   
_colorScheme.primary.withAlpha(36),
   
_colorScheme.surface,
 
);

 
int selectedIndex = 0;                       // Add this variable

 
@override
 
Widget build(BuildContext context) {
   
return Scaffold(
     
body: Container(
       
color: _backgroundColor,
       
child: EmailListView(
                                             
// Add from here...
         
selectedIndex: selectedIndex,
         
onSelected: (index) {
           
setState(() {
             
selectedIndex = index;
           
});
         
},
                                             
// ... to here.
         
currentUser: widget.currentUser,
       
),
     
),
     
floatingActionButton: FloatingActionButton(
       
backgroundColor: _colorScheme.tertiaryContainer,
       
foregroundColor: _colorScheme.onTertiaryContainer,
       
onPressed: () {},
       
child: const Icon(Icons.add),
     
),
                                                 
// Add from here...
     
bottomNavigationBar: NavigationBar(
       
elevation: 0,
       
backgroundColor: Colors.white,
       
destinations: destinations.map<NavigationDestination>((d) {
         
return NavigationDestination(icon: Icon(d.icon), label: d.label);
       
}).toList(),
       
selectedIndex: selectedIndex,
       
onDestinationSelected: (index) {
         
setState(() {
           
selectedIndex = index;
         
});
       
},
     
),
                                               
// ...to here.
   
);
 
}
}

हर डेस्टिनेशन के लिए अलग-अलग कॉन्टेंट तय करने के बजाय, NavigationBar में चुने गए डेस्टिनेशन को दिखाने के लिए, अलग-अलग मैसेज की स्थिति बदलें. यह सुविधा, उलटे क्रम में भी काम करती है: किसी मैसेज को चुनने पर, NavigationBar में उससे जुड़ा डेस्टिनेशन दिखता है. इन बदलावों की पुष्टि करने के लिए, ऐप्लिकेशन चलाएं:

9b3f7628fd32679c.png

यह छोटे कॉन्फ़िगरेशन में सही लगता है. हालांकि, विंडो को बड़ा करने या फ़ोन सिम्युलेटर को हॉरिज़ॉन्टल में घुमाने पर, यह थोड़ा अजीब दिखता है. इस गड़बड़ी को ठीक करने के लिए, ऐप्लिकेशन के ज़रूरत के मुताबिक चौड़े होने पर, स्क्रीन की बाईं ओर NavigationRail जोड़ें. इसकी जानकारी अगले चरण में दी गई है.

6. NavigationRail जोड़ना

इस चरण में, आपके ऐप्लिकेशन में NavigationRail जोड़ दिया जाता है. स्क्रीन साइज़ के हिसाब से, दो में से सिर्फ़ एक नेविगेशन विजेट दिखाया जा सकता है. इसका मतलब है कि आपको ज़रूरत पड़ने पर NavigationBar को छिपाना या दिखाना होगा. lib/widgets डायरेक्ट्री में, disappearing_bottom_navigation_bar.dart फ़ाइल बनाएं और यह कोड जोड़ें:

lib/widgets/disappearing_bottom_navigation_bar.dart

import 'package:flutter/material.dart';

import '../destinations.dart';

class DisappearingBottomNavigationBar extends StatelessWidget {
 
const DisappearingBottomNavigationBar({
   
super.key,
   
required this.selectedIndex,
   
this.onDestinationSelected,
 
});

 
final int selectedIndex;
 
final ValueChanged<int>? onDestinationSelected;

 
@override
 
Widget build(BuildContext context) {
   
return NavigationBar(
     
elevation: 0,
     
backgroundColor: Colors.white,
     
destinations: destinations.map<NavigationDestination>((d) {
       
return NavigationDestination(icon: Icon(d.icon), label: d.label);
     
}).toList(),
     
selectedIndex: selectedIndex,
     
onDestinationSelected: onDestinationSelected,
   
);
 
}
}

उसी डायरेक्ट्री में, disappearing_navigation_rail.dart नाम की एक और फ़ाइल जोड़ें. इसमें यह कोड डालें:

lib/widgets/disappearing_navigation_rail.dart

import 'package:flutter/material.dart';

import '../destinations.dart';

class DisappearingNavigationRail extends StatelessWidget {
 
const DisappearingNavigationRail({
   
super.key,
   
required this.backgroundColor,
   
required this.selectedIndex,
   
this.onDestinationSelected,
 
});

 
final Color backgroundColor;
 
final int selectedIndex;
 
final ValueChanged<int>? onDestinationSelected;

 
@override
 
Widget build(BuildContext context) {
   
final colorScheme = Theme.of(context).colorScheme;
   
return NavigationRail(
     
selectedIndex: selectedIndex,
     
backgroundColor: backgroundColor,
     
onDestinationSelected: onDestinationSelected,
     
leading: Column(
       
children: [
         
IconButton(onPressed: () {}, icon: const Icon(Icons.menu)),
         
const SizedBox(height: 8),
         
FloatingActionButton(
           
shape: const RoundedRectangleBorder(
             
borderRadius: BorderRadius.all(Radius.circular(15)),
           
),
           
backgroundColor: colorScheme.tertiaryContainer,
           
foregroundColor: colorScheme.onTertiaryContainer,
           
onPressed: () {},
           
child: const Icon(Icons.add),
         
),
       
],
     
),
     
groupAlignment: -0.85,
     
destinations: destinations.map((d) {
       
return NavigationRailDestination(
         
icon: Icon(d.icon),
         
label: Text(d.label),
       
);
     
}).toList(),
   
);
 
}
}

नेविगेशन आइडिओम को अपने विजेट में रीफ़ैक्टर करने के बाद, lib/main.dart फ़ाइल में कुछ बदलाव करने होंगे:

lib/main.dart

import 'package:flutter/material.dart';

// Remove the destination.dart import, it's not required
import 'models/data.dart' as data;
import 'models/models.dart';
import 'widgets/disappearing_bottom_navigation_bar.dart';  // Add import
import 'widgets/disappearing_navigation_rail.dart';        // Add import
import 'widgets/email_list_view.dart';

void main() {
 
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
 
const MainApp({super.key});

 
@override
 
Widget build(BuildContext context) {
   
return MaterialApp(
     
theme: ThemeData.light(),
     
home: Feed(currentUser: data.user_0),
   
);
 
}
}

class Feed extends StatefulWidget {
 
const Feed({super.key, required this.currentUser});

 
final User currentUser;

 
@override
 
State<Feed> createState() => _FeedState();
}

class _FeedState extends State<Feed> {
 
late final _colorScheme = Theme.of(context).colorScheme;
 
late final _backgroundColor = Color.alphaBlend(
   
_colorScheme.primary.withAlpha(36),
   
_colorScheme.surface,
 
);

 
int selectedIndex = 0;
                                                 
// Add from here...
 
bool wideScreen = false;

 
@override
 
void didChangeDependencies() {
   
super.didChangeDependencies();

   
final double width = MediaQuery.of(context).size.width;
   
wideScreen = width > 600;
 
}
                                                 
// ... to here.

 
@override
 
Widget build(BuildContext context) {
                                                 
// Modify from here...
   
return Scaffold(
     
body: Row(
       
children: [
         
if (wideScreen)
           
DisappearingNavigationRail(
             
selectedIndex: selectedIndex,
             
backgroundColor: _backgroundColor,
             
onDestinationSelected: (index) {
               
setState(() {
                 
selectedIndex = index;
               
});
             
},
           
),
         
Expanded(
           
child: Container(
             
color: _backgroundColor,
             
child: EmailListView(
               
selectedIndex: selectedIndex,
               
onSelected: (index) {
                 
setState(() {
                   
selectedIndex = index;
                 
});
               
},
               
currentUser: widget.currentUser,
             
),
           
),
         
),
       
],
     
),
     
floatingActionButton: wideScreen
         
? null
         
: FloatingActionButton(
             
backgroundColor: _colorScheme.tertiaryContainer,
             
foregroundColor: _colorScheme.onTertiaryContainer,
             
onPressed: () {},
             
child: const Icon(Icons.add),
           
),
     
bottomNavigationBar: wideScreen
         
? null
         
: DisappearingBottomNavigationBar(
             
selectedIndex: selectedIndex,
             
onDestinationSelected: (index) {
               
setState(() {
                 
selectedIndex = index;
               
});
             
},
           
),
   
);
                                                   
// ... to here.
 
}
}

main.dart फ़ाइल में किया गया पहला अहम बदलाव, wideScreen स्टेटस जोड़ना है. यह स्टेटस तब अपडेट होता है, जब उपयोगकर्ता डिसप्ले का साइज़ बदलता है. भले ही, वह ब्राउज़र विंडो का साइज़ बदल रहा हो या फ़ोन को घुमा रहा हो. अगला बदलाव, NavigationBar और FloatingActionButton में बदलाव करता है. यह इस बात पर निर्भर करता है कि ऐप्लिकेशन wideScreen मोड में है या नहीं. आखिर में, अगर स्क्रीन काफ़ी चौड़ी है, तो NavigationRail को बाईं ओर दिखाया जाता है. वेब या डेस्कटॉप पर ऐप्लिकेशन चलाएं और दो अलग-अलग लेआउट दिखाने के लिए, स्क्रीन का साइज़ बदलें.

दो अलग-अलग लेआउट होना अच्छा है. हालांकि, दोनों के बीच ट्रांज़िशन अच्छा नहीं है. बार को रेल से और रेल को बार से बदलने के लिए, ज़्यादा डाइनैमिक तरीके का इस्तेमाल किया जा सकता है. इससे इस ऐप्लिकेशन को बेहतर बनाया जा सकता है. अगले चरण में, आपको यह ऐनिमेशन जोड़ना होगा.

7. ट्रांज़िशन को ऐनिमेट करना

ऐनिमेशन वाला अनुभव बनाने के लिए, ऐनिमेशन की एक सीरीज़ तैयार की जाती है. इसमें हर कॉम्पोनेंट को सही तरीके से कोरियोग्राफ़ किया जाता है. इस ऐनिमेशन के लिए, आपको lib डायरेक्ट्री में animations.dart नाम की एक नई फ़ाइल बनानी होगी. इसमें आपको अपनी ज़रूरत के हिसाब से ऐनिमेशन कर्व डालने होंगे.

lib/animations.dart

import 'package:flutter/animation.dart';

class BarAnimation extends ReverseAnimation {
 
BarAnimation({required AnimationController parent})
   
: super(
       
CurvedAnimation(
         
parent: parent,
         
curve: const Interval(0, 1 / 5),
         
reverseCurve: const Interval(1 / 5, 4 / 5),
       
),
     
);
}

class OffsetAnimation extends CurvedAnimation {
 
OffsetAnimation({required super.parent})
   
: super(
       
curve: const Interval(
         
2 / 5,
         
3 / 5,
         
curve: Curves.easeInOutCubicEmphasized,
       
),
       
reverseCurve: Interval(
         
4 / 5,
         
1,
         
curve: Curves.easeInOutCubicEmphasized.flipped,
       
),
     
);
}

class RailAnimation extends CurvedAnimation {
 
RailAnimation({required super.parent})
   
: super(
       
curve: const Interval(0 / 5, 4 / 5),
       
reverseCurve: const Interval(3 / 5, 1),
     
);
}

class RailFabAnimation extends CurvedAnimation {
 
RailFabAnimation({required super.parent})
   
: super(curve: const Interval(3 / 5, 1));
}

class ScaleAnimation extends CurvedAnimation {
 
ScaleAnimation({required super.parent})
   
: super(
       
curve: const Interval(
         
3 / 5,
         
4 / 5,
         
curve: Curves.easeInOutCubicEmphasized,
       
),
       
reverseCurve: Interval(
         
3 / 5,
         
1,
         
curve: Curves.easeInOutCubicEmphasized.flipped,
       
),
     
);
}

class ShapeAnimation extends CurvedAnimation {
 
ShapeAnimation({required super.parent})
   
: super(
       
curve: const Interval(
         
2 / 5,
         
3 / 5,
         
curve: Curves.easeInOutCubicEmphasized,
       
),
     
);
}

class SizeAnimation extends CurvedAnimation {
 
SizeAnimation({required super.parent})
   
: super(
       
curve: const Interval(
         
0 / 5,
         
3 / 5,
         
curve: Curves.easeInOutCubicEmphasized,
       
),
       
reverseCurve: Interval(
         
2 / 5,
         
1,
         
curve: Curves.easeInOutCubicEmphasized.flipped,
       
),
     
);
}

इन कर्व को डेवलप करने के लिए, बार-बार बदलाव करना पड़ता है. Flutter के हॉट रीलोड की सुविधा की मदद से, ऐसा करना काफ़ी आसान हो जाता है. इन ऐनिमेशन का इस्तेमाल करने के लिए, आपको कुछ ट्रांज़िशन की ज़रूरत होगी. lib डायरेक्ट्री में transitions नाम की सबडायरेक्ट्री बनाएं और नीचे दिए गए कोड के साथ bottom_bar_transition.dart नाम की फ़ाइल जोड़ें:

lib/transitions/bottom_bar_transition.dart

import 'package:flutter/material.dart';
import '../animations.dart';

class BottomBarTransition extends StatefulWidget {
 
const BottomBarTransition({
   
super.key,
   
required this.animation,
   
required this.backgroundColor,
   
required this.child,
 
});

 
final Animation<double> animation;
 
final Color backgroundColor;
 
final Widget child;

 
@override
 
State<BottomBarTransition> createState() => _BottomBarTransition();
}

class _BottomBarTransition extends State<BottomBarTransition> {
 
late final Animation<Offset> offsetAnimation = Tween<Offset>(
   
begin: const Offset(0, 1),
   
end: Offset.zero,
 
).animate(OffsetAnimation(parent: widget.animation));

 
late final Animation<double> heightAnimation = Tween<double>(
   
begin: 0,
   
end: 1,
 
).animate(SizeAnimation(parent: widget.animation));

 
@override
 
Widget build(BuildContext context) {
   
return ClipRect(
     
child: DecoratedBox(
       
decoration: BoxDecoration(color: widget.backgroundColor),
       
child: Align(
         
alignment: Alignment.topLeft,
         
heightFactor: heightAnimation.value,
         
child: FractionalTranslation(
           
translation: offsetAnimation.value,
           
child: widget.child,
         
),
       
),
     
),
   
);
 
}
}

lib/transitions डायरेक्ट्री में nav_rail_transition.dart नाम की एक और फ़ाइल जोड़ें और यह कोड जोड़ें:

lib/transitions/nav_rail_transition.dart

import 'package:flutter/material.dart';
import '../animations.dart';

class NavRailTransition extends StatefulWidget {
 
const NavRailTransition({
   
super.key,
   
required this.animation,
   
required this.backgroundColor,
   
required this.child,
 
});

 
final Animation<double> animation;
 
final Widget child;
 
final Color backgroundColor;

 
@override
 
State<NavRailTransition> createState() => _NavRailTransitionState();
}

class _NavRailTransitionState extends State<NavRailTransition> {
 
// The animations are only rebuilt by this method when the text
 
// direction changes because this widget only depends on Directionality.
 
late final bool ltr = Directionality.of(context) == TextDirection.ltr;
 
late final Animation<Offset> offsetAnimation = Tween<Offset>(
   
begin: ltr ? const Offset(-1, 0) : const Offset(1, 0),
   
end: Offset.zero,
 
).animate(OffsetAnimation(parent: widget.animation));
 
late final Animation<double> widthAnimation = Tween<double>(
   
begin: 0,
   
end: 1,
 
).animate(SizeAnimation(parent: widget.animation));

 
@override
 
Widget build(BuildContext context) {
   
return ClipRect(
     
child: DecoratedBox(
       
decoration: BoxDecoration(color: widget.backgroundColor),
       
child: AnimatedBuilder(
         
animation: widthAnimation,
         
builder: (context, child) {
           
return Align(
             
alignment: Alignment.topLeft,
             
widthFactor: widthAnimation.value,
             
child: FractionalTranslation(
               
translation: offsetAnimation.value,
               
child: widget.child,
             
),
           
);
         
},
       
),
     
),
   
);
 
}
}

ये दो ट्रांज़िशन विजेट, नेविगेशन रेल और बार विजेट को रैप करते हैं, ताकि वे ऐनिमेशन के साथ दिखें और गायब हों. इन दो ट्रांज़िशन विजेट का इस्तेमाल करने के लिए, disappearing_bottom_navigation_bar.dart से शुरू होने वाले दो विजेट अपडेट करें:

lib/widgets/disappearing_bottom_navigation_bar.dart

import 'package:flutter/material.dart';

import '../animations.dart';                          // Add this import
import '../destinations.dart';
import '../transitions/bottom_bar_transition.dart';   // Add this import

class DisappearingBottomNavigationBar extends StatelessWidget {
 
const DisappearingBottomNavigationBar({
   
super.key,
   
required this.barAnimation,                       // Add this parameter
   
required this.selectedIndex,
   
this.onDestinationSelected,
 
});

 
final BarAnimation barAnimation;                   // Add this variable
 
final int selectedIndex;
 
final ValueChanged<int>? onDestinationSelected;

 
@override
 
Widget build(BuildContext context) {
                                                     
// Modify from here...
   
return BottomBarTransition(
     
animation: barAnimation,
     
backgroundColor: Colors.white,
     
child: NavigationBar(
       
elevation: 0,
       
backgroundColor: Colors.white,
       
destinations: destinations.map<NavigationDestination>((d) {
         
return NavigationDestination(icon: Icon(d.icon), label: d.label);
       
}).toList(),
       
selectedIndex: selectedIndex,
       
onDestinationSelected: onDestinationSelected,
     
),
   
);
                                                     
// ... to here.
 
}
}

पिछले बदलाव में, एक ऐनिमेशन जोड़ा गया है और ट्रांज़िशन जोड़ा गया है. इससे, यह कंट्रोल किया जा सकता है कि नेविगेशन बार कैसे दिखे और कैसे छिपे.

इसके बाद, disappearing_navigation_rail.dart में इस तरह बदलाव करें:

lib/widgets/disappearing_navigation_rail.dart

import 'package:flutter/material.dart';

import '../animations.dart';                          // Add this import
import '../destinations.dart';
import '../transitions/nav_rail_transition.dart';     // Add this import
import 'animated_floating_action_button.dart';        // Add this import

class DisappearingNavigationRail extends StatelessWidget {
 
const DisappearingNavigationRail({
   
super.key,
   
required this.railAnimation,                      // Add this parameter
   
required this.railFabAnimation,                   // Add this parameter
   
required this.backgroundColor,
   
required this.selectedIndex,
   
this.onDestinationSelected,
 
});

 
final RailAnimation railAnimation;                  // Add this variable
 
final RailFabAnimation railFabAnimation;            // Add this variable
 
final Color backgroundColor;
 
final int selectedIndex;
 
final ValueChanged<int>? onDestinationSelected;

 
@override
 
Widget build(BuildContext context) {
   
// Delete colorScheme
                                                     
// Modify from here ...
   
return NavRailTransition(
     
animation: railAnimation,
     
backgroundColor: backgroundColor,
     
child: NavigationRail(
       
selectedIndex: selectedIndex,
       
backgroundColor: backgroundColor,
       
onDestinationSelected: onDestinationSelected,
       
leading: Column(
         
children: [
           
IconButton(onPressed: () {}, icon: const Icon(Icons.menu)),
           
const SizedBox(height: 8),
           
AnimatedFloatingActionButton(
             
animation: railFabAnimation,
             
elevation: 0,
             
onPressed: () {},
             
child: const Icon(Icons.add),
           
),
         
],
       
),
       
groupAlignment: -0.85,
       
destinations: destinations.map((d) {
         
return NavigationRailDestination(
           
icon: Icon(d.icon),
           
label: Text(d.label),
         
);
       
}).toList(),
     
),
   
);
                                                     
// ... to here.
 
}
}

पिछले कोड को डालते समय, आपको FloatingActionButton नाम के ऐसे विजेट के बारे में गड़बड़ी की चेतावनियां मिली होंगी जिसकी जानकारी नहीं दी गई है. इसे ठीक करने के लिए, lib/widgets में animated_floating_action_button.dart नाम की फ़ाइल जोड़ें. इसके लिए, नीचे दिया गया कोड इस्तेमाल करें:

lib/widgets/animated_floating_action_button.dart

import 'dart:ui';

import 'package:flutter/material.dart';
import '../animations.dart';

class AnimatedFloatingActionButton extends StatefulWidget {
 
const AnimatedFloatingActionButton({
   
super.key,
   
required this.animation,
   
this.elevation,
   
this.onPressed,
   
this.child,
 
});

 
final Animation<double> animation;
 
final VoidCallback? onPressed;
 
final Widget? child;
 
final double? elevation;

 
@override
 
State<AnimatedFloatingActionButton> createState() =>
     
_AnimatedFloatingActionButton();
}

class _AnimatedFloatingActionButton
   
extends State<AnimatedFloatingActionButton> {
 
late final ColorScheme _colorScheme = Theme.of(context).colorScheme;
 
late final Animation<double> _scaleAnimation = ScaleAnimation(
   
parent: widget.animation,
 
);
 
late final Animation<double> _shapeAnimation = ShapeAnimation(
   
parent: widget.animation,
 
);

 
@override
 
Widget build(BuildContext context) {
   
return ScaleTransition(
     
scale: _scaleAnimation,
     
child: FloatingActionButton(
       
elevation: widget.elevation,
       
shape: RoundedRectangleBorder(
         
borderRadius: BorderRadius.all(
           
Radius.circular(lerpDouble(30, 15, _shapeAnimation.value)!),
         
),
       
),
       
backgroundColor: _colorScheme.tertiaryContainer,
       
foregroundColor: _colorScheme.onTertiaryContainer,
       
onPressed: widget.onPressed,
       
child: widget.child,
     
),
   
);
 
}
}

इन बदलावों को ऐप्लिकेशन में लागू करने के लिए, main.dart फ़ाइल को इस तरह अपडेट करें:

lib/main.dart

import 'package:flutter/material.dart';

import 'animations.dart';                               // Add this import
import 'models/data.dart' as data;
import 'models/models.dart';
import 'widgets/animated_floating_action_button.dart';  // Add this import
import 'widgets/disappearing_bottom_navigation_bar.dart';
import 'widgets/disappearing_navigation_rail.dart';
import 'widgets/email_list_view.dart';

void main() {
 
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
 
const MainApp({super.key});

 
@override
 
Widget build(BuildContext context) {
   
return MaterialApp(
     
theme: ThemeData.light(),
     
home: Feed(currentUser: data.user_0),
   
);
 
}
}

class Feed extends StatefulWidget {
 
const Feed({super.key, required this.currentUser});

 
final User currentUser;

 
@override
 
State<Feed> createState() => _FeedState();
}

class _FeedState extends State<Feed> with SingleTickerProviderStateMixin {
 
late final _colorScheme = Theme.of(context).colorScheme;
 
late final _backgroundColor = Color.alphaBlend(
   
_colorScheme.primary.withAlpha(36),
   
_colorScheme.surface,
 
);
                                                       
// Add from here...
 
late final _controller = AnimationController(
   
duration: const Duration(milliseconds: 1000),
   
reverseDuration: const Duration(milliseconds: 1250),
   
value: 0,
   
vsync: this,
 
);
 
late final _railAnimation = RailAnimation(parent: _controller);
 
late final _railFabAnimation = RailFabAnimation(parent: _controller);
 
late final _barAnimation = BarAnimation(parent: _controller);
                                                       
// ... to here.

 
int selectedIndex = 0;
 
// Remove wideScreen
 
bool controllerInitialized = false;                   // Add this variable

 
@override
 
void didChangeDependencies() {
   
super.didChangeDependencies();

   
final double width = MediaQuery.of(context).size.width;
   
// Remove wideScreen reference
                                                       
// Add from here ...
   
final AnimationStatus status = _controller.status;
   
if (width > 600) {
     
if (status != AnimationStatus.forward &&
         
status != AnimationStatus.completed) {
       
_controller.forward();
     
}
   
} else {
     
if (status != AnimationStatus.reverse &&
         
status != AnimationStatus.dismissed) {
       
_controller.reverse();
     
}
   
}
   
if (!controllerInitialized) {
     
controllerInitialized = true;
     
_controller.value = width > 600 ? 1 : 0;
   
}
                                                       
// ... to here.
 
}

                                                       
// Add from here ...
 
@override
 
void dispose() {
   
_controller.dispose();
   
super.dispose();
 
}
                                                       
// ... to here.

 
@override
 
Widget build(BuildContext context) {
                                                       
// Modify from here ...
   
return AnimatedBuilder(
     
animation: _controller,
     
builder: (context, _) {
       
return Scaffold(
         
body: Row(
           
children: [
             
DisappearingNavigationRail(
               
railAnimation: _railAnimation,
               
railFabAnimation: _railFabAnimation,
               
selectedIndex: selectedIndex,
               
backgroundColor: _backgroundColor,
               
onDestinationSelected: (index) {
                 
setState(() {
                   
selectedIndex = index;
                 
});
               
},
             
),
             
Expanded(
               
child: Container(
                 
color: _backgroundColor,
                 
child: EmailListView(
                   
selectedIndex: selectedIndex,
                   
onSelected: (index) {
                     
setState(() {
                       
selectedIndex = index;
                     
});
                   
},
                   
currentUser: widget.currentUser,
                 
),
               
),
             
),
           
],
         
),
         
floatingActionButton: AnimatedFloatingActionButton(
           
animation: _barAnimation,
           
onPressed: () {},
           
child: const Icon(Icons.add),
         
),
         
bottomNavigationBar: DisappearingBottomNavigationBar(
           
barAnimation: _barAnimation,
           
selectedIndex: selectedIndex,
           
onDestinationSelected: (index) {
             
setState(() {
               
selectedIndex = index;
             
});
           
},
         
),
       
);
     
},
   
);
                                                       
// ... to here.
 
}
}

ऐप्लिकेशन चलाएं. शुरुआत में, यह पहले जैसा ही दिखेगा. स्क्रीन का साइज़ बदलकर, नेविगेशन रेल और नेविगेशन बार के बीच यूज़र इंटरफ़ेस (यूआई) टॉगल करें. यह साइज़ और डाइमेंशन के हिसाब से दिखेगा. इन ट्रांज़िशन की मोशन अब आसानी से दिखेगी और मज़ेदार लगेगी. ऐनिमेशन कर्व में बदलाव करने के लिए, हॉट रीलोड का इस्तेमाल करें. इससे यह देखा जा सकता है कि ऐनिमेशन कर्व में बदलाव करने से, ऐप्लिकेशन का अनुभव कैसे बदलता है.

8. ज़्यादा जानकारी वाले व्यू में सूची जोड़ना

बोनस के तौर पर, मैसेजिंग ऐप्लिकेशन में ज़्यादा जानकारी वाले लेआउट में सूची दिखाने की सुविधा मिलती है. हालांकि, ऐसा सिर्फ़ तब किया जा सकता है, जब डिसप्ले का साइज़ ज़रूरत के मुताबिक हो. lib/widgets में reply_list_view.dart नाम की फ़ाइल जोड़कर शुरुआत करें और उसमें यह कोड डालें:

lib/widgets/reply_list_view.dart

import 'package:flutter/material.dart';

import '../models/data.dart' as data;
import 'email_widget.dart';

class ReplyListView extends StatelessWidget {
 
const ReplyListView({super.key});

 
@override
 
Widget build(BuildContext context) {
   
return Padding(
     
padding: const EdgeInsets.only(right: 8.0),
     
child: ListView(
       
children: [
         
const SizedBox(height: 8),
         
...List.generate(data.replies.length, (index) {
           
return Padding(
             
padding: const EdgeInsets.only(bottom: 8.0),
             
child: EmailWidget(
               
email: data.replies[index],
               
isPreview: false,
               
isThreaded: true,
               
showHeadline: index == 0,
             
),
           
);
         
}),
       
],
     
),
   
);
 
}
}

इसके बाद, lib/transitions में list_detail_transition.dart जोड़ें और उसे इस कोड से भरें:

lib/transitions/list_detail_transition.dart

import 'dart:ui';

import 'package:flutter/material.dart';
import '../animations.dart';

class ListDetailTransition extends StatefulWidget {
 
const ListDetailTransition({
   
super.key,
   
required this.animation,
   
required this.one,
   
required this.two,
 
});

 
final Animation<double> animation;
 
final Widget one;
 
final Widget two;

 
@override
 
State<ListDetailTransition> createState() => _ListDetailTransitionState();
}

class _ListDetailTransitionState extends State<ListDetailTransition> {
 
Animation<double> widthAnimation = const AlwaysStoppedAnimation(0);
 
late final Animation<double> sizeAnimation = SizeAnimation(
   
parent: widget.animation,
 
);
 
late final Animation<Offset> offsetAnimation = Tween<Offset>(
   
begin: const Offset(1, 0),
   
end: Offset.zero,
 
).animate(OffsetAnimation(parent: sizeAnimation));
 
double currentFlexFactor = 0;

 
@override
 
void didChangeDependencies() {
   
super.didChangeDependencies();

   
final double width = MediaQuery.of(context).size.width;
   
double nextFlexFactor = switch (width) {
     
>= 800 && < 1200 => lerpDouble(1000, 2000, (width - 800) / 400)!,
     
>= 1200 && < 1600 => lerpDouble(2000, 3000, (width - 1200) / 400)!,
     
>= 1600 => 3000,
     
_ => 1000,
   
};

   
if (nextFlexFactor == currentFlexFactor) {
     
return;
   
}

   
if (currentFlexFactor == 0) {
     
widthAnimation = Tween<double>(
       
begin: 0,
       
end: nextFlexFactor,
     
).animate(sizeAnimation);
   
} else {
     
final TweenSequence<double> sequence = TweenSequence([
       
if (sizeAnimation.value > 0) ...[
         
TweenSequenceItem(
           
tween: Tween(begin: 0, end: widthAnimation.value),
           
weight: sizeAnimation.value,
         
),
       
],
       
if (sizeAnimation.value < 1) ...[
         
TweenSequenceItem(
           
tween: Tween(begin: widthAnimation.value, end: nextFlexFactor),
           
weight: 1 - sizeAnimation.value,
         
),
       
],
     
]);

     
widthAnimation = sequence.animate(sizeAnimation);
   
}

   
currentFlexFactor = nextFlexFactor;
 
}

 
@override
 
Widget build(BuildContext context) {
   
return widthAnimation.value.toInt() == 0
       
? widget.one
       
: Row(
           
children: [
             
Flexible(flex: 1000, child: widget.one),
             
Flexible(
               
flex: widthAnimation.value.toInt(),
               
child: FractionalTranslation(
                 
translation: offsetAnimation.value,
                 
child: widget.two,
               
),
             
),
           
],
         
);
 
}
}

इस कॉन्टेंट को ऐप्लिकेशन में इंटिग्रेट करने के लिए, lib/main.dart को इस तरह अपडेट करें:

lib/main.dart

import 'package:flutter/material.dart';

import 'animations.dart';
import 'models/data.dart' as data;
import 'models/models.dart';
import 'transitions/list_detail_transition.dart';          // Add import
import 'widgets/animated_floating_action_button.dart';
import 'widgets/disappearing_bottom_navigation_bar.dart';
import 'widgets/disappearing_navigation_rail.dart';
import 'widgets/email_list_view.dart';
import 'widgets/reply_list_view.dart';                     // Add import

void main() {
 
runApp(const MainApp());
}

class MainApp extends StatelessWidget {
 
const MainApp({super.key});

 
@override
 
Widget build(BuildContext context) {
   
return MaterialApp(
     
theme: ThemeData.light(),
     
home: Feed(currentUser: data.user_0),
   
);
 
}
}

class Feed extends StatefulWidget {
 
const Feed({super.key, required this.currentUser});

 
final User currentUser;

 
@override
 
State<Feed> createState() => _FeedState();
}

class _FeedState extends State<Feed> with SingleTickerProviderStateMixin {
 
late final _colorScheme = Theme.of(context).colorScheme;
 
late final _backgroundColor = Color.alphaBlend(
   
_colorScheme.primary.withAlpha(36),
   
_colorScheme.surface,
 
);
 
late final _controller = AnimationController(
   
duration: const Duration(milliseconds: 1000),
   
reverseDuration: const Duration(milliseconds: 1250),
   
value: 0,
   
vsync: this,
 
);
 
late final _railAnimation = RailAnimation(parent: _controller);
 
late final _railFabAnimation = RailFabAnimation(parent: _controller);
 
late final _barAnimation = BarAnimation(parent: _controller);

 
int selectedIndex = 0;
 
bool controllerInitialized = false;

 
@override
 
void didChangeDependencies() {
   
super.didChangeDependencies();

   
final double width = MediaQuery.of(context).size.width;
   
final AnimationStatus status = _controller.status;
   
if (width > 600) {
     
if (status != AnimationStatus.forward &&
         
status != AnimationStatus.completed) {
       
_controller.forward();
     
}
   
} else {
     
if (status != AnimationStatus.reverse &&
         
status != AnimationStatus.dismissed) {
       
_controller.reverse();
     
}
   
}
   
if (!controllerInitialized) {
     
controllerInitialized = true;
     
_controller.value = width > 600 ? 1 : 0;
   
}
 
}

 
@override
 
void dispose() {
   
_controller.dispose();
   
super.dispose();
 
}

 
@override
 
Widget build(BuildContext context) {
   
return AnimatedBuilder(
     
animation: _controller,
     
builder: (context, _) {
       
return Scaffold(
         
body: Row(
           
children: [
             
DisappearingNavigationRail(
               
railAnimation: _railAnimation,
               
railFabAnimation: _railFabAnimation,
               
selectedIndex: selectedIndex,
               
backgroundColor: _backgroundColor,
               
onDestinationSelected: (index) {
                 
setState(() {
                   
selectedIndex = index;
                 
});
               
},
             
),
             
Expanded(
               
child: Container(
                 
color: _backgroundColor,
                                               
// Update from here ...
                 
child: ListDetailTransition(
                   
animation: _railAnimation,
                   
one: EmailListView(
                     
selectedIndex: selectedIndex,
                     
onSelected: (index) {
                       
setState(() {
                         
selectedIndex = index;
                       
});
                     
},
                     
currentUser: widget.currentUser,
                   
),
                   
two: const ReplyListView(),
                 
),
                                               
// ... to here.
               
),
             
),
           
],
         
),
         
floatingActionButton: AnimatedFloatingActionButton(
           
animation: _barAnimation,
           
onPressed: () {},
           
child: const Icon(Icons.add),
         
),
         
bottomNavigationBar: DisappearingBottomNavigationBar(
           
barAnimation: _barAnimation,
           
selectedIndex: selectedIndex,
           
onDestinationSelected: (index) {
             
setState(() {
               
selectedIndex = index;
             
});
           
},
         
),
       
);
     
},
   
);
 
}
}

ऐप्लिकेशन को चलाकर देखें कि सभी डेटा एक साथ दिख रहा है या नहीं. आपके पास किसी ऐप्लिकेशन में, Material 3 स्टाइल और अलग-अलग लेआउट के बीच ऐनिमेशन है. यह ऐप्लिकेशन, किसी असल ऐप्लिकेशन की तरह दिखता है. यह इस तरह दिखना चाहिए:

94b96bd9d65a829.png

9. बधाई हो

बधाई हो, आपने Material 3 Flutter का अपना पहला ऐप्लिकेशन बना लिया है!

कोड में इस कोडलैब के सभी चरणों की समीक्षा करने के लिए, Flutter कोडलैब के GitHub रिपॉज़िटरी में जाएं.

आगे क्या करना है?

इनमें से कुछ कोडलैब देखें...

इसके बारे में और पढ़ें