import 'package:flutter/material.dart';
void main() => runApp(TestPage());
// リスト 5.10
class TestPage extends StatelessWidget {
const TestPage({Key? key}) : super(key: key);
static const String _title = '状態に応じて表示切り替え';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: TestWidget(),
);
}
}
class TestWidget extends StatefulWidget {
const TestWidget({Key? key}) : super(key: key);
@override
State<TestWidget> createState() => _TestWidgetState();
}
class _TestWidgetState extends State<TestWidget> {
Future<String> _getFuture() async {
return await Future<String>.delayed(Duration(seconds: 5),
() => '読み込み完了');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('状態に応じて表示切り替え')),
body: Center(
child: FutureBuilder<String>(
future: _getFuture(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return CircularProgressIndicator();
}
if (snapshot.hasData) {
return Text(snapshot.data!);
}
return Text('データがない');
}
)
)
);
}
}