2017-05-11 102 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