浏览代码

Tmp store.

/main
Yuncong Zhang 6 年前
当前提交
e6fb37e1
共有 3 个文件被更改,包括 1292 次插入298 次删除
  1. 586
      Samples/UIWidgetsGallery/demo/material/chip_demo.cs
  2. 1001
      Runtime/material/chip.cs
  3. 3
      Runtime/material/chip.cs.meta

586
Samples/UIWidgetsGallery/demo/material/chip_demo.cs


using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
const List<String> _defaultMaterials = <String>[
'poker',
'tortilla',
'fish and',
'micro',
'wood',
];
class ChipDemoUtils {
public static readonly List<string> _defaultMaterials = new List<string> {
"poker",
"tortilla",
"fish and",
"micro",
"wood"
};
const List<String> _defaultActions = <String>[
'flake',
'cut',
'fragment',
'splinter',
'nick',
'fry',
'solder',
'cash in',
'eat',
];
public static readonly List<string> _defaultActions = new List<string> {
"flake",
"cut",
"fragment",
"splinter",
"nick",
"fry",
"solder",
"cash in",
"eat"
};
const Map<String, String> _results = <String, String>{
'flake': 'flaking',
'cut': 'cutting',
'fragment': 'fragmenting',
'splinter': 'splintering',
'nick': 'nicking',
'fry': 'frying',
'solder': 'soldering',
'cash in': 'cashing in',
'eat': 'eating',
};
public static readonly Dictionary<string, string> _results = new Dictionary<string, string> {
{"flake", "flaking"},
{"cut", "cutting"},
{"fragment", "fragmenting"},
{"splinter", "splintering"},
{"nick", "nicking"},
{"fry", "frying"},
{"solder", "soldering"},
{"cash in", "cashing in"},
{"eat", "eating"}
};
public static readonly List<string> _defaultTools = new List<string> {
"hammer",
"chisel",
"fryer",
"fabricator",
"customer"
};
public static readonly Dictionary<string, string> _avatars = new Dictionary<string, string> {
{"hammer", "people/square/ali.png"},
{"chisel", "people/square/sandra.png"},
{"fryer", "people/square/trevor.png"},
{"fabricator", "people/square/stella.png"},
{"customer", "people/square/peter.png"}
};
const List<String> _defaultTools = <String>[
'hammer',
'chisel',
'fryer',
'fabricator',
'customer',
];
public static readonly Dictionary<string, HashSet<string>> _toolActions =
new Dictionary<string, HashSet<string>> {
{"hammer", new HashSet<string>(new List<string> {"flake", "fragment", "splinter"})},
{"chisel", new HashSet<string>(new List<string> {"flake", "nick", "splinter"})},
{"fryer", new HashSet<string>(new List<string> {"fry"})},
{"fabricator", new HashSet<string>(new List<string> {"solder"})},
{"customer", new HashSet<string>(new List<string> {"cash in", "eat"})}
};
const Map<String, String> _avatars = <String, String>{
'hammer': 'people/square/ali.png',
'chisel': 'people/square/sandra.png',
'fryer': 'people/square/trevor.png',
'fabricator': 'people/square/stella.png',
'customer': 'people/square/peter.png',
};
public static readonly Dictionary<string, HashSet<string>> _materialActions =
new Dictionary<string, HashSet<string>> {
{"poker", new HashSet<string>(new List<string> {"cash in"})},
{"tortilla", new HashSet<string>(new List<string> {"fry", "eat"})},
{"fish and", new HashSet<string>(new List<string> {"fry", "eat"})},
{"micro", new HashSet<string>(new List<string> {"solder", "fragment"})},
{"wood", new HashSet<string>(new List<string> {"flake", "cut", "splinter", "nick"})}
};
}
const Map<String, Set<String>> _toolActions = <String, Set<String>>{
'hammer': <String>{'flake', 'fragment', 'splinter'},
'chisel': <String>{'flake', 'nick', 'splinter'},
'fryer': <String>{'fry'},
'fabricator': <String>{'solder'},
'customer': <String>{'cash in', 'eat'},
};
const Map<String, Set<String>> _materialActions = <String, Set<String>>{
'poker': <String>{'cash in'},
'tortilla': <String>{'fry', 'eat'},
'fish and': <String>{'fry', 'eat'},
'micro': <String>{'solder', 'fragment'},
'wood': <String>{'flake', 'cut', 'splinter', 'nick'},
};
class _ChipsTile : StatelessWidget {
public _ChipsTile(
Key key = null,
string label = null,
List<Widget> children = null
) : base(key: key) {
this.label = label;
this.children = children;
}
class _ChipsTile extends StatelessWidget {
const _ChipsTile({
Key key,
this.label,
this.children,
}) : super(key: key);
public readonly string label;
public readonly List<Widget> children;
final String label;
final List<Widget> children;
public override Widget build(BuildContext context) {
List<Widget> cardChildren = new List<Widget> {
new Container(
padding: EdgeInsets.only(top: 16.0f, bottom: 4.0f),
alignment: Alignment.center,
child: new Text(this.label, textAlign: TextAlign.left)
)
};
if (this.children.isNotEmpty()) {
cardChildren.Add(new Wrap(
children: this.children.Select<Widget, Widget>((Widget chip) => {
return new Padding(
padding: EdgeInsets.all(2.0f),
child: chip
);
}).ToList()));
}
else {
TextStyle textStyle = Theme.of(context).textTheme.caption.copyWith(fontStyle: FontStyle.italic);
cardChildren.Add(
new Container(
alignment: Alignment.center,
constraints: new BoxConstraints(minWidth: 48.0f, minHeight: 48.0f),
padding: EdgeInsets.all(8.0f),
child: new Text("None", style: textStyle)
)
);
}
// Wraps a list of chips into a ListTile for display as a section in the demo.
@override
Widget build(BuildContext context) {
final List<Widget> cardChildren = <Widget>[
Container(
padding: const EdgeInsets.only(top: 16.0, bottom: 4.0),
alignment: Alignment.center,
child: Text(label, textAlign: TextAlign.start),
),
];
if (children.isNotEmpty) {
cardChildren.add(Wrap(
children: children.map<Widget>((Widget chip) {
return Padding(
padding: const EdgeInsets.all(2.0),
child: chip,
);
}).toList()));
} else {
final TextStyle textStyle = Theme.of(context).textTheme.caption.copyWith(fontStyle: FontStyle.italic);
cardChildren.add(
Semantics(
container: true,
child: Container(
alignment: Alignment.center,
constraints: const BoxConstraints(minWidth: 48.0, minHeight: 48.0),
padding: const EdgeInsets.all(8.0),
child: Text('None', style: textStyle),
),
));
return new Card(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: cardChildren
)
);
}
return Card(
semanticContainer: false,
child: Column(
mainAxisSize: MainAxisSize.min,
children: cardChildren,
),
);
}
}
public class ChipDemo : StatefulWidget {
const string routeName = "/material/chip";
class ChipDemo extends StatefulWidget {
static const String routeName = '/material/chip';
public override State createState() {
return new _ChipDemoState();
}
}
@override
_ChipDemoState createState() => _ChipDemoState();
}
class _ChipDemoState : State<ChipDemo> {
public _ChipDemoState() {
this._reset();
}
class _ChipDemoState extends State<ChipDemo> {
_ChipDemoState() {
_reset();
}
HashSet<string> _materials = new HashSet<string>(new List<string> { });
string _selectedMaterial = "";
string _selectedAction = "";
HashSet<string> _tools = new HashSet<string>(new List<string> { });
HashSet<string> _selectedTools = new HashSet<string>(new List<string> { });
HashSet<string> _actions = new HashSet<string>(new List<string> { });
bool _showShapeBorder = false;
final Set<String> _materials = <String>{};
String _selectedMaterial = '';
String _selectedAction = '';
final Set<String> _tools = <String>{};
final Set<String> _selectedTools = <String>{};
final Set<String> _actions = <String>{};
bool _showShapeBorder = false;
void _reset() {
this._materials.Clear();
this._materials.UnionWith(ChipDemoUtils._defaultMaterials);
this._actions.Clear();
this._actions.UnionWith(ChipDemoUtils._defaultActions);
this._tools.Clear();
this._tools.UnionWith(ChipDemoUtils._defaultTools);
this._selectedMaterial = "";
this._selectedAction = "";
this._selectedTools.Clear();
}
// Initialize members with the default data.
void _reset() {
_materials.clear();
_materials.addAll(_defaultMaterials);
_actions.clear();
_actions.addAll(_defaultActions);
_tools.clear();
_tools.addAll(_defaultTools);
_selectedMaterial = '';
_selectedAction = '';
_selectedTools.clear();
}
void _removeMaterial(string name) {
this._materials.Remove(name);
if (this._selectedMaterial == name) {
this._selectedMaterial = "";
}
}
void _removeMaterial(String name) {
_materials.remove(name);
if (_selectedMaterial == name) {
_selectedMaterial = '';
}
}
void _removeTool(string name) {
this._tools.Remove(name);
this._selectedTools.Remove(name);
}
void _removeTool(String name) {
_tools.remove(name);
_selectedTools.remove(name);
}
string _capitalize(string name) {
D.assert(name != null && name.isNotEmpty());
return name.Substring(0, 1).ToUpper() + name.Substring(1);
}
String _capitalize(String name) {
assert(name != null && name.isNotEmpty);
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
Color _nameToColor(string name) {
D.assert(name.Length > 1);
int hash = name.GetHashCode() & 0xffff;
float hue = (360.0f * hash / (1 << 15)) % 360.0f;
return HSVColor.fromAHSV(1.0f, hue, 0.4f, 0.90f).toColor();
}
// This converts a String to a unique color, based on the hash value of the
// String object. It takes the bottom 16 bits of the hash, and uses that to
// pick a hue for an HSV color, and then creates the color (with a preset
// saturation and value). This means that any unique strings will also have
// unique colors, but they'll all be readable, since they have the same
// saturation and value.
Color _nameToColor(String name) {
assert(name.length > 1);
final int hash = name.hashCode & 0xffff;
final double hue = (360.0 * hash / (1 << 15)) % 360.0;
return HSVColor.fromAHSV(1.0, hue, 0.4, 0.90).toColor();
}
AssetImage _nameToAvatar(string name) {
D.assert(ChipDemoUtils._avatars.ContainsKey(name));
return new AssetImage(
ChipDemoUtils._avatars[name]
);
}
AssetImage _nameToAvatar(String name) {
assert(_avatars.containsKey(name));
return AssetImage(
_avatars[name],
package: 'flutter_gallery_assets',
);
}
string _createResult() {
if (this._selectedAction.isEmpty()) {
return "";
}
String _createResult() {
if (_selectedAction.isEmpty) {
return '';
}
return _capitalize(_results[_selectedAction]) + '!';
}
return this._capitalize(ChipDemoUtils._results[this._selectedAction]) + "!";
}
@override
Widget build(BuildContext context) {
final List<Widget> chips = _materials.map<Widget>((String name) {
return Chip(
key: ValueKey<String>(name),
backgroundColor: _nameToColor(name),
label: Text(_capitalize(name)),
onDeleted: () {
setState(() {
_removeMaterial(name);
});
},
);
}).toList();
public override Widget build(BuildContext context) {
List<Widget> chips = this._materials.Select<string, Widget>((string name) => {
return new Chip(
key: Key.key(name),
backgroundColor: this._nameToColor(name),
label: new Text(this._capitalize(name)),
onDeleted: () => { this.setState(() => { this._removeMaterial(name); }); }
);
}).ToList();
final List<Widget> inputChips = _tools.map<Widget>((String name) {
return InputChip(
key: ValueKey<String>(name),
avatar: CircleAvatar(
backgroundImage: _nameToAvatar(name),
),
label: Text(_capitalize(name)),
onDeleted: () {
setState(() {
_removeTool(name);
});
});
}).toList();
List<Widget> inputChips = this._tools.Select<string, Widget>((string name) => {
return new InputChip(
key: ValueKey<string>.key(name),
avatar: CircleAvatar(
backgroundImage: this._nameToAvatar(name)
),
label: new Text(this._capitalize(name)),
onDeleted: () => { this.setState(() => { this._removeTool(name); }); });
}).ToList();
final List<Widget> choiceChips = _materials.map<Widget>((String name) {
return ChoiceChip(
key: ValueKey<String>(name),
backgroundColor: _nameToColor(name),
label: Text(_capitalize(name)),
selected: _selectedMaterial == name,
onSelected: (bool value) {
setState(() {
_selectedMaterial = value ? name : '';
});
},
);
}).toList();
List<Widget> choiceChips = this._materials.Select<string, Widget>((string name) => {
return new ChoiceChip(
key: ValueKey<string>.key(name),
backgroundColor: this._nameToColor(name),
label: new Text(this._capitalize(name)),
selected: this._selectedMaterial == name,
onSelected: (bool value) => {
this.setState(() => { this._selectedMaterial = value ? name : ""; });
}
);
}).ToList();
final List<Widget> filterChips = _defaultTools.map<Widget>((String name) {
return FilterChip(
key: ValueKey<String>(name),
label: Text(_capitalize(name)),
selected: _tools.contains(name) ? _selectedTools.contains(name) : false,
onSelected: !_tools.contains(name)
? null
: (bool value) {
setState(() {
if (!value) {
_selectedTools.remove(name);
} else {
_selectedTools.add(name);
}
});
},
);
}).toList();
List<Widget> filterChips = ChipDemoUtils._defaultTools.Select<string, Widget>((string name) => {
return new FilterChip(
key: ValueKey<string>.key(name),
label: new Text(this._capitalize(name)),
selected: this._tools.Contains(name) ? this._selectedTools.Contains(name) : false,
onSelected: !this._tools.Contains(name)
? (ValueChanged<bool>) null
: (bool value) => {
this.setState(() => {
if (!value) {
this._selectedTools.Remove(name);
}
else {
this._selectedTools.Add(name);
}
});
}
);
}).ToList();
Set<String> allowedActions = <String>{};
if (_selectedMaterial != null && _selectedMaterial.isNotEmpty) {
for (String tool in _selectedTools) {
allowedActions.addAll(_toolActions[tool]);
}
allowedActions = allowedActions.intersection(_materialActions[_selectedMaterial]);
}
HashSet<string> allowedActions = new HashSet<string>(new List<string> { });
if (this._selectedMaterial != null && this._selectedMaterial.isNotEmpty()) {
foreach (string tool in this._selectedTools) {
allowedActions.UnionWith(ChipDemoUtils._toolActions[tool]);
}
final List<Widget> actionChips = allowedActions.map<Widget>((String name) {
return ActionChip(
label: Text(_capitalize(name)),
onPressed: () {
setState(() {
_selectedAction = name;
});
},
);
}).toList();
allowedActions =
new HashSet<string>(
allowedActions.Intersect(ChipDemoUtils._materialActions[this._selectedMaterial]));
}
final ThemeData theme = Theme.of(context);
final List<Widget> tiles = <Widget>[
const SizedBox(height: 8.0, width: 0.0),
_ChipsTile(label: 'Available Materials (Chip)', children: chips),
_ChipsTile(label: 'Available Tools (InputChip)', children: inputChips),
_ChipsTile(label: 'Choose a Material (ChoiceChip)', children: choiceChips),
_ChipsTile(label: 'Choose Tools (FilterChip)', children: filterChips),
_ChipsTile(label: 'Perform Allowed Action (ActionChip)', children: actionChips),
const Divider(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Text(
_createResult(),
style: theme.textTheme.title,
),
),
),
];
List<Widget> actionChips = allowedActions.Select<string, Widget>((string name) => {
return new ActionChip(
label: new Text(this._capitalize(name)),
onPressed: () => { this.setState(() => { this._selectedAction = name; }); }
);
}).ToList();
return Scaffold(
appBar: AppBar(
title: const Text('Chips'),
actions: <Widget>[
MaterialDemoDocumentationButton(ChipDemo.routeName),
IconButton(
onPressed: () {
setState(() {
_showShapeBorder = !_showShapeBorder;
});
},
icon: const Icon(Icons.vignette, semanticLabel: 'Update border shape'),
),
],
),
body: ChipTheme(
data: _showShapeBorder
? theme.chipTheme.copyWith(
shape: BeveledRectangleBorder(
side: const BorderSide(width: 0.66, style: BorderStyle.solid, color: Colors.grey),
borderRadius: BorderRadius.circular(10.0),
))
: theme.chipTheme,
child: ListView(children: tiles),
),
floatingActionButton: FloatingActionButton(
onPressed: () => setState(_reset),
child: const Icon(Icons.refresh, semanticLabel: 'Reset chips'),
),
);
}
}
ThemeData theme = Theme.of(context);
List<Widget> tiles = new List<Widget> {
new SizedBox(height: 8.0f, width: 0.0f),
new _ChipsTile(label: "Available Materials (Chip)", children: chips),
new _ChipsTile(label: "Available Tools (InputChip)", children: inputChips),
new _ChipsTile(label: "Choose a Material (ChoiceChip)", children: choiceChips),
new _ChipsTile(label: "Choose Tools (FilterChip)", children: filterChips),
new _ChipsTile(label: "Perform Allowed Action (ActionChip)", children: actionChips),
new Divider(),
new Padding(
padding: EdgeInsets.all(8.0f),
child: new Center(
child: new Text(this._createResult(),
style: theme.textTheme.title
)
)
)
};
return new Scaffold(
appBar: new AppBar(
title: new Text("Chips"),
actions: new List<Widget> {
new MaterialDemoDocumentationButton(ChipDemo.routeName),
new IconButton(
onPressed: () => {
this.setState(() => { this._showShapeBorder = !this._showShapeBorder; });
},
icon: new Icon(Icons.vignette)
)
}
),
body: new ChipTheme(
data: this._showShapeBorder
? theme.chipTheme.copyWith(
shape: new BeveledRectangleBorder(
side: new BorderSide(width: 0.66f, style: BorderStyle.solid, color: Colors.grey),
borderRadius: BorderRadius.circular(10.0f)
))
: theme.chipTheme,
child: new ListView(children: tiles)
),
floatingActionButton: new FloatingActionButton(
onPressed: () => this.setState(this._reset),
child: new Icon(Icons.refresh)
)
);
}
}
}

1001
Runtime/material/chip.cs
文件差异内容过多而无法显示
查看文件

3
Runtime/material/chip.cs.meta


fileFormatVersion: 2
guid: d4ca6b86209d4615b9aba98d6d112673
timeCreated: 1555477997
正在加载...
取消
保存