~cytrogen/fluent-reader-mobile

ref: 57e40efcfdbcbe7f4251c8ebea6d08b9eeb96b99 fluent-reader-mobile/lib/pages/settings/text_editor_page.dart -rw-r--r-- 2.8 KiB
57e40efc — Haoyuan Liu Create LICENSE 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import 'dart:async';

import 'package:fluent_reader_lite/components/list_tile_group.dart';
import 'package:fluent_reader_lite/generated/l10n.dart';
import 'package:fluent_reader_lite/utils/colors.dart';
import 'package:flutter/cupertino.dart';

class TextEditorPage extends StatefulWidget {
  final String title;
  final String saveText;
  final String initialValue;
  final Color navigationBarColor;
  final FutureOr<bool> Function(String) validate;
  final bool isPassword;

  TextEditorPage(
    this.title,
    this.validate,
    {
      this.navigationBarColor,
      this.saveText,
      this.initialValue: "",
      this.isPassword: false,
      Key key,
    })
    : super(key: key);

  @override
  _TextEditorPage createState() => _TextEditorPage();
}

class _TextEditorPage extends State<TextEditorPage> {
  TextEditingController _controller;
  bool _validating = false;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: widget.initialValue);
  }

  void _onSave() async {
    setState(() { _validating = true; });
    var trimmed = _controller.text.trim();
    var valid = await widget.validate(trimmed);
    if (!mounted) return;
    setState(() { _validating = false; });
    if (valid) {
      Navigator.of(context).pop(trimmed);
    } else {
      showCupertinoDialog(
        context: context,
        builder: (context) => CupertinoAlertDialog(
          title: Text(S.of(context).invalidValue),
          actions: [
            CupertinoDialogAction(
              child: Text(S.of(context).close),
              isDefaultAction: true,
              onPressed: () { Navigator.of(context).pop(); },
            ),
          ],
        ),
      );
    }
  }
  
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      backgroundColor: MyColors.background,
      navigationBar: CupertinoNavigationBar(
        middle: Text(widget.title),
        backgroundColor: widget.navigationBarColor,
        trailing: CupertinoButton(
          padding: EdgeInsets.zero,
          child: _validating
            ? CupertinoActivityIndicator()
            : Text(widget.saveText ?? S.of(context).save),
          onPressed: _validating ? null : _onSave,
        ),
      ),
      child: ListView(children: [
        ListTileGroup([
          CupertinoTextField(
            controller: _controller,
            decoration: null,
            padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12),
            clearButtonMode: OverlayVisibilityMode.editing,
            readOnly: _validating,
            autofocus: true,
            obscureText: widget.isPassword,
            keyboardType: widget.isPassword ? TextInputType.visiblePassword : null,
            onSubmitted: (v) { _onSave(); },
          ),
        ]),
      ]),
    );
  }
}