这是我的第一篇文章,如果我忘记了什么,很抱歉......
对于我的工作,我必须使用 Material UI,并且我需要一个带有一些包含样式(如真正的按钮)的 IconButton!
我设法使用 Mui 组件的完整复制粘贴来完成此操作:https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/IconButton/IconButton.js
我所做的只是添加基于 Button 组件的包含样式的代码和样式,但我认为这不是正确的方法...我想导入 IconButton 作为别名并添加变体一些新样式的道具,但我不知道该怎么做。
这是我的组件:
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { chainPropTypes } from '@material-ui/utils';
import withStyles from '@material-ui/core/styles/withStyles';
import ButtonBase from '@material-ui/core/ButtonBase';
import { fade } from '@material-ui/core/styles/colorManipulator';
import capitalize from '@material-ui/core/utils/capitalize';
// TODO Better use of MUI possible ? -> Not copying the component but overriding it ?
// TODO Bug with Dark Mode
export const styles = …Run Code Online (Sandbox Code Playgroud) 我有一个IconButton,通常情况下,它的图标是Icons.expand_more,但是当我按下它的图标时,它的图标应该是Icons.expand_less。我想对其进行动画处理,这样如果我按下该按钮,它就会旋转并从上行词指向下行词。当我按下expand_less时,它应该变成expand_more旋转动画。我怎样才能做到这一点?下面是我的代码:
IconButton(
icon: _expanded ? Icon(Icons.expand_less) : Icon(Icons.expand_more),
onPressed: () {
setState(() {
_expanded = !_expanded;
});
},
)
Run Code Online (Sandbox Code Playgroud)
我尝试使用animatedContainer,但它不起作用,因为我使用两个不同的图标,而且我无法用它实现旋转效果。我也尝试用下面的方法旋转图标,但当它从0度旋转到180度时,它无法显示动画。
IconButton(
icon: AnimatedContainer(
duration: Duration(seconds: 3),
child: Transform.rotate(
angle: _expanded ? 0 : 180 * math.pi / 180,
child: Icon(Icons.expand_less))),
// icon:
// _expanded ? Icon(Icons.expand_less) : Icon(Icons.expand_more),
onPressed: () {
setState(() {
_expanded = !_expanded;
});
},
)
Run Code Online (Sandbox Code Playgroud)
这是扩展之前的:
这是扩展后的:
我想要点击按钮时的旋转动画。
我有一个带有多个 IconButton 的行,我需要更改它们的颜色和大小。我设法更改了颜色,但无法更改图标大小。
IconTheme(
data: IconThemeData(size: 48.0, color: Colors.yellow),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
icon: Icon(Icons.delete),
onPressed: () => null,
),
IconButton(
icon: Icon(Icons.file_upload),
onPressed: () => _addPhoto(false),
),
IconButton(
icon: Icon(Icons.camera_alt),
onPressed: () => _addPhoto(true),
),
],
),
),
Run Code Online (Sandbox Code Playgroud)
如果我使用 iconSize 在 IconButtons 中设置大小,它会起作用,但使用 IconTheme 则不会。
我该如何解决?
IconButton我想在图标下方制作一个带有文字的图标。我尝试将这些与宽度相关的方法应用于Modifier所有IconButton、Column和Icon。Text下面的代码是我得到的最接近的。结果看起来像这样。这就是我想要实现的目标。
@Composable
fun IconButtonWithTextBelow(
title: String,
@DrawableRes imageId: Int,
onClick: () -> Unit
) {
IconButton(
onClick = onClick,
modifier = Modifier.requiredWidth(IntrinsicSize.Max)
) {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.requiredWidth(IntrinsicSize.Max)
) {
Icon(
painter = painterResource(id = imageId),
contentDescription = title,
)
Text(
text = title,
)
}
}
}
Run Code Online (Sandbox Code Playgroud) 通过阅读文档,我确信这是明确声明的,但添加图标仍然是灰色的。
class _TaskState extends State<Task> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.red,
title: Text('Tasks'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: Icon(Icons.add),
color: Colors.white,
iconSize: 32.0,
),
],
),
drawer: TheDrawer()
);
}
}
Run Code Online (Sandbox Code Playgroud) 我Row在 Flutter 应用程序中有这个小部件,带有一些IconButtons
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
icon: Icon(Icons.skip_previous,
color: Colors.amber, size: 35),
onPressed: () {
setState(() {
pageIndex = 1;
});
}),
IconButton(
icon: Icon(Icons.arrow_left,
color: Colors.amber, size: 45),
onPressed: decIndex),
Text('Page $pageIndex',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.amber,
fontSize: 20,
fontWeight: FontWeight.bold)),
IconButton(
icon: Icon(Icons.arrow_right,
color: Colors.amber, size: 45),
onPressed: () {
incIndex(pageNumbers);
}),
IconButton(
icon: Icon(Icons.skip_next,
color: Colors.amber, size: 35),
onPressed: () {
setState(() {
pageIndex = pageNumbers;
});
}),
IconButton( …Run Code Online (Sandbox Code Playgroud) 我想为图标按钮应用背景色,但没有看到它的显式backgroundColor属性。我想实现这一目标:
目前,我能够做到:
下面是到目前为止的代码:
@override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
backgroundColor: Color(0xFF13212C),
appBar: AppBar(
title: Text('Demo'),
),
drawer: appDrawer(),
body: new Center(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
// mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new Column(
children: <Widget>[
// new Flexible(
new TextField(
style: new TextStyle(
color: Colors.white,
fontSize: 16.0),
cursorColor: Colors.green,
decoration: new InputDecoration(
suffixIcon: new IconButton(
icon: new Image.asset('assets/search_icon_ivory.png'),onPressed: null),
fillColor: Colors.black,
contentPadding: new EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
filled: true,
hintText: 'What Do You Need Help …Run Code Online (Sandbox Code Playgroud) 我想要一个带有文本的图标,所以我使用了TextButton.icon但我无法更改文本或图标的颜色!如果有人有解决方案,请建议这是我的代码:
SizedBox(height: 8,),
TextButton.icon(
icon: Icon(Icons.delete),
label: Text('delete'),
onPressed: (){},
),
Run Code Online (Sandbox Code Playgroud) 如何在Card可组合项上实现下图所示的效果,X例如图标挂在右上角?我不想要圆角,也不想要黑色背景,只想要挂在右上角的图标Card。尽管多次尝试,我还是无法实现这一目标。
SO 上的原始代码
Box(
modifier = Modifier
.background(LightGray)
.padding(16.dp)
.size(88.dp),
contentAlignment = Alignment.TopEnd
) {
Image(
painter = painterResource(
id = R.drawable.ic_launcher_foreground,
),
contentDescription = "",
modifier = Modifier
.align(Alignment.Center)
.clip(RoundedCornerShape(16.dp))
.background(Black)
.size(80.dp),
contentScale = ContentScale.Crop,
)
IconButton(
onClick = {},
modifier = Modifier
.clip(CircleShape)
.background(White)
.align(Alignment.TopEnd)
.size(16.dp)
) {
Icon(
imageVector = Icons.Rounded.Close,
contentDescription = "",
)
}
}
Run Code Online (Sandbox Code Playgroud)
可能的代码结构?
Box(...) {
Card(...) {
Image(...) {
}
}
IconButton(...) {
Icon(...) {
} …Run Code Online (Sandbox Code Playgroud) 我想知道如何在按下 IconButton 时更改其图标。(Favorite_border 到收藏夹)。我尝试了一些东西,但它不起作用。也许这很容易,但我是初学者,我不太了解它是如何工作的。
更新
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import '../recyclerview/data.dart';
import 'package:watch/constants.dart';
int itemCount = item.length;
List<bool> selected = new List<bool>();
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
initState() {
for (var i = 0; i < itemCount; i++) {
selected.add(false);
}
super.initState();
}
Icon notFavorite = Icon(Icons.favorite_border, size: 25,);
Icon inFavorite = Icon(Icons.favorite, size: 25,);
@override
Widget build(BuildContext context) …Run Code Online (Sandbox Code Playgroud)