9_flutter_SimpleDialog(对话框),FloatingActionButton(浮动按钮),Slider(滑动器)

500 阅读1分钟

1_SimpleDialog(对话框)


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

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

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

enum Answers{YES,NO,MAYBE}

class _State extends State<MyApp>{

  String _value = '';

  void _setValue(String value) => setState(() => _value = value);

  Future _askUser() async {
    var showDialog2 = showDialog(
        context: context,
        child:SimpleDialog(
          title: Text('开关电源'),
          children: <Widget>[
            SimpleDialogOption(child: Text('升压'),onPressed: (){Navigator.pop(context, Answers.YES);},),
            SimpleDialogOption(child: Text('升降压'),onPressed: (){Navigator.pop(context, Answers.NO);},),
            SimpleDialogOption(child: Text('降压'),onPressed: (){Navigator.pop(context, Answers.MAYBE);},),
          ],
        )
    );
    switch(
    await showDialog2
    ) {
      case Answers.YES:
        _setValue('升压');
        break;
      case Answers.NO:
        _setValue('升降压');
        break;
      case Answers.MAYBE:
        _setValue('降压');
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('SimpleDialog'),
        backgroundColor: Colors.red,
      ),
      body: Container(
        child: Center(
          child: Column(
            children: <Widget>[
              Text(_value),
              RaisedButton(onPressed: _askUser, child: Text('确定'),)
            ],
          ),
        ),
      ),
    );
  }
}

2_FloatingActionButton(浮动按钮)


import 'package:flutter/material.dart';

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

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

class _State extends State<MyApp>{

  String _value = '';
  void _onClicked() => setState(() => _value = DateTime.now().toString());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('FloatingActionButton'),
        backgroundColor: Colors.red,
      ),
      floatingActionButton: FloatingActionButton(
          onPressed: _onClicked,
          backgroundColor: Colors.red,
          mini: false,
          child: Icon(Icons.timer),
      ),
      body: Container(
        padding: EdgeInsets.all(32.0),
        child: Center(
          child: Column(
            children: <Widget>[
              Text(_value)
            ],
          ),
        ),
      ),
    );
  }
}

3_Slider(滑动器)


import 'package:flutter/material.dart';

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

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

class _State extends State<MyApp>{

  double _value = 0.0;
  void _setvalue(double value) => setState(() => _value = value);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Slider'),
      ),

      body: Container(
        padding: EdgeInsets.all(32.0),
        child: Center(
          child: Column(
            children: <Widget>[
              Text('值: ${(_value * 100).round()}'),
              Slider(value: _value, onChanged: _setvalue)
            ],
          ),
        ),
      ),
    );
  }
}