No able to change from WillPopScope to PopScope

I am reading Chapter 7 of the Flutter Cookbook – Second Edition by Simone Alessandria, and I noticed that WillPopScope has been is now PopScope in Flutter. I do not know how to modify the code with PopScope.

Here is the code:

import 'package:flutter/material.dart'; // Importing necessary Flutter package for Material design components
import '../models/data_layer.dart'; // Importing custom data models
import '../plan_provider.dart'; // Importing custom provider for managing plan data

// The PlanScreen is a StatefulWidget that represents the main screen displaying the plan details
class PlanScreen extends StatefulWidget {
  final Plan plan; // A Plan object passed to the screen
  const PlanScreen(
      {super.key,
      required this.plan}); // Constructor to initialize the PlanScreen with the given Plan

  @override
  State<PlanScreen> createState() =>
      _PlanScreenState(); // Creating state for PlanScreen
}

// State class for PlanScreen
class _PlanScreenState extends State<PlanScreen> {
  late ScrollController
      scrollController; // ScrollController to manage scroll position and events
  Plan get plan =>
      widget.plan; // Getter to access the Plan object from the widget

  @override
  void initState() {
    // Initializing the ScrollController and adding a listener to hide the keyboard when scrolled
    scrollController = ScrollController()
      ..addListener(() {
        FocusScope.of(context)
            .requestFocus(FocusNode()); // Hide keyboard on scroll
      });

    super.initState(); // Calling the superclass initState method
  }

  @override
  Widget build(BuildContext context) {
    // Building the UI of the PlanScreen
    return WillPopScope(
      // Handling back navigation to save the plan before exiting
      onWillPop: () {
        final controller =
            PlanProvider.of(context); // Accessing the PlanProvider
        controller.savePlan(plan); // Saving the current plan
        return Future.value(true); // Indicating that the screen can be popped
      },
      child: Scaffold(
        // Main scaffold for the screen
        appBar: AppBar(title: const Text('Master Plan')), // App bar with title
        body: Column(children: [
          Expanded(child: _buildList()), // Building the list of tasks
          SafeArea(
              child: Text(plan
                  .completenessMessage)) // Displaying the plan completeness message
        ]),
        floatingActionButton:
            _buildAddTaskButton(), // Floating action button to add a new task
      ),
    );
  }

  // Method to build the FloatingActionButton
  Widget _buildAddTaskButton() {
    return FloatingActionButton(
      child: const Icon(Icons.add), // Icon for the button
      onPressed: () {
        final controller =
            PlanProvider.of(context); // Accessing the PlanProvider
        controller.createNewTask(plan); // Creating a new task
        setState(() {}); // Updating the state to reflect the new task
      },
    );
  }

  // Method to build the list of tasks using ListView.builder
  Widget _buildList() {
    return ListView.builder(
      controller: scrollController, // Assigning the ScrollController
      itemCount: plan.tasks.length, // Number of items in the list
      itemBuilder: (context, index) =>
          _buildTaskTile(plan.tasks[index]), // Building each task tile
    );
  }

  // Method to build a single task tile
  Widget _buildTaskTile(Task task) {
    return Dismissible(
      key: ValueKey(task), // Unique key for each task
      background:
          Container(color: Colors.red), // Background color when dismissing
      direction: DismissDirection.endToStart, // Dismiss direction
      onDismissed: (_) {
        final controller =
            PlanProvider.of(context); // Accessing the PlanProvider
        controller.deleteTask(plan, task); // Deleting the task
        setState(() {}); // Updating the state to reflect the task deletion
      },
      child: ListTile(
          leading: Checkbox(
              value: task.complete, // Checkbox to mark task as complete
              onChanged: (selected) {
                setState(() {
                  task.complete =
                      selected ?? false; // Updating task completion status
                });
              }),
          title: TextFormField(
            initialValue:
                task.description, // Initial value for the task description
            onFieldSubmitted: (text) {
              setState(() {
                task.description = text; // Updating task description
              });
            },
          )),
    );
  }

  @override
  void dispose() {
    // Disposing the ScrollController when the state is disposed
    scrollController.dispose();
    super.dispose(); // Calling the superclass dispose method
  }
}

The full code can be found on the author’s GitHub

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật