扩展时如何更改扩展标题颜色?
How to change Expansion title Color when it is Expanded?
我是 flutter 的初学者 我想在扩展时更改扩展标题颜色 默认为蓝色 现在我想在扩展时更改扩展标题颜色 这里是我的代码 请检查它只更改标题颜色 任何一个在这里谁能帮助我?`
`Padding(
padding: EdgeInsets.only(left: 30),
child: ExpansionTile(
children: <Widget>[
Text(
'
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sollicitudin nulla risus, vel molestie quam mattis vitae. Quisque in neque in libero hendrerit ultricies a vel purus. Ut iaculis hendrerit nibh, nec pulvinar quam condimentum in. Fusce mollis elit vel lectus venenatis, sagittis sollicitudin lorem tristique. Quisque mattis vel dolor eget dictum. Integer facilisis tortor non lectus ullamcorpe'.',style: TextStyle(
fontSize: 14,
fontFamily: 'Avenir LT 55 Roman',
),)
],
title: Row(
children: <Widget>[
Image.asset(
'images/online.png',
),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Online()),
);
},
child: Text(
'Online Giving',
style: TextStyle(
fontSize: 20,
fontFamily: 'TT NORMS',
fontWeight: FontWeight.bold,
),
),
)
],
)),
),
要解决您的问题,您需要了解 statefulWidget 和 StatelessWidget 的用途。
您需要使用 StatefulWidget class 当您构建的小部件具有本地更改时,例如他自己的文本颜色 ... ext
当您的构建小部件没有本地更改(您自己进行更改)时,您需要一个 StatelessWidget class。
所以在你的代码中你有一个简单的小部件树,它由 :
Scaffold -> ListView -> [Tile1,Tile2... TileN]
.
脚手架、ListView 不需要本地更改,所以我们将它们设为 StatelessWidget。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
// each tile need her own state management since it depends on a setstate method
CustomExpensionTile(),
CustomExpensionTile(),
CustomExpensionTile(),
],
),
);
}
}
但是列表图块需要局部更改,例如您示例中的文本颜色,但您只希望其中之一在展开时进行更改。所以将它们放入单独的小部件中。
@override
_CustomExpensionTileState createState() => _CustomExpensionTileState();
}
class _CustomExpensionTileState extends State<CustomExpensionTile> {
bool _isExpanded;
@override
Widget build(BuildContext context) {
return ExpansionTile(
backgroundColor: Colors.transparent,
title: Text(
"test",
style: TextStyle(
color: _isExpanded ? Colors.red : Colors.black,
),
),
onExpansionChanged: (value) {
_isExpanded = value;
// whene setState methode is called the widget build function will be replayed with the new changes that we've done
setState(() {});
},
);
}
}
并且我们在 ExpansionChanges
事件上调用 setState
以使更改发生。
有关更多信息,请继续 https://flutter.dev/
我是 flutter 的初学者 我想在扩展时更改扩展标题颜色 默认为蓝色 现在我想在扩展时更改扩展标题颜色 这里是我的代码 请检查它只更改标题颜色 任何一个在这里谁能帮助我?`
`Padding(
padding: EdgeInsets.only(left: 30),
child: ExpansionTile(
children: <Widget>[
Text(
'
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sollicitudin nulla risus, vel molestie quam mattis vitae. Quisque in neque in libero hendrerit ultricies a vel purus. Ut iaculis hendrerit nibh, nec pulvinar quam condimentum in. Fusce mollis elit vel lectus venenatis, sagittis sollicitudin lorem tristique. Quisque mattis vel dolor eget dictum. Integer facilisis tortor non lectus ullamcorpe'.',style: TextStyle(
fontSize: 14,
fontFamily: 'Avenir LT 55 Roman',
),)
],
title: Row(
children: <Widget>[
Image.asset(
'images/online.png',
),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Online()),
);
},
child: Text(
'Online Giving',
style: TextStyle(
fontSize: 20,
fontFamily: 'TT NORMS',
fontWeight: FontWeight.bold,
),
),
)
],
)),
),
要解决您的问题,您需要了解 statefulWidget 和 StatelessWidget 的用途。
您需要使用 StatefulWidget class 当您构建的小部件具有本地更改时,例如他自己的文本颜色 ... ext
当您的构建小部件没有本地更改(您自己进行更改)时,您需要一个 StatelessWidget class。
所以在你的代码中你有一个简单的小部件树,它由 :
Scaffold -> ListView -> [Tile1,Tile2... TileN]
.
脚手架、ListView 不需要本地更改,所以我们将它们设为 StatelessWidget。
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
// each tile need her own state management since it depends on a setstate method
CustomExpensionTile(),
CustomExpensionTile(),
CustomExpensionTile(),
],
),
);
}
}
但是列表图块需要局部更改,例如您示例中的文本颜色,但您只希望其中之一在展开时进行更改。所以将它们放入单独的小部件中。
@override
_CustomExpensionTileState createState() => _CustomExpensionTileState();
}
class _CustomExpensionTileState extends State<CustomExpensionTile> {
bool _isExpanded;
@override
Widget build(BuildContext context) {
return ExpansionTile(
backgroundColor: Colors.transparent,
title: Text(
"test",
style: TextStyle(
color: _isExpanded ? Colors.red : Colors.black,
),
),
onExpansionChanged: (value) {
_isExpanded = value;
// whene setState methode is called the widget build function will be replayed with the new changes that we've done
setState(() {});
},
);
}
}
并且我们在 ExpansionChanges
事件上调用 setState
以使更改发生。
有关更多信息,请继续 https://flutter.dev/