2017-05-11 68 views
2

我希望在用户选择单选按钮时提醒用户注意提交按钮,并且我想知道是否无论如何要实现这个功能。我查看了RaisedButton文档,但似乎没有任何属性闪烁或摇动按钮。例如,下面的代码最初没有单选按钮,所以按钮变灰,一旦在多个单选按钮中进行选择,submit RaisedButton onPressed属性值不再为空,而是替换为所需的操作;不过,我也想在那里,如果选择不同的单选按钮的情况下,有一些方法来添加一些运动(闪光/震动按钮)提交按钮,但不会改变onPressed财产将闪烁添加到按钮

new Radio<int>(
value: 1, 
groupValue: 0, 
onChanged: handleRadioValue 
) 

new RaisedButton(
child: const Text('SUBMIT') 
onPressed: selected 
) 

handleRadioValue(){ 
selected = groupValue == 0 ? null : submitButton(); 
//change Raised button to attract attention} 

回答

1

可以吸引眼球动画RaisedButtoncolor。当单选按钮选择发生变化时,此示例将提醒您注意RaisedButton,方法是将其颜色更改为当前主题的disabledColor并返回。

screenshot

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

void main() { 
    runApp(new MyApp()); 
} 

class MyApp extends StatelessWidget { 
    @override 
    Widget build(BuildContext context) { 
    return new MaterialApp(
     title: 'Flutter Demo', 
     home: new MyHomePage(), 
    ); 
    } 
} 

class MyHomePage extends StatefulWidget { 
    MyHomePage({Key key}) : super(key: key); 
    @override 
    createState() => new MyHomePageState(); 
} 

class MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin { 
    AnimationController _controller; 
    int _radioValue; 

    @override initState() { 
    _controller = new AnimationController(
     vsync: this, 
     duration: const Duration(milliseconds: 100), 
    )..addStatusListener((AnimationStatus status) { 
     if (status == AnimationStatus.completed) 
     _controller.reverse(); 
    }); 
    super.initState(); 
    } 

    void _handleRadioValue(int value) { 
    // Don't animate the first time that the radio value is set 
    if (_radioValue != null) 
     _controller.forward(); 
    setState(() { 
     _radioValue = value; 
    }); 
    } 

    @override 
    Widget build(BuildContext context) { 
    ThemeData theme = Theme.of(context); 
    return new Scaffold(
     body: new Center(
     child: new Column(
      mainAxisSize: MainAxisSize.max, 
      mainAxisAlignment: MainAxisAlignment.center, 
      children: [ 
      new Radio<int>(
       value: 0, 
       groupValue: _radioValue, 
       onChanged: _handleRadioValue 
      ), 
      new Radio<int>(
       value: 1, 
       groupValue: _radioValue, 
       onChanged: _handleRadioValue 
      ), 
      new AnimatedBuilder(
       child: const Text('SUBMIT'), 
       animation: _controller, 
       builder: (BuildContext context, Widget child) { 
       return new RaisedButton(
        color: new ColorTween(
        begin: theme.primaryColor, 
        end: theme.disabledColor, 
       ).animate(_controller).value, 
        colorBrightness: Brightness.dark, 
        child: child, 
        onPressed: _radioValue == null ? 
          null : 
          () => print('submit') 
       ); 
       } 
      ) 
      ], 
     ), 
    ), 
    ); 
    } 

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

,因为我想这只是工作。谢谢!要跟进,如果电台有一个孩子的文本,我想要替换上按提交(使用onPressed函数),我会在哪里放置函数?当调用_handleRadioValue但不激活提交按钮时,将函数放置在setState中会更改文本。 – driftavalii

+0

我不确定我是否理解你的问题而不看你的代码。你能用你的代码示例创建一个新的问题,我会回答它吗? –

+0

好的,发布了一个新问题[here](http://stackoverflow.com/questions/43944095/how-do-i-step-through-the-questions-list-using-the-submit-button) – driftavalii