我正在构建一个扑动的应用程序,我正在使用ExpansionTile的示例代码:
import 'package:flutter/material.dart';
class ExpansionTileSample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('ExpansionTile'),
),
body: new ListView.builder(
itemBuilder: (BuildContext context, int index) =>
new EntryItem(data[index]),
itemCount: data.length,
),
),
);
}
}
// One entry in the multilevel list displayed by this app.
class Entry {
Entry(this.title, [this.children = const <Entry>[]]);
final String title;
final List<Entry> children;
}
// The entire multilevel list displayed by this app.
final List<Entry> data = <Entry>[
new Entry(
'Chapter A',
<Entry>[
new Entry(
'Section A0',
<Entry>[
new Entry('Items:\n\nItem A0.1\nItem A0.1.1'),
new Entry('Item A0.2'),
new Entry('Item A0.3'),
],
),
new Entry('Section A1'),
new Entry('Section A2'),
],
),
];
// Displays one Entry. If the entry has children then it's displayed
// with an ExpansionTile.
class EntryItem extends StatelessWidget {
const EntryItem(this.entry);
final Entry entry;
Widget _buildTiles(Entry root) {
if (root.children.isEmpty) return new ListTile(
title: new Text(root.title));
return new ExpansionTile(
key: new PageStorageKey<Entry>(root),
title: new Text(root.title),
children: root.children.map(_buildTiles).toList(),
);
}
@override
Widget build(BuildContext context) {
return _buildTiles(entry);
}
}
https://flutter.io/catalog/samples/expansion-tile-sample/
我正在寻找一种方法来添加粗体并更改条目的文本颜色,例如,将文本:'Items:\n\nItem A0.1\nItem A0.1.1'
更改为Items:
,红色为Item A0.1
,绿色为Item A0.1.1
。
我使用TextSpan类,但是我无法在Entry
类中使它工作。
我认为你正在寻找它的TextStyle小部件,它在Text小部件中使用,如下所示:
Widget _buildTiles(Entry root) {
if (root.children.isEmpty) return new ListTile(
title: new Text(root.title, style: TextStye(fontWeight: FontWeight.bold));
return new ExpansionTile(
key: new PageStorageKey<Entry>(root),
title: new Text(root.title),
children: root.children.map(_buildTiles).toList(),
);
}
您可以使用该TextStyle小部件来改变颜色和样式,并将重量检查出Here
或者如果你想在扩展时做一些更改,需要在ExpantionTile选项中的onExpantionChange中进行更改,并且内部骰子不要忘记在setState中进行