import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/list_tile.dart';

class MessageSettingPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PageState();
}

class _PageState extends State<MessageSettingPage> {
  bool _message = true;

  @override
  void initState() {
    super.initState();
    loadSetting();
  }

  void loadSetting() {
    SharedPreferences.getInstance().then((value) {
      _message = value.getBool("message_setting") ?? true;
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    const contentPadding = EdgeInsets.symmetric(horizontal: 0.0);
    return Scaffold(
        backgroundColor: Colors.white,
        body: CustomScrollView(
          slivers: <Widget>[
            buildSliverAppBar(context, "消息提醒"),
            SliverPadding(
              padding: const EdgeInsets.symmetric(horizontal: 12),
              sliver: SliverToBoxAdapter(
                child: Column(
                  children: <Widget>[
                    ListView(
                      padding: EdgeInsets.zero,
                      physics: NeverScrollableScrollPhysics(),
                      shrinkWrap: true,
                      children: divideTiles(
                        context: context,
                        includeLast: true,
                        tiles: [
                          ListTile(
                            title: Text('信息通知提醒'),
                            trailing: Switch(
                              value: _message,
                              onChanged: (bool value) {
                                SharedPreferences.getInstance().then((prefs) {
                                  _message = value;
                                  prefs.setBool("message_setting", value);
                                  setState(() {});
                                });
                              },
                            ),
                            contentPadding: contentPadding,
                          ),
                        ],
                      ).toList(),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ));
  }
}
