https://genotechies.medium.co...
滑动和隐藏是移动应用程序中常见的 UI 模式。要在 Flutter 作到这一点,能够使用 Dismissible widget。它有一个 child,background 和 key 。它将检测滑动手势和动画的 child 小部件。你也能够双向和垂直的交换。你能够用本身的方式使用更多的属性。您能够经过复制并粘贴下面的代码来尝试。git
class _MyHomePageState extends State<MyHomePage> { List<String> _values = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']; @override Widget build(BuildContext context) { return ListView.separated( itemCount: _values.length, padding: const EdgeInsets.all(5.0), separatorBuilder: (context, index) => Divider( color: Colors.black, ), itemBuilder: (context, index) { return Dismissible( key: Key('item ${_values[index]}'), onDismissed: (DismissDirection direction) { if (direction == DismissDirection.startToEnd) { print("Selected Item"); } else { print('Delete item'); } setState(() { _values.removeAt(index); }); }, child: ListTile( leading: Icon(Icons.email, size: 50), title: Text(_values[index]), ), ); } ); } }
这是一个小部件示例。当你有一个小部件,应该是固定的大小。例如,一个按钮的大小应该为 width = 100px 和 height = 50px。您须要将按钮包装在 SizedBox 中。下面是类的构造函数。github
const SizedBox( {Key key, double width, double height, Widget child} )
在许多应用程序中,咱们能够看到拖动选项,如在电子邮件,文档拖动。有了这个 Flutter 小部件,很容易实现这个功能。在这里,咱们拖动数据。这里我传递一个从 Draggable 到 DragTarget 的字符串。而后你须要说明你传递的数据是什么,子属性显示你的数据。DragTarget 目标是拖曳 Draggable 的着陆区。主要有三种调用方法。编程
大多数时候,咱们使用行和列来显示一组子窗口小部件。但他们须要灵活的大小来显示与父母的相关性。您只须要将全部子窗口小部件包装在一个灵活的窗口小部件中。Flex 值决定每一个子元素得到多少空间。当改变屏幕大小时,它不会改变儿童之间的比例。api
child: Column( children: <Widget>[ Flexible( flex: 3, child: Container( color: Colors.red, ) ), Flexible( flex: 1, child: Container( color: Colors.green, ) ), Flexible( flex: 2, child: Container( color: Colors.blue, ) ), ], )
若是你的目标是在手机和选项卡上运行你的应用程序,你的应用程序须要支持不一样的用户界面大小。此外,有时用户有本身的 UI 指望,如字体大小或小,方向,填充等。使用这个 MediaQuery,您能够得到屏幕大小信息和用户首选项,并根据这些细节构建布局。微信
const MediaQueryData({ this.size = Size.zero, this.devicePixelRatio = 1.0, this.textScaleFactor = 1.0, this.platformBrightness = Brightness.light, this.padding = EdgeInsets.zero, this.viewInsets = EdgeInsets.zero, this.systemGestureInsets = EdgeInsets.zero, this.viewPadding = EdgeInsets.zero, this.alwaysUse24HourFormat = false, this.accessibleNavigation = false, this.invertColors = false, this.highContrast = false, this.disableAnimations = false, this.boldText = false, this.navigationMode = NavigationMode.traditional, })
这是一个提取屏幕尺寸的示例。app
MediaQueryData deviceInfo = MediaQuery.of(context);
输出框架
I/flutter ( 6508): size: Size(360.0, 592.0) I/flutter ( 6508): padding: EdgeInsets(0.0, 24.0, 0.0, 0.0) I/flutter (6508) : Size: Size (360.0,592.0) i/flutter (6508) : padding: EdgeInsets (0.0,24.0,0.0,0.0)
这是另外一个小部件,您最好在事先自定义中使用它。在一行中,咱们能够使用 MainAxisAlignment 定义子级之间的空间。可是使用 Spacer 小部件,你能够作得更多。只需在其余小部件之间添加间隔符便可。而后 children 扩展开来制造额外的空间。有一个 flex 属性来肯定相对大小。编程语言
SizedBox( height: 50, child: Row( children: <Widget>[ Container( width: 50, color: Colors.red, ), Spacer(flex: 2,), Container( width: 50, color: Colors.green, ), Spacer(flex: 1,), Container( width: 50, color: Colors.blue, ), Container( width: 50, color: Colors.yellow, ), ], ), );
已经有一个巨大的图标集已经在框架。也有动画图标,你能够在你的应用程序中使用。要使用这些,咱们须要一个 AnimatedIcon 小部件。你须要提供图标和主要的进度属性。Flutter 提供了许多不一样的动画图标供您使用。ide
import 'package:flutter/animation.dart'; import 'package:flutter/material.dart'; void main() => runApp(LogoApp()); class LogoApp extends StatefulWidget { _LogoAppState createState() => _LogoAppState(); } class _LogoAppState extends State<LogoApp> with SingleTickerProviderStateMixin { bool isPlaying = false; Animation animation; AnimationController controller; @override void initState() { super.initState(); controller = AnimationController( duration: const Duration(milliseconds: 500), vsync: this); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: IconButton( iconSize: 70, icon: AnimatedIcon( icon: AnimatedIcons.play_pause, progress: controller, ), onPressed: () => _onpressed(), )), ), ); } @override void dispose() { controller.dispose(); super.dispose(); } _onpressed() { setState(() { isPlaying = !isPlaying; isPlaying ? controller.forward() : controller.reverse(); }); } }
有时您须要为 UI 的特定组件保留空间,直到最后肯定该组件的视图。所以,与其保留一个空间,咱们能够在那里放置 Plaholder 以便进一步实现。在你能够开始实施它以后。这将填补全部提到的空间。函数
Center( child: Column( children: <Widget>[ Container( child: Placeholder() ), Expanded( child: Row( children: <Widget>[ Flexible( flex: 1, child: Placeholder(color: Colors.red,), ), Flexible( flex: 4, child: Placeholder(color: Colors.green,), ), ], ), ) ], ) ),
文本是每一个应用程序的主要 UI 组件之一。所以字体设计很是重要。你必须注意文字的样式和外观,如文字大小、字体、样式等。有时候你须要显示一个结合了不一样风格的段落。用粗体表示强调,或用斜体表示,或用下划线表示,或用不一样的颜色,不一样的字体大小,或同时显示全部内容。你最好使用 RichText。下面是一个例子:
RichText( text: TextSpan( style: TextStyle(color: Colors.black, fontSize: 24), children: <TextSpan>[ TextSpan(text: 'Flutter ', style: TextStyle(color: Colors.red)), TextSpan(text: 'Placeholder '), TextSpan(text: 'Widget', style: TextStyle(decoration: TextDecoration.underline, fontStyle: FontStyle.italic)) ], ), )
在咱们的应用程序中,咱们使用列表视图来显示一组数据并滚动它们。一般,您不能移动和更改列表中的位置。ReorderbaleListView 是解决方案。有了它,用户能够长时间按下该项目,并将其放入一个新的他或她喜欢的地方。列表视图的每一个项都有一个用于标识该项的键,在移动该项时,调用 onReorder 方法并跟踪移动和更改。下面是一个例子。
class _TopListState extends State<TopList> { List<String> topMovies = [ "It Happened One Night(1934)", "Black Panther(2018)", "Citizen Kane(1941)", "Parasite (Gisaengchung)(2019)", "Avengers: Endgame(2019)", "The Wizard of Oz(1939)", "Casablanca(1942)", "Knives Out(2019)" ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("ReorderableListView Example"), ), body: ReorderableListView( onReorder: (int oldIndex, int newIndex) {}, children: getListItems(), ), ); } List<ListTile> getListItems() => topMovies .asMap() .map((i, item) => MapEntry(i, buildTenableListTile(item, i))) .values .toList(); ListTile buildTenableListTile(String item, int index) { return ListTile( key: ValueKey(item), title: Text(item), leading: Text("${index + 1}"), ); } }
© 猫哥
https://github.com/ducafecat/...
https://github.com/ducafecat/...
https://ducafecat.tech/catego...
https://space.bilibili.com/40...
https://space.bilibili.com/40...
https://space.bilibili.com/40...
https://space.bilibili.com/40...
https://space.bilibili.com/40...
https://space.bilibili.com/40...