Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.3 kB
5
Indexable
Never
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: Home());
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  var _switchValue = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Switch(
            value: _switchValue,
            onChanged: (newValue) {
              setState(() => _switchValue = newValue);
            },
          ),
          FutureBuilder(
            future: _fetchData(),
            builder: (context, AsyncSnapshot snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                case ConnectionState.waiting:
                  return const CircularProgressIndicator();
                default:
                  return Center(child: Text(snapshot.data));
              }
            },
          ),
        ],
      ),
    );
  }

  Future<String> _fetchData() async {
    await Future.delayed(const Duration(seconds: 2));
    return 'Some Asynchronous Data!';
  }
}
Leave a Comment