flutter_in_action_2nd icon indicating copy to clipboard operation
flutter_in_action_2nd copied to clipboard

3.4 单选开关和复选框 一节中代码示例中可能有歧义

Open Liu-code3 opened this issue 1 year ago • 0 comments

书中代码示例歧义的地方

image

问题描述

  • 如果直接把SwitchAndCheckBoxTestRoute放在mart.dart中的MateriaApp中的home属性的话,会报错。报错信息为: No Material widget found
  • 修改后如下
import 'package:flutter/material.dart';

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

  @override
  State<SwitchAndCheckBoxTestRoute> createState() =>
      _SwitchAndCheckBoxTestRouteState();
}

class _SwitchAndCheckBoxTestRouteState
    extends State<SwitchAndCheckBoxTestRoute> {
  bool _switchSelected = true; // 维护单选开关状态
  bool _checkboxSelected = true; // 维护复选框状态
  @override
  Widget build(BuildContext context) {
    return Scaffold( // 要引入 Material 小部件,您可以直接包含一个小部件,或者使用包含 Material 本身的小部件,例如 Card ,Dialog , Drawer 或 Scaffold 。
      body: Column(
        children: [
          Switch(
              value: _switchSelected,
              onChanged: (value) {
                setState(() {
                  _switchSelected = value;
                });
              }),
          Checkbox(
              value: _checkboxSelected,
              onChanged: (value) {
                setState(() {
                  _checkboxSelected = value!;
                });
              })
        ],
      ),
    );
  }
}

Liu-code3 avatar May 28 '24 02:05 Liu-code3