颤振放射值不变

时间:2019-03-02 09:46:05

标签: flutter

我正在尝试设置一组非常简单的单选按钮,这就是为什么它如此令人沮丧以至于它们无法正常工作。我尝试过在类似的班级中进行设置,并且成功了。我知道一个事实是setstate被调用,但是由于某种原因它没有更新单个单选按钮。这使我认为这是与状态有关的一些奇怪问题。

无论如何,所有帮助将不胜感激。我的主要课程是下面代码的第二部分。

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../bloc/thembloc.dart';
import './components/textfield.dart';

class SignUp extends StatefulWidget {
  @override
  _SignUpState createState() => _SignUpState();
}

class _SignUpState extends State<SignUp> {

  /*
  ui for signup
  includes multiple textfields.
  includes all of the information that we'll need
  to collect for an user to register an account. 
  todo: wrap everything in a form, encrypt it and send it to a private server.
  */
  @override
  Widget build(BuildContext context) {
    double _height = MediaQuery.of(context).size.height;
    double _width = MediaQuery.of(context).size.width;
    final double _margin = 16.0;
    final double _promptWidth = _width - 32.0;
    final double _promptHeight = _height - 32.0;
    final double _textFieldWidth = _promptWidth - 32.0;
    int subscriberValue;

    void switchSubscriber(int value) {
      setState(() {
       subscriberValue = value; 
      });
    }

    return BlocBuilder(
        bloc: BlocProvider.of<ThemeBloc>(context),
        builder: (context, ThemeState state) {
          return Scaffold(
            resizeToAvoidBottomInset: false,
            resizeToAvoidBottomPadding: false,
            appBar: AppBar(
              centerTitle: true,
              title: Text(
                "smartmoney",
                style: BlocProvider.of<ThemeBloc>(context).currentState.themedata.primaryTextTheme.display2,
              ),

              // appbar
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(8.0),
                      bottomRight: Radius.circular(8.0))),
              leading: IconButton(
                icon: Icon(
                  Icons.arrow_back,
                  color: BlocProvider.of<ThemeBloc>(context).currentState.themedata.buttonColor,
                ),
                onPressed: () {
                  print("going back");
                },
              ),
              backgroundColor: BlocProvider.of<ThemeBloc>(context).currentState.themedata.canvasColor,
            ),
            body: Container(
              height: _height,
              width: _width,
              color: BlocProvider.of<ThemeBloc>(context).currentState.themedata.backgroundColor,
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(top: _margin),
                    child: Container(
                      decoration: BoxDecoration(
                         borderRadius: BorderRadius.all(Radius.circular(8.0)),
                         color: BlocProvider.of<ThemeBloc>(context).currentState.themedata.canvasColor,
                         boxShadow: [
                           BoxShadow(
                               spreadRadius: 0.0,
                               color: Colors.black38,
                               blurRadius: 6.0,
                               offset: Offset(0.0, 3.0)),
                         ]),
                      width: _promptWidth,
                      height: _promptHeight - 48 - _margin,
                      child: Column(
                        children: <Widget>[
                          Text("Let's get started",
                          style: BlocProvider.of<ThemeBloc>(context).currentState.themedata.primaryTextTheme.display2,
                          ),
                          Text("Enter your information to create an account",
                          style: BlocProvider.of<ThemeBloc>(context).currentState.themedata.primaryTextTheme.subtitle,
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 8.0),
                            child: StyledTextField(
                              textFieldWidth: _textFieldWidth,
                              helperText: "First name",
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 8.0),
                            child: StyledTextField(
                              textFieldWidth: _textFieldWidth,
                              helperText: "Last name",
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 8.0),
                            child: StyledTextField(
                              textFieldWidth: _textFieldWidth,
                              helperText: "Email",
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 8.0),
                            child: StyledTextField(
                              textFieldWidth: _textFieldWidth,
                              helperText: "Password",
                            ),
                          ),
                          Padding(
                            padding: EdgeInsets.only(top: 8.0),
                            child: StyledTextField(
                              textFieldWidth: _textFieldWidth,
                              helperText: "Phone number",
                            ),
                          ),
                          Text("Subscriber type",
                          style: BlocProvider.of<ThemeBloc>(context).currentState.themedata.primaryTextTheme.display1,
                          ),
                          Radio(
                            groupValue: subscriberValue,
                            value: 0,
                            onChanged: (int value) => switchSubscriber(value),
                          ),
                          Radio(
                            groupValue: subscriberValue,
                            value: 1,
                            onChanged: (int value) => switchSubscriber(value),
                          )
                        ],
                      ),
                    ),
                  )
                ],
              ),
          ),
          );
        });
  }
}

import 'package:flutter/material.dart';
import './bloc/thembloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'ui/signin.dart';
import 'ui/signup.dart';
import 'ui/onboarding.dart';
import './ui/testing/whatthefuck.dart';

void main() {
  runApp(
    MaterialApp(
      home: SmartMoney(),
    )
    // SmartMoney()
  );
}

class SmartMoney extends StatefulWidget {
  @override
  _SmartMoneyState createState() => _SmartMoneyState();
}

class _SmartMoneyState extends State<SmartMoney> {

  final _themeBloc = ThemeBloc();

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        bloc: _themeBloc,
        child: SignUp(),
      );
  }
}

1 个答案:

答案 0 :(得分:1)

问题是因为您在subscriberValue方法中定义了变量build。您正在使用调用setState方法的build调用,并且在每次build的调用中,您都会丢失subscriberValue的值。我建议您始终使用可控制窗口小部件状态的变量作为类成员。

class _SignUpState extends State<SignUp> {

  // HAS TO BE CLASS MEMBER AND IT'S GOOD AN INITIAL VALUE TOO..
  int subscriberValue =1; // asuming that  1 is default radio button option 

  @override
  Widget build(BuildContext context) {
    //... some codes ...
    //int subscriberValue; REMOVE THIS LINE. YOU'RE LOSING THE VALUE IN EVERY setState call

    //You can define this method outside from build too.
    void switchSubscriber(int value) {
      setState(() {
        subscriberValue = value;
      });
    }
}