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...