顫振無線電值在步進器中沒有改變 (Flutter Radio Value not Changing in Stepper)


問題描述

顫振無線電值在步進器中沒有改變 (Flutter Radio Value not Changing in Stepper)

問題是 _qualificationRadioGroupValue 值在 setState() 調用時更新,但在 build 方法調用其重置為其原始值之後我是初學者,所以不知道我做錯了什麼我只測試了 2 個單選按鈕,它在步進器之外工作,但在步進器中不起作用我不知道為什麼

這是我屏幕的代碼

import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  final title;

  HomePage({this.title});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<Step> _steps = <Step>[];
  int _currentStep = 0;
  int _isStepsCompleted = false;

  @override
  void initState() {
    super.initState();
    _steps.add(_lastQualificationStep());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Stack(
        children: <Widget>[
          Stepper(
            steps: _steps,
            currentStep: _currentStep,
            onStepTapped: (step) => onStepTapped(step),
            onStepContinue: onStepContinue,
            onStepCancel: onStepCancel,
          ),
        ],
      ),
    );
  }

  void onStepContinue() {
    _currentStep + 1 != _steps.length
        ? onStepTapped(_currentStep + 1)
        : setState(() => _isStepsCompleted = true);
  }

  void onStepTapped(int step) => setState(() => _currentStep = step);

  void onStepCancel() {
    if (_currentStep > 0) {
      onStepTapped(_currentStep ‑ 1);
    }
  }

  void setSelectRadioButton(int value, int groupValue) {
    print('Radio Value: $value');
    print('GroupValue before setState: $groupValue');
    setState(() {
      groupValue= value;
    });
    print('GroupValue after setState: $groupValue');
  }

  int _qualificationRadioGroupValue = 0;

  Step _lastQualificationStep() {
    List<RadioModel> qualifitcationList = [
      RadioModel(
          title: 'Pre‑Engineering',
          value: 0,
          groupValue: _qualificationRadioGroupValue),
      RadioModel(
          title: 'Pre‑Medical',
          value: 1,
          groupValue: _qualificationRadioGroupValue),
      RadioModel(
          title: 'ICS', value: 2, groupValue: _qualificationRadioGroupValue),
      RadioModel(
          title: 'Commerce',
          value: 3,
          groupValue: _qualificationRadioGroupValue),
    ];
    return Step(
      title: Text("What is your last qualification?"),
      isActive: _currentStep == 0,
      state: _currentStep == 0 ? StepState.editing : StepState.indexed,
      content: Column(
        children: _qualifitcationList
            .map(
              (qualification) => RadioListTile(
                title: Text(qualification.title),
                value: qualification.value,
                groupValue: qualification.groupValue,
                onChanged: (value) =>
                    setSelectRadioButton(value, qualification.groupValue),
              ),
            )
            .toList(),
      ),
    );
  }
}

class RadioModel {
  final title;
  final value;
  final groupValue;

  RadioModel({this.title, this.value, this.groupValue});
}


參考解法

方法 1:

You can copy paste run full code below Step 1: List<Step> _steps = <Step>[]; was built only once and then the same instance is reused every time. You can reference detail in https://github.com/flutter/flutter/issues/22033#issuecomment‑424228926 You need to use

 List<Step> get _steps => <Step>[_lastQualificationStep()];

Step 2: change groupValue to _qualificationRadioGroupValue

        children: qualifitcationList
            .map(
              (qualification) => RadioListTile(
            title: Text(qualification.title),
            value: qualification.value,
            groupValue: _qualificationRadioGroupValue,
            onChanged: (value) =>

...
setState(() {
  _qualificationRadioGroupValue = value;
}); 

working demo

enter image description here

full code

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class HomePage extends StatefulWidget {
  final title;

  HomePage({this.title});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<Step> get _steps => <Step>[_lastQualificationStep()];
  int _currentStep = 0;
  bool _isStepsCompleted = false;

  @override
  void initState() {
    super.initState();
    //_steps.add(_lastQualificationStep());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Stack(
        children: <Widget>[
          Stepper(
            steps: _steps,
            currentStep: _currentStep,
            onStepTapped: (step) => onStepTapped(step),
            onStepContinue: onStepContinue,
            onStepCancel: onStepCancel,
          ),
        ],
      ),
    );
  }

  void onStepContinue() {
    _currentStep + 1 != _steps.length
        ? onStepTapped(_currentStep + 1)
        : setState(() => _isStepsCompleted = true);
  }

  void onStepTapped(int step) => setState(() => _currentStep = step);

  void onStepCancel() {
    if (_currentStep > 0) {
      onStepTapped(_currentStep ‑ 1);
    }
  }

  void setSelectRadioButton(int value, int groupValue) {
    print('Radio Value: $value');
    print('GroupValue before setState: $groupValue');
    setState(() {
      _qualificationRadioGroupValue = value;
    });
    print('GroupValue after setState: $groupValue');
  }

  int _qualificationRadioGroupValue = 0;

  Step _lastQualificationStep() {
    List<RadioModel> qualifitcationList = [
      RadioModel(
          title: 'Pre‑Engineering',
          value: 0,
          groupValue: _qualificationRadioGroupValue),
      RadioModel(
          title: 'Pre‑Medical',
          value: 1,
          groupValue: _qualificationRadioGroupValue),
      RadioModel(
          title: 'ICS', value: 2, groupValue: _qualificationRadioGroupValue),
      RadioModel(
          title: 'Commerce',
          value: 3,
          groupValue: _qualificationRadioGroupValue),
    ];
    return Step(
      title: Text("What is your last qualification?"),
      isActive: _currentStep == 0,
      state: _currentStep == 0 ? StepState.editing : StepState.indexed,
      content: Column(
        children: qualifitcationList
            .map(
              (qualification) => RadioListTile(
            title: Text(qualification.title),
            value: qualification.value,
            groupValue: _qualificationRadioGroupValue,
            onChanged: (value) =>
                setSelectRadioButton(value, qualification.groupValue),
          ),
        )
            .toList(),
      ),
    );
  }
}

class RadioModel {
  final title;
  final value;
  final groupValue;

  RadioModel({this.title, this.value, this.groupValue});
}

(by Burhan Khanzadachunhunghan)

參考文件

  1. Flutter Radio Value not Changing in Stepper (CC BY‑SA 2.5/3.0/4.0)

#dart #Flutter #stepper #radio-button






相關問題

如何將數據傳遞給有狀態的小部件 (How to pass data to stateful widget)

顫振無線電值在步進器中沒有改變 (Flutter Radio Value not Changing in Stepper)

如何在顫動中製作響應式播放按鈕? (How to make a responsive play button in flutter?)

如何將數據數組轉換為在顫振/飛鏢中展開或折疊的小部件列表? (How to convert an array of data to a list of widgets with expand or fold in flutter/dart?)

Flutter - 迭代異步/未來列表 (Flutter - iterate over an asynchrous/future list)

使用顫振將圖像作為blob存儲在mysql數據庫中 (Storing image's as blob in mysql database with flutter)

如何在顫動中將視頻播放器浮動在 youtube 等所有頁面上? (How to float a video player over all pages like youtube in flutter?)

無法設置回調函數。錯誤說:“DropDown”必須有一個方法體,因為“ListLayout”不是抽象的 (Unable to set callback function . Error Says: 'DropDown' must have a method body because 'ListLayout' isn't abstract)

Flutter:如何在沒有評級動作的情況下實現評級欄 (Flutter : How to implement rating bar without rating action)

設置狀態內存洩漏 (set state memory leak)

Flutter 視頻播放器不接受自簽名 https 連接 (Flutter video player wont accept self signed https connection)

為什麼有時上下文被替換為下劃線或“_”? (Why sometimes context is being replace as underscore or "_"?)







留言討論