|
| 1 | +/// |
| 2 | +/// Created by NieBin on 2019/5/26 |
| 3 | +/// Github: https://github.com/nb312 |
| 4 | + |
| 5 | +/// |
| 6 | +import "package:flutter/material.dart"; |
| 7 | +import 'package:flutter_widgets/const/_const.dart'; |
| 8 | + |
| 9 | +class AssetsPage extends StatefulWidget { |
| 10 | + @override |
| 11 | + _AssetsState createState() => _AssetsState(); |
| 12 | +} |
| 13 | + |
| 14 | +class _AssetsState extends State<AssetsPage> { |
| 15 | + final _formKey = GlobalKey<FormState>(); |
| 16 | + final _fieldKey = GlobalKey<FormFieldState>(); |
| 17 | + var _value = "Hello world"; |
| 18 | + var _count = 0; |
| 19 | + |
| 20 | +// this.onWillPop, |
| 21 | +// this.onChanged, |
| 22 | + Future<bool> _willPop() async { |
| 23 | + return _formKey.currentState.validate(); |
| 24 | + } |
| 25 | + |
| 26 | + Widget _form() => Form( |
| 27 | + key: _formKey, |
| 28 | + autovalidate: true, |
| 29 | + onWillPop: _willPop, |
| 30 | + onChanged: () { |
| 31 | + print("onChange.value= ${_fieldKey.currentState.value}"); |
| 32 | + }, |
| 33 | + child: Column( |
| 34 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 35 | + children: <Widget>[ |
| 36 | + TextFormField( |
| 37 | + key: _fieldKey, |
| 38 | + validator: (value) { |
| 39 | + if (value.isEmpty) { |
| 40 | + return 'Please enter some text'; |
| 41 | + } |
| 42 | + if (value == "hello world") { |
| 43 | + return 'This is is hello world'; |
| 44 | + } |
| 45 | + return null; |
| 46 | + }, |
| 47 | + ), |
| 48 | + Padding( |
| 49 | + padding: const EdgeInsets.symmetric(vertical: 16.0), |
| 50 | + child: RaisedButton( |
| 51 | + onPressed: () { |
| 52 | + // Validate will return true if the form is valid, or false if |
| 53 | + // the form is invalid. |
| 54 | + if (_formKey.currentState.validate()) { |
| 55 | + // Process data. |
| 56 | + print("print you."); |
| 57 | + } |
| 58 | + }, |
| 59 | + child: Text('Submit'), |
| 60 | + ), |
| 61 | + ), |
| 62 | + ], |
| 63 | + ), |
| 64 | + ); |
| 65 | + |
| 66 | + // @required this.text, |
| 67 | + // this.textAlign = TextAlign.start, |
| 68 | + // this.textDirection, |
| 69 | + // this.softWrap = true, |
| 70 | + // this.overflow = TextOverflow.clip, |
| 71 | + // this.textScaleFactor = 1.0, |
| 72 | + // this.maxLines, |
| 73 | + // this.locale, |
| 74 | + // this.strutStyle, |
| 75 | + // this.textWidthBasis = TextWidthBasis.parent, |
| 76 | + // |
| 77 | + List<TextSpan> _spans() => [ |
| 78 | + TextSpan( |
| 79 | + style: TextStyle(color: RED_LIGHT), |
| 80 | + text: "Hello", |
| 81 | + ), |
| 82 | + TextSpan( |
| 83 | + style: TextStyle(color: BLUE_DEEP, fontSize: 20), |
| 84 | + text: "World", |
| 85 | + ), |
| 86 | + ]; |
| 87 | + |
| 88 | + Widget _rich() => Column( |
| 89 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 90 | + children: <Widget>[ |
| 91 | + RichText( |
| 92 | + text: TextSpan( |
| 93 | + style: TextStyle(), |
| 94 | +// text: "Hello", |
| 95 | + children: _spans()), |
| 96 | + overflow: TextOverflow.ellipsis, |
| 97 | + ), |
| 98 | + ], |
| 99 | + ); |
| 100 | + |
| 101 | +// @required this.builder, |
| 102 | +// this.onSaved, |
| 103 | +// this.validator, |
| 104 | +// this.initialValue, |
| 105 | +// this.autovalidate = false, |
| 106 | +// this.enabled = true, |
| 107 | +// this.enabled = false, |
| 108 | + Widget _formField() => FormField( |
| 109 | + builder: (FormFieldState s) { |
| 110 | + return FloatingActionButton( |
| 111 | + child: Text("Button $_value"), |
| 112 | + onPressed: () { |
| 113 | + _count++; |
| 114 | + s.setValue("${s.value}$_count"); |
| 115 | + s.save(); |
| 116 | + }, |
| 117 | + ); |
| 118 | + }, |
| 119 | + onSaved: (String v) { |
| 120 | + print("value: $v"); |
| 121 | + setState(() { |
| 122 | + _value = v; |
| 123 | + }); |
| 124 | + }, |
| 125 | + initialValue: _value, |
| 126 | + validator: (String v) { |
| 127 | + if (v.isEmpty) { |
| 128 | + return "Do not allow you to empty."; |
| 129 | + } |
| 130 | + }, |
| 131 | + autovalidate: true, |
| 132 | + enabled: true, |
| 133 | + ); |
| 134 | + |
| 135 | + Widget _defaultStyle() => DefaultTextStyle( |
| 136 | + style: TextStyle(color: BLUE), |
| 137 | + child: Column( |
| 138 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 139 | + children: <Widget>[ |
| 140 | + Text("Hello\ngive me some shine."), |
| 141 | + Text("World"), |
| 142 | + ], |
| 143 | + ), |
| 144 | + ); |
| 145 | + |
| 146 | + //TODO no done. |
| 147 | + Widget _rawImg(BuildContext context) => RawImage(); |
| 148 | + |
| 149 | + Widget _assetBundle() { |
| 150 | + DefaultAssetBundle.of(context).loadString(""); |
| 151 | + AssetImage( |
| 152 | + "", |
| 153 | + bundle: DefaultAssetBundle.of(context), |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + @override |
| 158 | + Widget build(BuildContext context) { |
| 159 | + return Scaffold( |
| 160 | + appBar: AppBar( |
| 161 | + title: Text(PageName.ASSET_PAGE), |
| 162 | + ), |
| 163 | + body: SingleChildScrollView( |
| 164 | + child: Column( |
| 165 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 166 | + children: <Widget>[ |
| 167 | + _form(), |
| 168 | + _rich(), |
| 169 | + _formField(), |
| 170 | + _defaultStyle(), |
| 171 | + ], |
| 172 | + ), |
| 173 | + ), |
| 174 | + ); |
| 175 | + } |
| 176 | +} |
0 commit comments