【问题标题】:alert dialog is not working in my flutter torch project警报对话框在我的颤振火炬项目中不起作用
【发布时间】:2019-05-30 07:47:46
【问题描述】:

AlertDialog 在我的项目中不起作用 代码链接已添加 https://github.com/alikthehacker/Flutter_Torch/issues/1#issue-448532152

【问题讨论】:

  • 您所需要的只是根目录下的MaterialApp 小部件,所以我回答的第一行就解决了您的问题。
  • 欢迎来到 SO!我建议您通过添加更多详细信息来更新您的问题。它可以帮助您更多地关注您的问题。

标签: flutter dart flutter-dependencies


【解决方案1】:

这是完整的工作代码。

void main() => runApp(MaterialApp(home: new MyApp())); // this is what you need

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _hasFlash = false;
  bool _isOn = false;
  double _intensity = 1.0;

  @override
  initState() {
    super.initState();
    initPlatformState();
  }

  initPlatformState() async {
    bool hasFlash = await Lamp.hasLamp;
    print("Device has flash ? $hasFlash");
    setState(() { _hasFlash = hasFlash; });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage("assets/images/chocolate_pic.png"), fit: BoxFit.cover)),
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          elevation:  10 ,
          backgroundColor: Colors.transparent,
          title: Text('Flutter Torch'),
          centerTitle: true,
          leading: IconButton(
              icon: Icon(
                Icons.info,
                color: Colors.white,
              ),
              onPressed: () {
                showinfo(context);
              }
          ),
        ),
        body: new Center(
          child: new Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ButtonTheme(
                  minWidth: 150.0,
                  height: 40.0,
                  child  : new RaisedButton(
                      child: Text('Flash is on: $_isOn'),
                      color: Theme.of(context).accentColor,
                      elevation: 10.0,
                      splashColor: Colors.blueGrey,
                      onPressed:  _turnFlash,
                      textColor: Colors.yellowAccent,
                      padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                      shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(30.0))
                  ),
                ),
                new Text('Device has flash: $_hasFlash',),
              ]),
        ),
      ),
    );
  }
  void showinfo(BuildContext context){
    var alertDialog = AlertDialog(
      title: Text('About Flutter Torch'),
      content: Text('app made by alik kumar ghosh'),
    );
    showDialog(
        context: context,
        builder: (BuildContext context){
          return alertDialog;
        }
    );
  }
  Future _turnFlash() async {
    _isOn ? Lamp.turnOff() : Lamp.turnOn(intensity: _intensity);
    var f = await Lamp.hasLamp;
    setState((){
      _hasFlash = f;
      _isOn = !_isOn;
    });
  }
}

【讨论】:

  • 这在这个脚手架不可达的情况下不起作用
  • 好的,你能把代码不工作的地方贴出来吗,因为我用了你的github完整代码,它100%按照你的代码工作。
猜你喜欢
  • 2021-04-12
  • 2020-12-28
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 2019-09-02
相关资源
最近更新 更多