分类 Flutter 下的文章

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is off -->
    <style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <!-- Show a splash screen on the activity. Automatically removed when
             the Flutter engine draws its first frame -->
        <item name="android:windowBackground">@drawable/launch_background</item>
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>
    <!-- Theme applied to the Android Window as soon as the process has started.
         This theme determines the color of the Android Window while your
         Flutter UI initializes, as well as behind your Flutter UI while its
         running.

         This Theme is only used starting with V2 of Flutter's Android embedding. -->
    <style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
        <item name="android:windowBackground">?android:colorBackground</item>
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>
</resources>

class ViewUtil {
  ///界面初始化完成
  static Future<Void> initFinish() async {
    Completer<Void> completer = Completer();

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      completer.complete();
    });

    return completer.future;
  }
}

void _init() async {
    var s = await ViewUtil.initFinish();
    print(s);
}

数据变化监听
除了使用 Obx 实现界面数据自动刷新外,GetX 提供了多种手动方式对响应式变量进行数据变化监听,当数据发生变化时执行自定义的逻辑,比如数据变更后重新请求接口等。

ever 当数据发生改变时触发
everAll 和 "ever "很像,只是监听的是多个响应式变量的变化,当其中一个发生变化就会触发回调
once 只在变量第一次被改变时被调用
debounce 防抖,即延迟一定时间调用,且在规定时间内只有最后一次改变会触发回调。如设置时间为 1 秒,发生了3次数据变化,每次间隔500毫秒,则只有最后一次变化会触发回调。
interval 时间间隔内只有最后一次变化会触发回调。如设置时间间隔为1秒,则在1秒内无论点击多少次都只有最后一次会触发回调,然后进入下一次的时间间隔

实例化和绑定,使用
GetBuilder(

init: CounterController(), //Controller 首次初始化
builder: (controller) {
    return Text("${controller.count}", style: const TextStyle(fontSize: 50));

})

直接实例化:
Get.put(CounterController());

延迟实例化,第一次使用时才被实例化:
Get.lazyPut(() => CounterController());

删除注入:
Get.delete();

查找:
final controller = Get.find();
// 或者
CounterController controller = Get.find();

普通路由

to:进入下一个界面
Get.to(CounterPage());

使用 arguments 进行参数传递:
Get.to(CounterPage(), arguments: count);
使用 arguments 方式可以传递任意类型的参数。
在下个页面获取参数:
dynamic args = Get.arguments;

off:进入下一个界面,且导航没有返回
Get.off(CounterPage());
offAll: 进入下一个界面并取消之前的所有路由
Get.offAll(CounterPage());

back:返回
Get.back();

返回传参:
Get.back(result: 'success');
获取返回参数:
var data = await Get.to(CounterPage());

有一点命名规则:
文件名用小写+_线。
类名用骆驼名字
私类或方法用_开头。

rand_page.dart 文件:

import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';

class RandPage extends StatefulWidget {
  const RandPage({Key? key}) : super(key: key);
  @override
  State<RandPage> createState() => _RandPageState();
}

class _RandPageState extends State<RandPage> {
  final _suggestions = <WordPair>[];
  final _biggerFont = const TextStyle(fontSize: 18.0);

  Widget _buildSuggestions() {
    return ListView.builder(
        padding: const EdgeInsets.all(16.0),
        itemBuilder: /*1*/ (context, i) {
          if (i.isOdd) return const Divider(); /*2*/

          final index = i ~/ 2; /*3*/
          if (index >= _suggestions.length) {
            _suggestions.addAll(generateWordPairs().take(10)); /*4*/
          }
          return _buildRow(_suggestions[index]);
        });
  }

  Widget _buildRow(WordPair pair) {
    return ListTile(
      title: Text(
        pair.asPascalCase,
        style: _biggerFont,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Startup Name Generator'),
      ),
      body: _buildSuggestions(),
    );
  }
  // #docregion RWS-class-only
}

main.dart 文件:

import 'package:flutter/material.dart';
import 'package:flutter_application_1/rand_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Startup Name Generator',
      home: RandPage(),
    );
  }
}