【问题标题】:How to append Slider under the text within button when i press the raised Button?当我按下凸起的按钮时,如何在按钮内的文本下附加滑块?
【发布时间】:2021-02-28 09:23:28
【问题描述】:

在我的应用启动后,它会立即显示一个按钮。 按下此按钮后,我想在同一按钮内构建一个滑块来控制此声音的音量。

我需要的只是让这个滑块出现,而不是控制音量的机制。

我想要实现的是here..

我的按钮代码

void main() => runApp(Home());
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
  title: "RelaX",
  home: Container(
    child: Scaffold(
      appBar: AppBar(
        elevation: 20.0,
        backgroundColor: Color(0xFF001E3D),
        title: Text(
          'Relax',
          style: GoogleFonts.raleway(
            fontSize: 30.0,
            color: Color(0xFF55b9f3),
          ),
        ),
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          RaisedButton(
              child: Text("View Slider"),
              onPressed: () => print("view slider")),
        ],
      ),
    ),
  ),
);
}
} 

【问题讨论】:

    标签: flutter build widget flutter-animation statefulwidget


    【解决方案1】:

    您可以使用可见性小部件来设置滑块的可见性。请看下面的代码。我正在使用 Container 和 Inkwell 来实现与 RaisedButton 相同的效果。

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MaterialApp(
        home: MyApp(),
      ));
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      double _currentSliderValue = 0;
      bool _sliderVisible = false;
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text("Raised Button"),
          ),
          body: Center(
            child: ClipRRect(
              borderRadius: const BorderRadius.all(
                Radius.circular(20),
              ),
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.blue[200],
                  borderRadius: const BorderRadius.all(
                    Radius.circular(20),
                  ),
                ),
                child: Material(
                  elevation: 2,
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        _sliderVisible = !_sliderVisible;
                      });
                    },
                    child: Container(
                      width: 125.0,
                      height: 125.0,
                      child: Column(
                        children: [
                          const SizedBox(
                            height: 15,
                          ),
                          Icon(
                            Icons.nightlight_round,
                            color: Colors.indigo[900],
                            size: 48,
                          ),
                          const SizedBox(
                            height: 5,
                          ),
                          Visibility(
                            visible: _sliderVisible,
                            child: Slider(
                              value: _currentSliderValue,
                              min: 0,
                              max: 10,
                              onChanged: (double value) {
                                setState(() {
                                  _currentSliderValue = value;
                                });
                              },
                              activeColor: Colors.indigo[900],
                              inactiveColor: Colors.indigo[900],
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                  color: Colors.transparent,
                ),
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多